Skip to content

Commit d1ded85

Browse files
steff456kgryte
andauthored
feat: add "base" string packages and refactor string/remove-first
This commit adds the following new packages: - `@stdlib/string/base/remove-first` - `@stdlib/string/base/remove-first-code-point` - `@stdlib/string/base/remove-first-grapheme-cluster` The top-level `@stdlib/string/remove-first` is refactored to depend on those "base" packages, with the default behavior remaining the same (i.e., removing the first grapheme cluster) and a new "mode" option for specifying what type of character to remove. PR-URL: #1073 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Ref: #1062
1 parent e09914b commit d1ded85

File tree

41 files changed

+2491
-40
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2491
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# removeFirstCodePoint
22+
23+
> Remove the first `n` Unicode code points of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var removeFirstCodePoint = require( '@stdlib/string/base/remove-first-code-point' );
31+
```
32+
33+
#### removeFirstCodePoint( str, n )
34+
35+
Removes the first `n` Unicode code points of a string.
36+
37+
```javascript
38+
var out = removeFirstCodePoint( 'last man standing', 1 );
39+
// returns 'ast man standing'
40+
41+
out = removeFirstCodePoint( 'Hidden Treasures', 1 );
42+
// returns 'idden Treasures'
43+
44+
out = removeFirstCodePoint( 'foo bar', 5 );
45+
// returns 'ar'
46+
47+
out = removeFirstCodePoint( 'foo bar', 10 );
48+
// returns ''
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
<!-- eslint no-undef: "error" -->
60+
61+
```javascript
62+
var removeFirstCodePoint = require( '@stdlib/string/base/remove-first-code-point' );
63+
64+
var str = removeFirstCodePoint( 'presidential election', 1 );
65+
// returns 'residential election'
66+
67+
str = removeFirstCodePoint( 'JavaScript', 1 );
68+
// returns 'avaScript'
69+
70+
str = removeFirstCodePoint( 'The Last of the Mohicans', 5 );
71+
// returns 'ast of the Mohicans'
72+
73+
str = removeFirstCodePoint( 'अनुच्छेद', 1 );
74+
// returns 'नुच्छेद'
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
82+
83+
<section class="related">
84+
85+
</section>
86+
87+
<!-- /.related -->
88+
89+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="links">
92+
93+
</section>
94+
95+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 removeFirst = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'beep boop',
38+
'foo bar',
39+
'xyz abc',
40+
'🐶🐮🐷🐰🐸'
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = removeFirst( values[ i%values.length ], 1 );
46+
if ( typeof out !== 'string' ) {
47+
b.fail( 'should return a string' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isString( out ) ) {
52+
b.fail( 'should return a string' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( str, n )
3+
Removes the first `n` Unicode code points of a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
n: integer
11+
Number of Unicode code points to remove.
12+
13+
Returns
14+
-------
15+
out: string
16+
Output string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'beep', 1 )
21+
'eep'
22+
> out = {{alias}}( 'Boop', 1 )
23+
'oop'
24+
> out = {{alias}}( 'foo bar', 5 )
25+
'ar'
26+
27+
See Also
28+
--------
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Removes the first `n` Unicode code points of a string.
23+
*
24+
* @param str - input string
25+
* @param n - number of code points to remove
26+
* @returns output string
27+
*
28+
* @example
29+
* var out = removeFirst( 'last man standing', 1 );
30+
* // returns 'ast man standing'
31+
*
32+
* @example
33+
* var out = removeFirst( 'presidential election', 1 );
34+
* // returns 'residential election'
35+
*
36+
* @example
37+
* var out = removeFirst( 'JavaScript', 1 );
38+
* // returns 'avaScript'
39+
*
40+
* @example
41+
* var out = removeFirst( 'Hidden Treasures', 1 );
42+
* // returns 'idden Treasures'
43+
*
44+
* @example
45+
* var out = removeFirst( 'foo bar', 5 );
46+
* // returns 'ar'
47+
*/
48+
declare function removeFirst( str: string, n: number ): string;
49+
50+
51+
// EXPORTS //
52+
53+
export = removeFirst;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
import removeFirst = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
removeFirst( 'abc', 1 ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
removeFirst( true, 1 ); // $ExpectError
32+
removeFirst( false, 1 ); // $ExpectError
33+
removeFirst( null, 1 ); // $ExpectError
34+
removeFirst( undefined, 1 ); // $ExpectError
35+
removeFirst( 5, 1 ); // $ExpectError
36+
removeFirst( [], 1 ); // $ExpectError
37+
removeFirst( {}, 1 ); // $ExpectError
38+
removeFirst( ( x: number ): number => x, 1 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is 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', 'abc' ); // $ExpectError
47+
removeFirst( 'abc', [] ); // $ExpectError
48+
removeFirst( 'abc', {} ); // $ExpectError
49+
removeFirst( 'abc', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
removeFirst(); // $ExpectError
55+
removeFirst( 'abc' ); // $ExpectError
56+
removeFirst( 'abc', 1, 2 ); // $ExpectError
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
var removeFirstCodePoint = require( './../lib' );
22+
23+
console.log( removeFirstCodePoint( 'presidential election', 1 ) );
24+
// => 'residential election'
25+
26+
console.log( removeFirstCodePoint( 'JavaScript', 1 ) );
27+
// => 'avaScript'
28+
29+
console.log( removeFirstCodePoint( 'The Last of the Mohicans', 5 ) );
30+
// => 'ast of the Mohicans'
31+
32+
console.log( removeFirstCodePoint( 'अनुच्छेद', 1 ) );
33+
// => 'नुच्छेद'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
/**
22+
* Remove the first `n` Unicode code points of a string.
23+
*
24+
* @module @stdlib/string/base/remove-first-code-point
25+
*
26+
* @example
27+
* var removeFirst = require( '@stdlib/string/base/remove-first-code-point' );
28+
*
29+
* var out = removeFirst( 'last man standing', 1 );
30+
* // returns 'ast man standing'
31+
*
32+
* out = removeFirst( 'Hidden Treasures', 1 );
33+
* // returns 'idden Treasures';
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)