Skip to content

Commit 5fd8af8

Browse files
headlessNodekgryte
andauthored
feat: add string/base/slice-grapheme-clusters
PR-URL: #5457 Closes: stdlib-js/metr-issue-tracker#35 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 955bcbe commit 5fd8af8

File tree

10 files changed

+750
-0
lines changed

10 files changed

+750
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# sliceGraphemeClusters
22+
23+
> Slice a string based on grapheme cluster (i.e., user-perceived character) indices.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var sliceGraphemeClusters = require( '@stdlib/string/base/slice-grapheme-clusters' );
41+
```
42+
43+
#### sliceGraphemeClusters( str, start, end )
44+
45+
Slices a string based on grapheme cluster (i.e., user-perceived character) indices.
46+
47+
```javascript
48+
var out = sliceGraphemeClusters( 'Hello World', 0, 5 );
49+
// returns 'Hello'
50+
51+
out = sliceGraphemeClusters( '👋👋👋', 0, 2 );
52+
// returns '👋👋'
53+
54+
out = sliceGraphemeClusters( '六书/六書', 1, 5 );
55+
// returns '书/六書'
56+
57+
out = sliceGraphemeClusters( '🌷🍕👉🏿', 1, 2 );
58+
// returns '🍕'
59+
```
60+
61+
The function accepts the following arguments:
62+
63+
- **str**: input string.
64+
- **start**: the `ith` grapheme cluster to start a slice (inclusive).
65+
- **end**: the `jth` grapheme cluster to end a slice (exclusive).
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
72+
73+
<section class="notes">
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<!-- Package usage examples. -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
```javascript
86+
var sliceGraphemeClusters = require( '@stdlib/string/base/slice-grapheme-clusters' );
87+
88+
console.log( sliceGraphemeClusters( 'Hello World', 0, 5 ) );
89+
// => 'Hello'
90+
91+
console.log( sliceGraphemeClusters( 'Hello World', -5, -1 ) );
92+
// => 'Worl'
93+
94+
console.log( sliceGraphemeClusters( '👋👋👋', 0, 2 ) );
95+
// => '👋👋'
96+
97+
console.log( sliceGraphemeClusters( '六书/六書', 1, 5 ) );
98+
// => '书/六書'
99+
100+
console.log( sliceGraphemeClusters( '👨‍👩‍👧‍👦👨‍👩‍👧‍👦👨‍👩‍👧‍👦', 0, 2 ) );
101+
// => '👨‍👩‍👧‍👦👨‍👩‍👧‍👦'
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
<!-- <related-links> -->
129+
130+
<!-- </related-links> -->
131+
132+
</section>
133+
134+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 sliceGraphemeClusters = 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+
'Iñtërnâtiônàlizætiøn',
38+
'presidential election',
39+
'🐶🐮🐷🐰🐸',
40+
'Hello 👋 World',
41+
'अनुच्छेद'
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = sliceGraphemeClusters( values[ i%values.length ], 1, 3 );
47+
if ( typeof out !== 'string' ) {
48+
b.fail( 'should return a string' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isString( out ) ) {
53+
b.fail( 'should return a string' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( str, start, end )
3+
Slices a string based on grapheme cluster (i.e., user-perceived character)
4+
indices.
5+
6+
Parameters
7+
----------
8+
str: string
9+
Input string.
10+
11+
start: integer
12+
The `ith` grapheme cluster to start a slice (inclusive).
13+
14+
end: integer
15+
The `jth` grapheme cluster to end a slice (exclusive).
16+
17+
Returns
18+
-------
19+
out: string
20+
Output string.
21+
22+
Examples
23+
--------
24+
> var out = {{alias}}( 'beep', 0, 2 )
25+
'be'
26+
> out = {{alias}}( 'Boop', 1, 3 )
27+
'oo'
28+
> out = {{alias}}( 'foo bar', 4, 7 )
29+
'bar'
30+
> out = {{alias}}( '🐶🐮🐷🐰🐸', 0, 2 )
31+
'🐶🐮'
32+
33+
See Also
34+
--------
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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: 4.1
20+
21+
/**
22+
* Slices a string based on grapheme cluster (i.e., user-perceived character) indices.
23+
*
24+
* @param str - input string
25+
* @param start - the `ith` grapheme cluster to start a slice (inclusive)
26+
* @param end - the `jth` grapheme cluster to end a slice (exclusive)
27+
* @returns output string
28+
*
29+
* @example
30+
* var out = sliceGraphemeClusters( 'Hello World', 0, 5 );
31+
* // returns 'Hello'
32+
*
33+
* out = sliceGraphemeClusters( '👋👋👋', 0, 2 );
34+
* // returns '👋👋'
35+
*
36+
* out = sliceGraphemeClusters( 'अनुच्छेद', 1, 3 );
37+
* // returns 'नुच्'
38+
*
39+
* out = sliceGraphemeClusters( 'Hello World', -5, -1 );
40+
* // returns 'Worl'
41+
*/
42+
declare function sliceGraphemeClusters( str: string, start: number, end: number ): string;
43+
44+
45+
// EXPORTS //
46+
47+
export = sliceGraphemeClusters;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 sliceGraphemeClusters = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
sliceGraphemeClusters( 'beep', 0, 2 ); // $ExpectType string
27+
sliceGraphemeClusters( 'Boop', 1, 3 ); // $ExpectType string
28+
sliceGraphemeClusters( 'foo bar', 4, 7 ); // $ExpectType string
29+
sliceGraphemeClusters( '🐶🐮🐷🐰🐸', 1, 3 ); // $ExpectType string
30+
sliceGraphemeClusters( '🐶🐮🐷🐰🐸', -3, -1 ); // $ExpectType string
31+
}
32+
33+
// The compiler throws an error if the function is provided a first argument that is not a string...
34+
{
35+
sliceGraphemeClusters( true, 1, 2 ); // $ExpectError
36+
sliceGraphemeClusters( false, 1, 2 ); // $ExpectError
37+
sliceGraphemeClusters( null, 1, 2 ); // $ExpectError
38+
sliceGraphemeClusters( undefined, 1, 2 ); // $ExpectError
39+
sliceGraphemeClusters( 5, 1, 2 ); // $ExpectError
40+
sliceGraphemeClusters( [], 1, 2 ); // $ExpectError
41+
sliceGraphemeClusters( {}, 1, 2 ); // $ExpectError
42+
sliceGraphemeClusters( ( x: number ): number => x, 1, 2 ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided a second argument that is not a number...
46+
{
47+
sliceGraphemeClusters( 'abc', true, 2 ); // $ExpectError
48+
sliceGraphemeClusters( 'abc', false, 2 ); // $ExpectError
49+
sliceGraphemeClusters( 'abc', null, 2 ); // $ExpectError
50+
sliceGraphemeClusters( 'abc', 'abc', 2 ); // $ExpectError
51+
sliceGraphemeClusters( 'abc', [], 2 ); // $ExpectError
52+
sliceGraphemeClusters( 'abc', {}, 2 ); // $ExpectError
53+
sliceGraphemeClusters( 'abc', ( x: number ): number => x, 2 ); // $ExpectError
54+
}
55+
56+
// The compiler throws an error if the function is provided a third argument that is not a number...
57+
{
58+
sliceGraphemeClusters( 'abc', 1, true ); // $ExpectError
59+
sliceGraphemeClusters( 'abc', 1, false ); // $ExpectError
60+
sliceGraphemeClusters( 'abc', 1, null ); // $ExpectError
61+
sliceGraphemeClusters( 'abc', 1, 'abc' ); // $ExpectError
62+
sliceGraphemeClusters( 'abc', 1, [] ); // $ExpectError
63+
sliceGraphemeClusters( 'abc', 1, {} ); // $ExpectError
64+
sliceGraphemeClusters( 'abc', 1, ( x: number ): number => x ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided an unsupported number of arguments...
68+
{
69+
sliceGraphemeClusters(); // $ExpectError
70+
sliceGraphemeClusters( 'abc' ); // $ExpectError
71+
sliceGraphemeClusters( 'abc', 1, 2, {} ); // $ExpectError
72+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 sliceGraphemeClusters = require( './../lib' );
22+
23+
console.log( sliceGraphemeClusters( 'Hello World', 0, 5 ) );
24+
// => 'Hello'
25+
26+
console.log( sliceGraphemeClusters( 'Hello World', -5, -1 ) );
27+
// => 'Worl'
28+
29+
console.log( sliceGraphemeClusters( '👋👋👋', 0, 2 ) );
30+
// => '👋👋'
31+
32+
console.log( sliceGraphemeClusters( '六书/六書', 1, 5 ) );
33+
// => '书/六書'
34+
35+
console.log( sliceGraphemeClusters( '👨‍👩‍👧‍👦👨‍👩‍👧‍👦👨‍👩‍👧‍👦', 0, 2 ) );
36+
// => '👨‍👩‍👧‍👦👨‍👩‍👧‍👦'

0 commit comments

Comments
 (0)