Skip to content

Commit c78baf8

Browse files
committed
Update tests and benchmarks and add note
1 parent 6c470b9 commit c78baf8

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ var bool = endsWith( str, '' );
8686
// returns 2
8787
```
8888

89+
- This function differs from [`String.prototype.endsWith`][mdn-string-endswith] in the following ways:
90+
91+
- The function requires string values for the first and second arguments and requires that the `len` argument is an integer value.
92+
- The function does **not** clamp positive `len` values to the end of the input string.
93+
- Except when provided an empty `search` string, the function **always** returns `false` is a `len` resolves to a starting search position which exceeds the bounds of the input string.
94+
8995
</section>
9096

9197
<!-- /.notes -->
@@ -222,6 +228,8 @@ true
222228

223229
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
224230

231+
[mdn-string-endswith]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
232+
225233
<!-- <related-links> -->
226234

227235
[@stdlib/string/starts-with]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/starts-with

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ var pkg = require( './../package.json' ).name;
2626
var endsWith = require( './../lib' );
2727

2828

29+
// VARIABLES //
30+
31+
var opts = {
32+
'skip': typeof String.prototype.endsWith !== 'function'
33+
};
34+
35+
2936
// MAIN //
3037

3138
bench( pkg, function benchmark( b ) {
@@ -82,3 +89,58 @@ bench( pkg+'::substring', function benchmark( b ) {
8289
b.pass( 'benchmark finished' );
8390
b.end();
8491
});
92+
93+
bench( pkg+'::builtin', opts, function benchmark( b ) {
94+
var values;
95+
var bool;
96+
var str;
97+
var i;
98+
99+
values = [
100+
'.',
101+
'z',
102+
'questions'
103+
];
104+
str = 'To be, or not to be, that is the question.';
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
bool = str.endsWith( values[ i%values.length ] );
109+
if ( typeof bool !== 'boolean' ) {
110+
b.fail( 'should return a boolean' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isBoolean( bool ) ) {
115+
b.fail( 'should return a boolean' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});
120+
121+
bench( pkg+'::builtin,substring', opts, function benchmark( b ) {
122+
var values;
123+
var bool;
124+
var str;
125+
var i;
126+
127+
values = [
128+
'.',
129+
'z'
130+
];
131+
str = 'To be, or not to be, that is the question.';
132+
133+
b.tic();
134+
for ( i = 0; i < b.iterations; i++ ) {
135+
bool = str.endsWith( values[ i%values.length ], i%42 );
136+
if ( typeof bool !== 'boolean' ) {
137+
b.fail( 'should return a boolean' );
138+
}
139+
}
140+
b.toc();
141+
if ( !isBoolean( bool ) ) {
142+
b.fail( 'should return a boolean' );
143+
}
144+
b.pass( 'benchmark finished' );
145+
b.end();
146+
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ tape( 'the function returns `false` if provided a length parameter equal to `0`'
189189
t.end();
190190
});
191191

192+
tape( 'the function returns `false` if provided a length parameter which exceeds the input string length', function test( t ) {
193+
var bool = endsWith( 'abcdef', 'f', 9999 );
194+
t.strictEqual( bool, false, 'returns expected value' );
195+
t.end();
196+
});
197+
192198
tape( 'the function returns `false` if provided a search string which exceeds the input string length', function test( t ) {
193199
var bool = endsWith( 'abc', 'abcd' );
194200
t.strictEqual( bool, false, 'returns expected value' );

0 commit comments

Comments
 (0)