Skip to content

Commit 91ec365

Browse files
committed
Refactor to accept second argument
1 parent bc90cd4 commit 91ec365

13 files changed

Lines changed: 223 additions & 18 deletions

File tree

lib/node_modules/@stdlib/string/remove-first/README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# removeFirst
2222

23-
> Remove the first character of a string.
23+
> Remove the first character(s) of a string.
2424
2525
<section class="usage">
2626

@@ -30,7 +30,7 @@ limitations under the License.
3030
var removeFirst = require( '@stdlib/string/remove-first' );
3131
```
3232

33-
#### removeFirst( str )
33+
#### removeFirst( str\[, n] )
3434

3535
Removes the first character of a `string`.
3636

@@ -42,6 +42,16 @@ out = removeFirst( 'Hidden Treasures' );
4242
// returns 'idden Treasures'
4343
```
4444

45+
If provided a second argument, the function removes the first `n` characters.
46+
47+
```javascript
48+
out = removeFirst( 'Hidden Treasures', 'H' );
49+
// returns 'idden Treasures'
50+
51+
out = removeFirst( 'Hidden Treasures', 'T' );
52+
// returns 'Hidden Treasures'
53+
```
54+
4555
</section>
4656

4757
<!-- /.usage -->
@@ -66,6 +76,15 @@ str = removeFirst( 'javaScript' );
6676

6777
str = removeFirst( 'Hidden Treasures' );
6878
// returns 'idden Treasures'
79+
80+
str = removeFirst( 'The Last of the Mohicans', 4 );
81+
// returns 'Last of the Mohicans'
82+
83+
str = removeFirst( '🐶🐮🐷🐰🐸', 2 );
84+
// returns '🐷🐰🐸'
85+
86+
str = removeFirst( '🐶🐮🐷🐰🐸', 10 );
87+
// returns ''
6988
```
7089

7190
</section>
@@ -89,6 +108,7 @@ Options:
89108
90109
-h, --help Print this message.
91110
-V, --version Print the package version.
111+
--n Number of characters to remove. Default: 1.
92112
```
93113

94114
</section>
@@ -107,8 +127,9 @@ eep
107127
To use as a [standard stream][standard-streams],
108128

109129
```bash
110-
$ echo -n 'beep' | remove-first
111-
eep
130+
$ echo -n 'beep\nboop' | remove-first --n=2
131+
be
132+
bo
112133
```
113134

114135
</section>

lib/node_modules/@stdlib/string/remove-first/bin/cli

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function main() {
4343
var flags;
4444
var args;
4545
var cli;
46+
var n;
4647

4748
// Create a command-line interface:
4849
cli = new CLI({
@@ -58,6 +59,9 @@ function main() {
5859
if ( flags.help || flags.version ) {
5960
return;
6061
}
62+
if ( flags.n ) {
63+
n = parseInt( flags.n, 10 );
64+
}
6165

6266
// Get any provided command-line arguments:
6367
args = cli.args();
@@ -66,7 +70,11 @@ function main() {
6670
if ( !stdinStream.isTTY ) {
6771
return stdin( onRead );
6872
}
69-
console.log( removeFirst( args[ 0 ] ) ); // eslint-disable-line no-console
73+
if ( n ) {
74+
console.log( removeFirst( args[ 0 ], n ) ); // eslint-disable-line no-console
75+
} else {
76+
console.log( removeFirst( args[ 0 ] ) ); // eslint-disable-line no-console
77+
}
7078

7179
/**
7280
* Callback invoked upon reading from `stdin`.
@@ -83,8 +91,14 @@ function main() {
8391
return cli.error( error );
8492
}
8593
lines = data.toString().split( reEOL.REGEXP );
86-
for ( i = 0; i < lines.length; i++ ) {
87-
console.log( removeFirst( lines[ i ] ) ); // eslint-disable-line no-console
94+
if ( n ) {
95+
for ( i = 0; i < lines.length; i++ ) {
96+
console.log( removeFirst( lines[ i ], n ) ); // eslint-disable-line no-console
97+
}
98+
} else {
99+
for ( i = 0; i < lines.length; i++ ) {
100+
console.log( removeFirst( lines[ i ] ) ); // eslint-disable-line no-console
101+
}
88102
}
89103
}
90104
}

lib/node_modules/@stdlib/string/remove-first/docs/repl.txt

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

2-
{{alias}}( str )
3-
Removes the first character of a `string`.
2+
{{alias}}( str[, n] )
3+
Removes the first character(s) of a `string`.
44

55
Parameters
66
----------
77
str: string
88
Input string.
99

10+
n: integer (optional)
11+
Number of characters to remove. Default: 1.
12+
1013
Returns
1114
-------
1215
out: string
@@ -18,6 +21,8 @@
1821
'eep'
1922
> out = {{alias}}( 'Boop' )
2023
'oop'
24+
> out = {{alias}}( 'foo bar', 4 )
25+
'bar'
2126

2227
See Also
2328
--------

lib/node_modules/@stdlib/string/remove-first/docs/types/index.d.ts

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

2121
/**
22-
* Removes the first character of a string.
22+
* Removes the first character(s) of a string.
2323
*
2424
* @param str - input string
25+
* @param n - number of characters to remove (default: 1)
2526
* @returns updated string
2627
*
2728
* @example
@@ -39,8 +40,16 @@
3940
* @example
4041
* var out = removeFirst( 'Hidden Treasures' );
4142
* // returns 'idden Treasures'
43+
*
44+
* @example
45+
* var out = removeFirst( '🐶🐮🐷🐰🐸', 2 );
46+
* // returns '🐷🐰🐸'
47+
*
48+
* @example
49+
* var out = removeFirst( 'foo bar', 4 );
50+
* // returns 'bar'
4251
*/
43-
declare function removeFirst( str: string ): string;
52+
declare function removeFirst( str: string, n?: number ): string;
4453

4554

4655
// EXPORTS //

lib/node_modules/@stdlib/string/remove-first/docs/types/test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ import removeFirst = require( './index' );
3838
removeFirst( ( x: number ): number => x ); // $ExpectError
3939
}
4040

41+
// The function does not compile if provided a second argument that is not a number...
42+
{
43+
removeFirst( 'abc', true ); // $ExpectError
44+
removeFirst( 'abc', false ); // $ExpectError
45+
removeFirst( 'abc', null ); // $ExpectError
46+
removeFirst( 'abc', undefined ); // $ExpectError
47+
removeFirst( 'abc', 'abc' ); // $ExpectError
48+
removeFirst( 'abc', [] ); // $ExpectError
49+
removeFirst( 'abc', {} ); // $ExpectError
50+
removeFirst( 'abc', ( x: number ): number => x ); // $ExpectError
51+
}
52+
4153
// The function does not compile if provided insufficient arguments...
4254
{
4355
removeFirst(); // $ExpectError

lib/node_modules/@stdlib/string/remove-first/docs/usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Options:
55

66
-h, --help Print this message.
77
-V, --version Print the package version.
8+
--n Number of characters to remove. Default: 1.

lib/node_modules/@stdlib/string/remove-first/etc/cli_opts.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"help",
44
"version"
55
],
6+
"string": [
7+
"n"
8+
],
69
"alias": {
710
"help": [
811
"h"

lib/node_modules/@stdlib/string/remove-first/examples/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ console.log( removeFirst( 'javaScript' ) );
3131

3232
console.log( removeFirst( 'Hidden Treasures' ) );
3333
// => 'idden Treasures'
34+
35+
console.log( removeFirst( 'The Last of the Mohicans', 4 ) );
36+
// => 'Last of the Mohicans'
37+
38+
console.log( removeFirst( '🐶🐮🐷🐰🐸', 2 ) );
39+
// => '🐷🐰🐸'
40+
41+
console.log( removeFirst( '🐶🐮🐷🐰🐸', 10 ) );
42+
// => ''

lib/node_modules/@stdlib/string/remove-first/lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Remove the first character of a string.
22+
* Remove the first character(s) of a string.
2323
*
2424
* @module @stdlib/string/remove-first
2525
*
@@ -31,11 +31,14 @@
3131
*
3232
* out = removeFirst( 'Hidden Treasures' );
3333
* // returns 'idden Treasures';
34+
*
35+
* out = removeFirst( '🐮🐷🐸🐵', 2 );
36+
* // returns '🐸🐵
3437
*/
3538

3639
// MODULES //
3740

38-
var removeFirst = require( './remove_first.js' );
41+
var removeFirst = require( './main.js' );
3942

4043

4144
// EXPORTS //

lib/node_modules/@stdlib/string/remove-first/lib/remove_first.js renamed to lib/node_modules/@stdlib/string/remove-first/lib/main.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
// MODULES //
2222

2323
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
24+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
2425
var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' );
2526

2627

2728
// MAIN //
2829

2930
/**
30-
* Removes the first character of a string.
31+
* Removes the first character(s) of a string.
3132
*
3233
* @param {string} str - input string
34+
* @param {integer} [n=1] - number of characters to remove
3335
* @throws {TypeError} must provide a string primitive
36+
* @throws {TypeError} second argument must be a nonnegative integer
3437
* @returns {string} updated string
3538
*
3639
* @example
@@ -48,14 +51,33 @@ var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-br
4851
* @example
4952
* var out = removeFirst( 'Hidden Treasures' );
5053
* // returns 'idden Treasures'
54+
*
55+
* @example
56+
* var out = removeFirst( '🐶🐮🐷🐰🐸', 2 );
57+
* // returns '🐷🐰🐸'
58+
*
59+
* @example
60+
* var out = removeFirst( 'foo bar', 4 );
61+
* // returns 'bar'
5162
*/
52-
function removeFirst( str ) {
63+
function removeFirst( str, n ) {
5364
var nextBreak;
5465
if ( !isString( str ) ) {
5566
throw new TypeError( 'invalid argument. First argument must be a string primitive. Value: `' + str + '`.' );
5667
}
57-
nextBreak = nextGraphemeClusterBreak( str );
58-
68+
if ( arguments.length > 1 ) {
69+
if ( !isNonNegativeInteger( n ) ) {
70+
throw new TypeError( 'invalid argument. Second argument must be a nonnegative integer. Value: `' + n + '`.' );
71+
}
72+
nextBreak = 0;
73+
while ( n > 0 ) {
74+
nextBreak = nextGraphemeClusterBreak( str, nextBreak );
75+
n -= 1;
76+
}
77+
}
78+
else {
79+
nextBreak = nextGraphemeClusterBreak( str );
80+
}
5981
// Value of `nextBreak` will be -1 if and only if `str` is an empty string or `str` has only 1 extended grapheme cluster...
6082
if ( str === '' || nextBreak === -1 ) {
6183
return '';

0 commit comments

Comments
 (0)