Skip to content

Commit 29b346f

Browse files
committed
Rename file, update benchmarks, and clean-up
1 parent 3f5f9af commit 29b346f

10 files changed

Lines changed: 134 additions & 28 deletions

File tree

lib/node_modules/@stdlib/string/left-pad/README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818
1919
-->
2020

21-
# Left Pad
21+
# lpad
2222

2323
> Left pad a string.
2424
@@ -32,14 +32,14 @@ var lpad = require( '@stdlib/string/left-pad' );
3232

3333
#### lpad( str, len\[, pad] )
3434

35-
Left pads a `string` such that the padded `string` has a `length` of **at least** `len`.
35+
Left pads a string such that the padded string has a `length` of **at least** `len`.
3636

3737
```javascript
3838
var str = lpad( 'a', 5 );
3939
// returns ' a'
4040
```
4141

42-
By default, an input `string` is padded with `spaces`. To pad with a different character or sequence of characters, provide a `pad` string.
42+
By default, an input string is padded with a Unicode "space" character (U+0020). To pad with a different character or sequence of characters, provide a `pad` string.
4343

4444
```javascript
4545
var str = lpad( 'beep', 10, 'b' );
@@ -78,17 +78,13 @@ str = lpad( 'boop', 12, 'beep' );
7878
<!-- eslint no-undef: "error" -->
7979

8080
```javascript
81-
var round = require( '@stdlib/math/base/special/round' );
82-
var randu = require( '@stdlib/random/base/randu' );
81+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
8382
var lpad = require( '@stdlib/string/left-pad' );
8483
8584
var str = 'beep';
86-
var n;
8785
var i;
88-
8986
for ( i = 0; i < 100; i++ ) {
90-
n = round( randu()*10 ) + str.length;
91-
console.log( lpad( str, n, 'b' ) );
87+
console.log( lpad( str, discreteUniform( str.length, str.length+10 ), 'b' ) );
9288
}
9389
```
9490

@@ -107,7 +103,7 @@ for ( i = 0; i < 100; i++ ) {
107103
### Usage
108104

109105
```text
110-
Usage: lpad [options] [<string>] --len=<length>
106+
Usage: lpad [options] --len=<length> [<string>]
111107
112108
Options:
113109
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
27+
28+
// VARIABLES //
29+
30+
var opts = {
31+
'skip': typeof String.prototype.padStart !== 'function'
32+
};
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - string length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var values;
55+
var out;
56+
var i;
57+
58+
values = [
59+
'a',
60+
'b',
61+
'c'
62+
];
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
out = 'beep'.padStart( len, values[ i%values.length ] );
67+
if ( typeof out !== 'string' ) {
68+
b.fail( 'should return a string' );
69+
}
70+
}
71+
b.toc();
72+
if ( !isString( out ) ) {
73+
b.fail( 'should return a string' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10*min
96+
max = 10; // 10*max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = 10 * i;
100+
f = createBenchmark( len );
101+
bench( pkg+'::builtin:len='+len, opts, f );
102+
}
103+
}
104+
105+
main();

lib/node_modules/@stdlib/string/left-pad/benchmark/benchmark.js

Lines changed: 9 additions & 3 deletions
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 lpad = require( './../lib' );
2827

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

52+
values = [
53+
'a',
54+
'b',
55+
'c'
56+
];
57+
5258
b.tic();
5359
for ( i = 0; i < b.iterations; i++ ) {
54-
out = lpad( 'beep', len, fromCodePoint( i%126 ) );
55-
if ( !isString( out ) ) {
60+
out = lpad( 'beep', len, values[ i%values.length ] );
61+
if ( typeof out !== 'string' ) {
5662
b.fail( 'should return a string' );
5763
}
5864
}

lib/node_modules/@stdlib/string/left-pad/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
{{alias}}( str, len[, pad] )
3-
Left pads a `string` such that the padded `string` has a length of at least
3+
Left pads a string such that the padded string has a length of at least
44
`len`.
55

66
An output string is not guaranteed to have a length of exactly `len`, but to

lib/node_modules/@stdlib/string/left-pad/docs/types/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import lpad = require( './index' );
2828
lpad( 'abd', 10 ); // $ExpectType string
2929
}
3030

31-
// The function does not compile if provided arguments having invalid types...
31+
// The compiler throws an error if the function is provided arguments having invalid types...
3232
{
3333
lpad( true, 6 ); // $ExpectError
3434
lpad( false, 6 ); // $ExpectError
@@ -52,7 +52,7 @@ import lpad = require( './index' );
5252
lpad( 'abd', 6, /[a-z]/ ); // $ExpectError
5353
}
5454

55-
// The function does not compile if provided insufficient arguments...
55+
// The compiler throws an error if the function is provided insufficient arguments...
5656
{
5757
lpad(); // $ExpectError
5858
lpad( 'abc' ); // $ExpectError

lib/node_modules/@stdlib/string/left-pad/docs/usage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Usage: lpad [options] [<string>] --len=<length>
2+
Usage: lpad [options] --len=<length> [<string>]
33

44
Options:
55

lib/node_modules/@stdlib/string/left-pad/examples/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
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 lpad = require( './../lib' );
2423

2524
var str = 'beep';
26-
var n;
2725
var i;
28-
2926
for ( i = 0; i < 100; i++ ) {
30-
n = round( randu()*10 ) + str.length;
31-
console.log( lpad( str, n, 'b' ) );
27+
console.log( lpad( str, discreteUniform( str.length, str.length+10 ), 'b' ) );
3228
}

lib/node_modules/@stdlib/string/left-pad/lib/index.js

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

3939
// MODULES //
4040

41-
var lpad = require( './left_pad.js' );
41+
var main = require( './main.js' );
4242

4343

4444
// EXPORTS //
4545

46-
module.exports = lpad;
46+
module.exports = main;

lib/node_modules/@stdlib/string/left-pad/lib/left_pad.js renamed to lib/node_modules/@stdlib/string/left-pad/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
2424
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25-
var repeat = require( '@stdlib/string/repeat' );
25+
var repeat = require( '@stdlib/string/base/repeat' );
2626
var ceil = require( '@stdlib/math/base/special/ceil' );
2727
var format = require( '@stdlib/string/format' );
2828
var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );

lib/node_modules/@stdlib/string/left-pad/test/test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ tape( 'main export is a function', function test( t ) {
3232
t.end();
3333
});
3434

35-
tape( 'if the first argument is not a string primitive, the function will throw an error', function test( t ) {
35+
tape( 'if the first argument is not a string, the function will throw an error', function test( t ) {
3636
var values;
3737
var i;
3838

3939
values = [
4040
5,
4141
true,
42+
false,
4243
NaN,
4344
null,
4445
void 0,
@@ -67,6 +68,7 @@ tape( 'if the second argument is not a nonnegative integer, the function will th
6768
3.14,
6869
-5,
6970
true,
71+
false,
7072
NaN,
7173
null,
7274
void 0,
@@ -86,13 +88,14 @@ tape( 'if the second argument is not a nonnegative integer, the function will th
8688
}
8789
});
8890

89-
tape( 'if the third argument is not a string primitive, the function will throw an error', function test( t ) {
91+
tape( 'if the third argument is not a string, the function will throw an error', function test( t ) {
9092
var values;
9193
var i;
9294

9395
values = [
9496
5,
9597
true,
98+
false,
9699
NaN,
97100
null,
98101
void 0,

0 commit comments

Comments
 (0)