Skip to content

Commit e23f3f8

Browse files
committed
Clean-up docs, tests, and examples
1 parent 319d142 commit e23f3f8

File tree

9 files changed

+59
-58
lines changed

9 files changed

+59
-58
lines changed

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

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

21-
# Replace
21+
# replace
2222

2323
> Replace search occurrences with a replacement string.
2424
@@ -32,14 +32,14 @@ var replace = require( '@stdlib/string/replace' );
3232

3333
#### replace( str, search, newval )
3434

35-
Replaces search occurrences with a replacement `string`.
35+
Replaces search occurrences with a replacement string.
3636

3737
```javascript
3838
var out = replace( 'beep', 'e', 'o' );
3939
// returns 'boop'
4040
```
4141

42-
If provided a `function` as the [third argument][replacer], the function is invoked for each match, and the function's return value is used as the replacement `string`.
42+
If provided a function as the [third argument][mdn-string-replace], the function is invoked for each match, and the function's return value is used as the replacement string.
4343

4444
```javascript
4545
function replacer( match, p1 ) {
@@ -58,7 +58,7 @@ var out = replace( str, /([^\s]+)/gi, replacer );
5858

5959
## Notes
6060

61-
- The function has one notable difference from [`String.prototype.replace`][mdn]. When provided a `string` as the `search` value, the function replaces **all** occurrences. To remove only the first match, use a regular expression.
61+
- The function has one notable difference from [`String.prototype.replace`][mdn-string-replace]. When provided a string as the `search` value, the function replaces **all** occurrences. To remove only the first match, use a regular expression.
6262

6363
```javascript
6464
var out = replace( 'beep', /e/, 'o' );
@@ -79,22 +79,18 @@ var out = replace( str, /([^\s]+)/gi, replacer );
7979
var capitalize = require( '@stdlib/string/capitalize' );
8080
var replace = require( '@stdlib/string/replace' );
8181
82-
var out;
83-
var str;
84-
85-
out = replace( 'beep', 'e', 'o' );
82+
var out = replace( 'beep', 'e', 'o' );
8683
// returns 'boop'
8784
8885
out = replace( 'Hello World', /world/i, 'Mr. President' );
8986
// returns 'Hello Mr. President'
9087
91-
str = 'Oranges and lemons say the bells of St. Clement\'s';
92-
out = replace( str, /([^\s]*)/gi, replacer );
93-
// returns 'Oranges And Lemons Say The Bells Of St. Clement\'s'
94-
9588
function replacer( match, p1 ) {
9689
return capitalize( p1 );
9790
}
91+
var str = 'Oranges and lemons say the bells of St. Clement\'s';
92+
out = replace( str, /([^\s]*)/gi, replacer );
93+
// returns 'Oranges And Lemons Say The Bells Of St. Clement\'s'
9894
```
9995

10096
</section>
@@ -162,9 +158,7 @@ beep
162158

163159
<section class="links">
164160

165-
[mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
166-
167-
[replacer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter
161+
[mdn-string-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
168162

169163
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
170164

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,29 @@
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 replace = require( './../lib' );
2827

2928

3029
// MAIN //
3130

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

37+
values = [
38+
'abc',
39+
'def',
40+
'hig'
41+
];
3742
str = 'To be, or not to be, that is the question.';
3843

3944
b.tic();
4045
for ( i = 0; i < b.iterations; i++ ) {
41-
out = replace( str, 'be', fromCodePoint( i%126 ) );
42-
if ( !isString( out ) ) {
46+
out = replace( str, 'be', values[ i%values.length ] );
47+
if ( typeof out !== 'string' ) {
4348
b.fail( 'should return a string' );
4449
}
4550
}
@@ -52,18 +57,24 @@ bench( pkg+'::string', function benchmark( b ) {
5257
});
5358

5459
bench( pkg+'::regexp', function benchmark( b ) {
60+
var values;
5561
var out;
5662
var str;
5763
var re;
5864
var i;
5965

66+
values = [
67+
'abc',
68+
'def',
69+
'hig'
70+
];
6071
str = 'To be, or not to be, that is the question.';
6172
re = /be/g;
6273

6374
b.tic();
6475
for ( i = 0; i < b.iterations; i++ ) {
65-
out = replace( str, re, fromCodePoint( i%126 ) );
66-
if ( !isString( out ) ) {
76+
out = replace( str, re, values[ i%values.length ] );
77+
if ( typeof out !== 'string' ) {
6778
b.fail( 'should return a string' );
6879
}
6980
}
@@ -85,7 +96,7 @@ bench( pkg+'::replacer', function benchmark( b ) {
8596
b.tic();
8697
for ( i = 0; i < b.iterations; i++ ) {
8798
out = replace( str, 'be', replacer );
88-
if ( !isString( out ) ) {
99+
if ( typeof out !== 'string' ) {
89100
b.fail( 'should return a string' );
90101
}
91102
}

lib/node_modules/@stdlib/string/replace/docs/repl.txt

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

22
{{alias}}( str, search, newval )
3-
Replaces `search` occurrences with a replacement `string`.
3+
Replaces `search` occurrences with a replacement string.
44

5-
When provided a `string` as the `search` value, the function replaces *all*
5+
When provided a string as the search value, the function replaces *all*
66
occurrences. To remove only the first match, use a regular expression.
77

88
Parameters

lib/node_modules/@stdlib/string/replace/docs/types/index.d.ts

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

2121
/**
22-
* Replace search occurrences with a replacement string.
22+
* Replaces search occurrences with a replacement string.
2323
*
2424
* ## Notes
2525
*
26-
* - When provided a `string` as the `search` value, the function replaces *all* occurrences. To remove only the first match, use a regular expression.
26+
* - When provided a `string` as the `search` value, the function replaces **all** occurrences. To remove only the first match, use a regular expression.
2727
*
2828
* @param str - input string
2929
* @param search - search expression
@@ -52,11 +52,7 @@
5252
* var out = replace( str, /([^\s]*)/gi, replacer);
5353
* // returns 'Oranges And Lemons Say The Bells Of St. Clement\'s'
5454
*/
55-
declare function repeat(
56-
str: string,
57-
search: string | RegExp,
58-
newval: string | Function
59-
): string;
55+
declare function repeat( str: string, search: string | RegExp, newval: string | Function ): string; // tslint:disable-line:max-line-length
6056

6157

6258
// EXPORTS //

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

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

31-
// The function does not compile if provided arguments of invalid types...
31+
// The compiler throws an error if the function is provided arguments having invalid types...
3232
{
3333
replace( true, 'd', 'a' ); // $ExpectError
3434
replace( false, 'd', 'a' ); // $ExpectError
@@ -52,7 +52,7 @@ import replace = require( './index' );
5252
replace( 'abd', 'd', /[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
replace(); // $ExpectError
5858
replace( 'abc' ); // $ExpectError

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121
var capitalize = require( '@stdlib/string/capitalize' );
2222
var replace = require( './../lib' );
2323

24-
var out;
25-
var str;
26-
27-
console.log( replace( 'beep', 'e', 'o' ) );
24+
var out = replace( 'beep', 'e', 'o' );
25+
console.log( out );
2826
// => 'boop'
2927

30-
console.log( replace( 'Hello World', /world/i, 'Mr. President' ) );
31-
// => 'Hello Mr. President'
32-
33-
str = 'Oranges and lemons say the bells of St. Clement\'s';
34-
out = replace( str, /([^\s]*)/gi, replacer );
28+
out = replace( 'Hello World', /world/i, 'Mr. President' );
3529
console.log( out );
36-
// => 'Oranges And Lemons Say The Bells Of St. Clement\'s'
30+
// => 'Hello Mr. President'
3731

3832
function replacer( match, p1 ) {
3933
return capitalize( p1 );
4034
}
35+
var str = 'Oranges and lemons say the bells of St. Clement\'s';
36+
out = replace( str, /([^\s]*)/gi, replacer );
37+
console.log( out );
38+
// => 'Oranges And Lemons Say The Bells Of St. Clement\'s'

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

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

3838
// MODULES //
3939

40-
var replace = require( './replace.js' );
40+
var main = require( './main.js' );
4141

4242

4343
// EXPORTS //
4444

45-
module.exports = replace;
45+
module.exports = main;

lib/node_modules/@stdlib/string/replace/lib/replace.js renamed to lib/node_modules/@stdlib/string/replace/lib/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var format = require( '@stdlib/string/format' );
3030
// MAIN //
3131

3232
/**
33-
* Replace search occurrences with a replacement string.
33+
* Replaces search occurrences with a replacement string.
3434
*
3535
* @param {string} str - input string
3636
* @param {(string|RegExp)} search - search expression
@@ -67,10 +67,8 @@ function replace( str, search, newval ) {
6767
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
6868
}
6969
if ( isString( search ) ) {
70-
search = rescape( search );
71-
search = new RegExp( search, 'g' );
72-
}
73-
else if ( !isRegExp( search ) ) {
70+
search = new RegExp( rescape( search ), 'g' );
71+
} else if ( !isRegExp( search ) ) {
7472
throw new TypeError( format( 'invalid argument. Second argument must be a string or regular expression. Value: `%s`.', search ) );
7573
}
7674
if ( !isString( newval ) && !isFunction( newval ) ) {

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tape( 'the function throws an error if the first argument is not a string', func
4040
5,
4141
null,
4242
true,
43+
false,
4344
void 0,
4445
NaN,
4546
[],
@@ -68,6 +69,7 @@ tape( 'the function throws an error if the second argument is not a string or re
6869
5,
6970
null,
7071
true,
72+
false,
7173
void 0,
7274
NaN,
7375
[],
@@ -87,14 +89,15 @@ tape( 'the function throws an error if the second argument is not a string or re
8789
}
8890
});
8991

90-
tape( 'the function throws an error if the third argument is not a function or string primitive', function test( t ) {
92+
tape( 'the function throws an error if the third argument is not a function or string', function test( t ) {
9193
var values;
9294
var i;
9395

9496
values = [
9597
5,
9698
null,
9799
true,
100+
false,
98101
void 0,
99102
NaN,
100103
[],
@@ -117,13 +120,13 @@ tape( 'the function replaces all occurrences of a string search value', function
117120
var out;
118121

119122
out = replace( 'abc abc abc', 'b', '' );
120-
t.equal( out, 'ac ac ac', 'replaces all occurrences of letter `b`' );
123+
t.equal( out, 'ac ac ac', 'returns expected value' );
121124

122125
out = replace( 'abc abc abc', 'b', 'cd' );
123-
t.equal( out, 'acdc acdc acdc', 'replaces all occurrences of letter `b` by `cd`' );
126+
t.equal( out, 'acdc acdc acdc', 'returns expected value' );
124127

125128
out = replace( 'Et tu, Brute?', 'Brute?', 'Caesar?' );
126-
t.equal( out, 'Et tu, Caesar?', 'replaces `Brute?`' );
129+
t.equal( out, 'Et tu, Caesar?', 'returns expected value' );
127130

128131
t.end();
129132
});
@@ -143,19 +146,20 @@ tape( 'the function replaces matches of a regular expression', function test( t
143146
t.end();
144147
});
145148

146-
tape( 'the function replaces found matches by values created from a replacer function', function test( t ) {
149+
tape( 'the function replaces matches with values returned by a replacer function', function test( t ) {
147150
var expected;
148151
var out;
149152
var str;
150153

151-
function replacer( match, p1 ) {
152-
return '/' + p1 + '/';
153-
}
154-
155154
str = 'Oranges and lemons say the bells of St. Clement\'s';
156155
out = replace( str, /([^\s]+)/gi, replacer );
156+
157157
expected = '/Oranges/ /and/ /lemons/ /say/ /the/ /bells/ /of/ /St./ /Clement\'s/';
158158
t.equal( out, expected, 'replaces matches using replacer function' );
159159

160160
t.end();
161+
162+
function replacer( match, p1 ) {
163+
return '/' + p1 + '/';
164+
}
161165
});

0 commit comments

Comments
 (0)