Skip to content

Commit b18c30c

Browse files
committed
Refactor to use built-in if available
1 parent 388eeed commit b18c30c

File tree

14 files changed

+526
-15
lines changed

14 files changed

+526
-15
lines changed

lib/node_modules/@stdlib/string/repeat/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818
1919
-->
2020

21-
# Repeat
21+
# repeat
2222

2323
> Repeat a string a specified number of times and return the concatenated result.
2424
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var repeat = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var opts = {
32+
'skip': typeof String.prototype.repeat !== 'function'
33+
};
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveInteger} n - number of repeats
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( n ) {
46+
return benchmark;
47+
48+
/**
49+
* Benchmark function.
50+
*
51+
* @private
52+
* @param {Benchmark} b - benchmark instance
53+
*/
54+
function benchmark( b ) {
55+
var values;
56+
var out;
57+
var i;
58+
59+
values = [
60+
'beep',
61+
'boop',
62+
'baap',
63+
'biip'
64+
];
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
out = repeat( values[ i%values.length ], n );
69+
if ( typeof out !== 'string' ) {
70+
b.fail( 'should return a string' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isString( out ) ) {
75+
b.fail( 'should return a string' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var min;
92+
var max;
93+
var n;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10*min
98+
max = 10; // 10*max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
n = 10 * i;
102+
f = createBenchmark( n );
103+
bench( pkg+'::builtin:n='+n, opts, f );
104+
}
105+
}
106+
107+
main();

lib/node_modules/@stdlib/string/repeat/benchmark/benchmark.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

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

@@ -46,13 +45,21 @@ function createBenchmark( n ) {
4645
* @param {Benchmark} b - benchmark instance
4746
*/
4847
function benchmark( b ) {
48+
var values;
4949
var out;
5050
var i;
5151

52+
values = [
53+
'beep',
54+
'boop',
55+
'baap',
56+
'biip'
57+
];
58+
5259
b.tic();
5360
for ( i = 0; i < b.iterations; i++ ) {
54-
out = repeat( fromCodePoint( i%126 ) + 'eep', n );
55-
if ( !isString( out ) ) {
61+
out = repeat( values[ i%values.length ], n );
62+
if ( typeof out !== 'string' ) {
5663
b.fail( 'should return a string' );
5764
}
5865
}

lib/node_modules/@stdlib/string/repeat/docs/types/test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import repeat = require( './index' );
2626
repeat( 'a', 2 ); // $ExpectType string
2727
}
2828

29-
// The function does not compile if provided values other than a string and a number...
29+
// The compiler throws an error if the function is provided values other than a string and a number...
3030
{
3131
repeat( true, 3 ); // $ExpectError
3232
repeat( false, 2 ); // $ExpectError
@@ -49,7 +49,7 @@ import repeat = require( './index' );
4949
repeat( '5', ( x: number ): number => x ); // $ExpectError
5050
}
5151

52-
// The function does not compile if provided insufficient arguments...
52+
// The compiler throws an error if the function is provided insufficient arguments...
5353
{
5454
repeat(); // $ExpectError
5555
repeat( 3 ); // $ExpectError

lib/node_modules/@stdlib/string/repeat/docs/usage.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Usage: repstr [options] [<string>] --n <repeats>
2+
Usage: repstr [options] --n <repeats> [<string>]
33

44
Options:
55

lib/node_modules/@stdlib/string/repeat/examples/index.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818

1919
'use strict';
2020

21-
var round = require( '@stdlib/math/base/special/round' );
22-
var randu = require( '@stdlib/random/base/randu' );
21+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
2322
var repeat = require( './../lib' );
2423

25-
var str = 'beep';
26-
var n;
2724
var i;
28-
2925
for ( i = 0; i < 100; i++ ) {
30-
n = round( randu()*3.0 );
31-
console.log( repeat( str, n ) );
26+
console.log( repeat( 'beep', discreteUniform( 0, 3 ) ) );
3227
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MAIN //
22+
23+
var repeat = String.prototype.repeat;
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = repeat;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MAIN //
22+
23+
var bool = ( typeof String.prototype.repeat !== 'undefined' );
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = bool;

lib/node_modules/@stdlib/string/repeat/lib/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@
3838

3939
// MODULES //
4040

41-
var repeat = require( './repeat.js' );
41+
var HAS_BUILTIN = require( './has_builtin.js' );
42+
var polyfill = require( './polyfill.js' );
43+
var main = require( './main.js' );
44+
45+
46+
// MAIN //
47+
48+
var repeat;
49+
if ( HAS_BUILTIN ) {
50+
repeat = main;
51+
} else {
52+
repeat = polyfill;
53+
}
4254

4355

4456
// EXPORTS //
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var format = require( '@stdlib/string/format' );
26+
var builtin = require( './builtin.js' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Repeats a string a specified number of times and returns the concatenated result.
33+
*
34+
* @param {string} str - string to repeat
35+
* @param {NonNegativeInteger} n - number of times to repeat the string
36+
* @throws {TypeError} first argument must be a string
37+
* @throws {TypeError} second argument must be a nonnegative integer
38+
* @throws {RangeError} output string length must not exceed maximum allowed string length
39+
* @returns {string} repeated string
40+
*
41+
* @example
42+
* var str = repeat( 'a', 5 );
43+
* // returns 'aaaaa'
44+
*
45+
* @example
46+
* var str = repeat( '', 100 );
47+
* // returns ''
48+
*
49+
* @example
50+
* var str = repeat( 'beep', 0 );
51+
* // returns ''
52+
*/
53+
function repeat( str, n ) {
54+
if ( !isString( str ) ) {
55+
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
56+
}
57+
if ( !isNonNegativeInteger( n ) ) {
58+
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) );
59+
}
60+
return builtin.call( str, n );
61+
}
62+
63+
64+
// EXPORTS //
65+
66+
module.exports = repeat;

lib/node_modules/@stdlib/string/repeat/lib/repeat.js renamed to lib/node_modules/@stdlib/string/repeat/lib/polyfill.js

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var format = require( '@stdlib/string/format' );
7070
* The algorithm runs in `O(log_2(n))` compared to `O(n)`.
7171
*
7272
*
73+
* @private
7374
* @param {string} str - string to repeat
7475
* @param {NonNegativeInteger} n - number of times to repeat the string
7576
* @throws {TypeError} first argument must be a string

lib/node_modules/@stdlib/string/repeat/test/test.js

+21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var proxyquire = require( 'proxyquire' );
25+
var polyfill = require( './../lib/polyfill.js' );
26+
var main = require( './../lib/main.js' );
2427
var repeat = require( './../lib' );
2528

2629

@@ -32,13 +35,30 @@ tape( 'main export is a function', function test( t ) {
3235
t.end();
3336
});
3437

38+
tape( 'the main export is a polyfill if an environment does not support String.prototype.repeat', function test( t ) {
39+
var repeat = proxyquire( './../lib', {
40+
'./has_builtin.js': false
41+
});
42+
t.equal( repeat, polyfill, 'returns expected value' );
43+
t.end();
44+
});
45+
46+
tape( 'the main export is a wrapper around a builtin if an environment supports String.prototype.repeat', function test( t ) {
47+
var repeat = proxyquire( './../lib', {
48+
'./has_builtin.js': true
49+
});
50+
t.equal( repeat, main, 'returns expected value' );
51+
t.end();
52+
});
53+
3554
tape( 'if the first argument is not a string primitive, the function will throw an error', function test( t ) {
3655
var values;
3756
var i;
3857

3958
values = [
4059
5,
4160
true,
61+
false,
4262
NaN,
4363
null,
4464
void 0,
@@ -67,6 +87,7 @@ tape( 'if the second argument is not a nonnegative integer, the function will th
6787
3.14,
6888
-5,
6989
true,
90+
false,
7091
NaN,
7192
null,
7293
void 0,

0 commit comments

Comments
 (0)