Skip to content

Commit 982de75

Browse files
steff456kgryte
andauthored
feat: refactor string/remove-last
This commit adds 3 new base string packages for removing code units, code points, and grapheme clusters, respectively. This commit subsequently refactors `string/remove-last` to depend on those base packages. As a consequence, a new option has been added to `string/remove-last` to select which processing "mode" is desired in order to balance performance considerations. Additionally, this commit fixes a bug in `string/remove-first` due to an off-by-one indexing error. Lastly, this commit fixes the `name` field in `string/base/remove-first*` `package.json` files. PR-URL: #1079 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Ref: #1062
1 parent 4d7c4c3 commit 982de75

File tree

47 files changed

+2506
-66
lines changed

Some content is hidden

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

47 files changed

+2506
-66
lines changed

lib/node_modules/@stdlib/string/base/remove-first-code-point/lib/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function removeFirst( str, n ) {
8585
break;
8686
}
8787
}
88-
return str.substring( cnt, str.length );
88+
return str.substring( i + 1, str.length );
8989
}
9090

9191

lib/node_modules/@stdlib/string/base/remove-first-code-point/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@stdlib/string/base/first-code-point",
2+
"name": "@stdlib/string/base/remove-first-code-point",
33
"version": "0.0.0",
44
"description": "Remove the first Unicode code point of a string.",
55
"license": "Apache-2.0",

lib/node_modules/@stdlib/string/base/remove-first-code-point/test/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ tape( 'the function removes the first Unicode code point of a provided string (U
6868
out = removeFirst( '六书/六書', 1 );
6969
t.strictEqual( out, '书/六書', 'returns expected value' );
7070

71+
out = removeFirst( '𐒻𐓟', 1 );
72+
t.strictEqual( out, '𐓟', 'returns expected value' );
73+
74+
out = removeFirst( '\uD800', 1 );
75+
t.strictEqual( out, '', 'returns expected value' );
76+
7177
t.end();
7278
});
7379

@@ -92,5 +98,8 @@ tape( 'the function supports removing the first `n` Unicode code points of a pro
9298
out = removeFirst( '六书/六書', 3 );
9399
t.strictEqual( out, '六書', 'returns expected value' );
94100

101+
out = removeFirst( '𐓟𐒻𐓟', 2 );
102+
t.strictEqual( out, '𐓟', 'returns expected value' );
103+
95104
t.end();
96105
});

lib/node_modules/@stdlib/string/base/remove-first-grapheme-cluster/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@stdlib/string/base/first-grapheme-cluster",
2+
"name": "@stdlib/string/base/remove-first-grapheme-cluster",
33
"version": "0.0.0",
44
"description": "Remove the first grapheme cluster (i.e., user-perceived character) of a string.",
55
"license": "Apache-2.0",

lib/node_modules/@stdlib/string/base/remove-first/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@stdlib/string/base/first",
2+
"name": "@stdlib/string/base/remove-first",
33
"version": "0.0.0",
44
"description": "Remove the first UTF-16 code unit of a string.",
55
"license": "Apache-2.0",
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+
# removeLastCodePoint
22+
23+
> Remove the last `n` Unicode code points of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var removeLastCodePoint = require( '@stdlib/string/base/remove-last-code-point' );
31+
```
32+
33+
#### removeLastCodePoint( str, n )
34+
35+
Removes the last `n` Unicode code points of a string.
36+
37+
```javascript
38+
var out = removeLastCodePoint( 'last man standing', 1 );
39+
// returns 'last man standin'
40+
41+
out = removeLastCodePoint( 'Hidden Treasures', 1 );
42+
// returns 'Hidden Treasure'
43+
44+
out = removeLastCodePoint( 'foo bar', 5 );
45+
// returns 'fo'
46+
47+
out = removeLastCodePoint( '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 removeLastCodePoint = require( '@stdlib/string/base/remove-last-code-point' );
63+
64+
var str = removeLastCodePoint( 'presidential election', 1 );
65+
// returns 'presidential electio'
66+
67+
str = removeLastCodePoint( 'JavaScript', 1 );
68+
// returns 'JavaScrip'
69+
70+
str = removeLastCodePoint( 'The Last of the Mohicans', 5 );
71+
// returns 'The Last of the Moh'
72+
73+
str = removeLastCodePoint( 'अनुच्छेद', 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 removeLast = 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 = removeLast( 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 last `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+
'bee'
22+
> out = {{alias}}( 'Boop', 1 )
23+
'Boo'
24+
> out = {{alias}}( 'foo bar', 5 )
25+
'fo'
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 last `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 = removeLast( 'last man standing', 1 );
30+
* // returns 'last man standin'
31+
*
32+
* @example
33+
* var out = removeLast( 'presidential election', 1 );
34+
* // returns 'presidential electio'
35+
*
36+
* @example
37+
* var out = removeLast( 'JavaScript', 1 );
38+
* // returns 'JavaScrip'
39+
*
40+
* @example
41+
* var out = removeLast( 'Hidden Treasures', 1 );
42+
* // returns 'Hidden Treasure'
43+
*
44+
* @example
45+
* var out = removeLast( 'foo bar', 5 );
46+
* // returns 'fo'
47+
*/
48+
declare function removeLast( str: string, n: number ): string;
49+
50+
51+
// EXPORTS //
52+
53+
export = removeLast;
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 removeLast = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
removeLast( '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+
removeLast( true, 1 ); // $ExpectError
32+
removeLast( false, 1 ); // $ExpectError
33+
removeLast( null, 1 ); // $ExpectError
34+
removeLast( undefined, 1 ); // $ExpectError
35+
removeLast( 5, 1 ); // $ExpectError
36+
removeLast( [], 1 ); // $ExpectError
37+
removeLast( {}, 1 ); // $ExpectError
38+
removeLast( ( 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+
removeLast( 'abc', true ); // $ExpectError
44+
removeLast( 'abc', false ); // $ExpectError
45+
removeLast( 'abc', null ); // $ExpectError
46+
removeLast( 'abc', 'abc' ); // $ExpectError
47+
removeLast( 'abc', [] ); // $ExpectError
48+
removeLast( 'abc', {} ); // $ExpectError
49+
removeLast( '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+
removeLast(); // $ExpectError
55+
removeLast( 'abc' ); // $ExpectError
56+
removeLast( '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 removeLastCodePoint = require( './../lib' );
22+
23+
console.log( removeLastCodePoint( 'presidential election', 1 ) );
24+
// => 'presidential electio'
25+
26+
console.log( removeLastCodePoint( 'JavaScript', 1 ) );
27+
// => 'JavaScrip'
28+
29+
console.log( removeLastCodePoint( 'The Last of the Mohicans', 5 ) );
30+
// => 'The Last of the Moh'
31+
32+
console.log( removeLastCodePoint( 'अनुच्छेद', 1 ) );
33+
// => 'अनुच्छे'

0 commit comments

Comments
 (0)