Skip to content

Commit 502dbc3

Browse files
committed
Refactor to use base implementation
1 parent 22dc39d commit 502dbc3

File tree

7 files changed

+69
-66
lines changed

7 files changed

+69
-66
lines changed

lib/node_modules/@stdlib/string/ends-with/README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var endsWith = require( '@stdlib/string/ends-with' );
3838

3939
#### endsWith( str, search\[, len] )
4040

41-
Tests if a `string` ends with the characters of another `string`.
41+
Tests if a string ends with the characters of another string.
4242

4343
```javascript
4444
var str = 'Remember the story I used to tell you when you were a boy?';
@@ -75,6 +75,21 @@ var bool = endsWith( str, '' );
7575

7676
<!-- /.usage -->
7777

78+
<section class="notes">
79+
80+
## Notes
81+
82+
- In general, exercise caution when operating on substrings containing Unicode characters, as the visual character length may not equal the number of code points. For example,
83+
84+
```javascript
85+
var len = '🏠'.length;
86+
// returns 2
87+
```
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
7893
<section class="examples">
7994

8095
## Examples
@@ -84,12 +99,9 @@ var bool = endsWith( str, '' );
8499
```javascript
85100
var endsWith = require( '@stdlib/string/ends-with' );
86101
87-
var bool;
88-
var str;
89-
90-
str = 'Fair is foul, and foul is fair, hover through fog and filthy air';
102+
var str = 'Fair is foul, and foul is fair, hover through fog and filthy air';
91103
92-
bool = endsWith( str, 'air' );
104+
var bool = endsWith( str, 'air' );
93105
// returns true
94106
95107
bool = endsWith( str, 'fair' );

lib/node_modules/@stdlib/string/ends-with/benchmark/benchmark.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,28 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25-
var fromCodePoint = require( '@stdlib/string/from-code-point' );
2625
var pkg = require( './../package.json' ).name;
2726
var endsWith = require( './../lib' );
2827

2928

3029
// MAIN //
3130

3231
bench( pkg, function benchmark( b ) {
32+
var values;
3333
var bool;
3434
var str;
3535
var i;
3636

37+
values = [
38+
'.',
39+
'z'
40+
];
3741
str = 'To be, or not to be, that is the question.';
3842

3943
b.tic();
4044
for ( i = 0; i < b.iterations; i++ ) {
41-
bool = endsWith( str, fromCodePoint( i%126 ) );
42-
if ( !isBoolean( bool ) ) {
45+
bool = endsWith( str, values[ i%values.length ] );
46+
if ( typeof bool !== 'boolean' ) {
4347
b.fail( 'should return a boolean' );
4448
}
4549
}
@@ -51,17 +55,22 @@ bench( pkg, function benchmark( b ) {
5155
b.end();
5256
});
5357

54-
bench( pkg+'::search', function benchmark( b ) {
58+
bench( pkg+'::substring', function benchmark( b ) {
59+
var values;
5560
var bool;
5661
var str;
5762
var i;
5863

64+
values = [
65+
'.',
66+
'z'
67+
];
5968
str = 'To be, or not to be, that is the question.';
6069

6170
b.tic();
6271
for ( i = 0; i < b.iterations; i++ ) {
63-
bool = endsWith( str, 'to be', i%42 );
64-
if ( !isBoolean( bool ) ) {
72+
bool = endsWith( str, values[ i%values.length ], i%42 );
73+
if ( typeof bool !== 'boolean' ) {
6574
b.fail( 'should return a boolean' );
6675
}
6776
}

lib/node_modules/@stdlib/string/ends-with/docs/repl.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
{{alias}}( str, search[, len] )
3-
Tests if a `string` ends with the characters of another `string`.
3+
Tests if a string ends with the characters of another string.
44

5-
If provided an empty `search` string, the function always returns `true`.
5+
If provided an empty search string, the function always returns `true`.
66

77
Parameters
88
----------
@@ -21,8 +21,8 @@
2121
Returns
2222
-------
2323
bool: boolean
24-
Boolean indicating whether a `string` ends with the characters of
25-
another `string`.
24+
Boolean indicating whether a string ends with the characters of another
25+
string.
2626

2727
Examples
2828
--------

lib/node_modules/@stdlib/string/ends-with/docs/types/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
// TypeScript Version: 2.0
2020

2121
/**
22-
* Test if a string ends with the characters of another string.
22+
* Tests if a string ends with the characters of another string.
23+
*
24+
* ## Notes
25+
*
26+
* - The last parameter restricts the search to a substring within the input string beginning from the leftmost character. If provided a negative value, `len` indicates to ignore the last `len` characters, returning the same output as `str.length + len`.
2327
*
2428
* @param str - input string
2529
* @param search - search string

lib/node_modules/@stdlib/string/ends-with/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343

4444
// MODULES //
4545

46-
var endsWith = require( './ends_with.js' );
46+
var main = require( './main.js' );
4747

4848

4949
// EXPORTS //
5050

51-
module.exports = endsWith;
51+
module.exports = main;

lib/node_modules/@stdlib/string/ends-with/lib/ends_with.js renamed to lib/node_modules/@stdlib/string/ends-with/lib/main.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2424
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2525
var format = require( '@stdlib/string/format' );
26+
var base = require( '@stdlib/string/base/ends-with' );
2627

2728

2829
// MAIN //
@@ -59,8 +60,6 @@ var format = require( '@stdlib/string/format' );
5960
* // returns true
6061
*/
6162
function endsWith( str, search, len ) {
62-
var idx;
63-
var i;
6463
if ( !isString( str ) ) {
6564
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
6665
}
@@ -71,31 +70,10 @@ function endsWith( str, search, len ) {
7170
if ( !isInteger( len ) ) {
7271
throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', len ) );
7372
}
74-
if ( len === 0 ) {
75-
return ( search.length === 0 );
76-
}
77-
if ( len < 0 ) {
78-
idx = str.length + len;
79-
} else {
80-
idx = len;
81-
}
8273
} else {
83-
idx = str.length;
84-
}
85-
if ( search.length === 0 ) {
86-
// Based on the premise that every string can be "surrounded" by empty strings (e.g., "" + "a" + "" + "b" + "" === "ab"):
87-
return true;
88-
}
89-
idx -= search.length;
90-
if ( idx < 0 ) {
91-
return false;
92-
}
93-
for ( i = 0; i < search.length; i++) {
94-
if ( str.charCodeAt( idx + i ) !== search.charCodeAt( i ) ) {
95-
return false;
96-
}
74+
len = str.length;
9775
}
98-
return true;
76+
return base( str, search, len );
9977
}
10078

10179

lib/node_modules/@stdlib/string/ends-with/test/test.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ tape( 'the function returns `true` if the input string ends with the search valu
118118
var bool;
119119

120120
bool = endsWith( 'Too late, I\'m afraid', 'afraid' );
121-
t.strictEqual( bool, true, 'returns true' );
121+
t.strictEqual( bool, true, 'returns expected value' );
122122

123123
bool = endsWith( 'Not too late, I\'m afraid...', 'afraid...' );
124-
t.strictEqual( bool, true, 'returns true' );
124+
t.strictEqual( bool, true, 'returns expected value' );
125125

126126
bool = endsWith( 'Welcome home!', 'home!' );
127-
t.strictEqual( bool, true, 'returns true' );
127+
t.strictEqual( bool, true, 'returns expected value' );
128128

129129
t.end();
130130
});
@@ -133,13 +133,13 @@ tape( 'the function returns `false` if the input string does not end with the se
133133
var bool;
134134

135135
bool = endsWith( 'Too late, I\'m afraid', 'Afraid' );
136-
t.strictEqual( bool, false, 'returns false' );
136+
t.strictEqual( bool, false, 'returns expected value' );
137137

138138
bool = endsWith( 'Not too late, I\'m afraid...', 'afraid' );
139-
t.strictEqual( bool, false, 'returns false' );
139+
t.strictEqual( bool, false, 'returns expected value' );
140140

141141
bool = endsWith( 'Welcome home!', 'welcome home!' );
142-
t.strictEqual( bool, false, 'returns false' );
142+
t.strictEqual( bool, false, 'returns expected value' );
143143

144144
t.end();
145145
});
@@ -151,13 +151,13 @@ tape( 'the function supports restricting the search to a substring by providing
151151
str = 'Too late, I\'m afraid';
152152

153153
bool = endsWith( str, 'late', 7 );
154-
t.strictEqual( bool, false, 'returns false' );
154+
t.strictEqual( bool, false, 'returns expected value' );
155155

156156
bool = endsWith( str, 'late', 8 );
157-
t.strictEqual( bool, true, 'returns true' );
157+
t.strictEqual( bool, true, 'returns expected value' );
158158

159159
bool = endsWith( str, 'late', 9 );
160-
t.strictEqual( bool, false, 'returns false' );
160+
t.strictEqual( bool, false, 'returns expected value' );
161161

162162
t.end();
163163
});
@@ -169,61 +169,61 @@ tape( 'the function supports restricting the search to a substring by providing
169169
str = 'Too late, I\'m afraid';
170170

171171
bool = endsWith( str, 'i', -1 );
172-
t.strictEqual( bool, true, 'returns true' );
172+
t.strictEqual( bool, true, 'returns expected value' );
173173

174174
bool = endsWith( str, 'late', -13 );
175-
t.strictEqual( bool, false, 'returns false' );
175+
t.strictEqual( bool, false, 'returns expected value' );
176176

177177
bool = endsWith( str, 'late', -12 );
178-
t.strictEqual( bool, true, 'returns true' );
178+
t.strictEqual( bool, true, 'returns expected value' );
179179

180180
bool = endsWith( str, 'late', -11 );
181-
t.strictEqual( bool, false, 'returns false' );
181+
t.strictEqual( bool, false, 'returns expected value' );
182182

183183
t.end();
184184
});
185185

186186
tape( 'the function returns `false` if provided a length parameter equal to `0`', function test( t ) {
187187
var bool = endsWith( 'abc', 'c', 0 );
188-
t.strictEqual( bool, false, 'returns false' );
188+
t.strictEqual( bool, false, 'returns expected value' );
189189
t.end();
190190
});
191191

192192
tape( 'the function returns `false` if provided a search string which exceeds the input string length', function test( t ) {
193193
var bool = endsWith( 'abc', 'abcd' );
194-
t.strictEqual( bool, false, 'returns false' );
194+
t.strictEqual( bool, false, 'returns expected value' );
195195
t.end();
196196
});
197197

198198
tape( 'the function returns `false` if provided a search string which exceeds the input (sub)string length', function test( t ) {
199199
var bool = endsWith( 'abcdef', 'abcd', 3 );
200-
t.strictEqual( bool, false, 'returns false' );
200+
t.strictEqual( bool, false, 'returns expected value' );
201201
t.end();
202202
});
203203

204204
tape( 'the function returns `false` if provided a search string which exceeds the input (sub)string length (negative)', function test( t ) {
205205
var bool = endsWith( 'abcdef', 'abcd', -3 );
206-
t.strictEqual( bool, false, 'returns false' );
206+
t.strictEqual( bool, false, 'returns expected value' );
207207
t.end();
208208
});
209209

210210
tape( 'the function returns `true` if provided an empty search string', function test( t ) {
211211
var bool;
212212

213213
bool = endsWith( '', '' );
214-
t.strictEqual( bool, true, 'returns true' );
214+
t.strictEqual( bool, true, 'returns expected value' );
215215

216216
bool = endsWith( 'abc', '' );
217-
t.strictEqual( bool, true, 'returns true' );
217+
t.strictEqual( bool, true, 'returns expected value' );
218218

219219
bool = endsWith( 'abc', '', 10 );
220-
t.strictEqual( bool, true, 'returns true' );
220+
t.strictEqual( bool, true, 'returns expected value' );
221221

222222
bool = endsWith( 'abc', '', -10 );
223-
t.strictEqual( bool, true, 'returns true' );
223+
t.strictEqual( bool, true, 'returns expected value' );
224224

225225
bool = endsWith( 'abc', '', 0 );
226-
t.strictEqual( bool, true, 'returns true' );
226+
t.strictEqual( bool, true, 'returns expected value' );
227227

228228
t.end();
229229
});

0 commit comments

Comments
 (0)