-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add string/base/slice-grapheme-clusters
#5457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d8003de
feat: add string/base/slice-grapheme-clusters
headlessNode 77bf03d
refactor: add args docs, fix repl, remove input validation
headlessNode fa73af6
refactor: use other dep
kgryte f810453
refactor: apply suggestions from code review
headlessNode 14a1679
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# sliceGraphemeClusters | ||
|
||
> Slice a string based on grapheme cluster (i.e., user-perceived character) indices. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var sliceGraphemeClusters = require( '@stdlib/string/base/slice-grapheme-clusters' ); | ||
``` | ||
|
||
#### sliceGraphemeClusters( str, start, end ) | ||
|
||
Slices a string based on grapheme cluster (i.e., user-perceived character) indices. | ||
|
||
```javascript | ||
var out = sliceGraphemeClusters( 'Hello World', 0, 5 ); | ||
// returns 'Hello' | ||
|
||
out = sliceGraphemeClusters( '👋👋👋', 0, 2 ); | ||
// returns '👋👋' | ||
|
||
out = sliceGraphemeClusters( '六书/六書', 1, 5 ); | ||
// returns '书/六書' | ||
|
||
out = sliceGraphemeClusters( '🌷🍕👉🏿', 1, 2 ); | ||
// returns '🍕' | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **str**: input string. | ||
- **start**: the `ith` grapheme cluster to start a slice (inclusive). | ||
- **end**: the `jth` grapheme cluster to end a slice (exclusive). | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
```javascript | ||
var sliceGraphemeClusters = require( '@stdlib/string/base/slice-grapheme-clusters' ); | ||
|
||
console.log( sliceGraphemeClusters( 'Hello World', 0, 5 ) ); | ||
// => 'Hello' | ||
|
||
console.log( sliceGraphemeClusters( 'Hello World', -5, -1 ) ); | ||
// => 'Worl' | ||
|
||
console.log( sliceGraphemeClusters( '👋👋👋', 0, 2 ) ); | ||
// => '👋👋' | ||
|
||
console.log( sliceGraphemeClusters( '六书/六書', 1, 5 ) ); | ||
// => '书/六書' | ||
|
||
console.log( sliceGraphemeClusters( '👨👩👧👦👨👩👧👦👨👩👧👦', 0, 2 ) ); | ||
// => '👨👩👧👦👨👩👧👦' | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- 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. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
57 changes: 57 additions & 0 deletions
57
lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var sliceGraphemeClusters = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var values; | ||
var out; | ||
var i; | ||
|
||
values = [ | ||
'Iñtërnâtiônàlizætiøn', | ||
'presidential election', | ||
'🐶🐮🐷🐰🐸', | ||
'Hello 👋 World', | ||
'अनुच्छेद' | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = sliceGraphemeClusters( values[ i%values.length ], 1, 3 ); | ||
if ( typeof out !== 'string' ) { | ||
b.fail( 'should return a string' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isString( out ) ) { | ||
b.fail( 'should return a string' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
34 changes: 34 additions & 0 deletions
34
lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
{{alias}}( str, start, end ) | ||
Slices a string based on grapheme cluster (i.e., user-perceived character) | ||
indices. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
start: integer | ||
The `ith` grapheme cluster to start a slice (inclusive). | ||
|
||
end: integer | ||
The `jth` grapheme cluster to end a slice (exclusive). | ||
|
||
Returns | ||
------- | ||
out: string | ||
Output string. | ||
|
||
Examples | ||
-------- | ||
> var out = {{alias}}( 'beep', 0, 2 ) | ||
'be' | ||
> out = {{alias}}( 'Boop', 1, 3 ) | ||
'oo' | ||
> out = {{alias}}( 'foo bar', 4, 7 ) | ||
'bar' | ||
> out = {{alias}}( '🐶🐮🐷🐰🐸', 0, 2 ) | ||
'🐶🐮' | ||
|
||
See Also | ||
-------- |
47 changes: 47 additions & 0 deletions
47
lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Slices a string based on grapheme cluster (i.e., user-perceived character) indices. | ||
* | ||
* @param str - input string | ||
* @param start - the `ith` grapheme cluster to start a slice (inclusive) | ||
* @param end - the `jth` grapheme cluster to end a slice (exclusive) | ||
* @returns output string | ||
* | ||
* @example | ||
* var out = sliceGraphemeClusters( 'Hello World', 0, 5 ); | ||
* // returns 'Hello' | ||
* | ||
* out = sliceGraphemeClusters( '👋👋👋', 0, 2 ); | ||
* // returns '👋👋' | ||
* | ||
* out = sliceGraphemeClusters( 'अनुच्छेद', 1, 3 ); | ||
* // returns 'नु' | ||
* | ||
* out = sliceGraphemeClusters( 'Hello World', -5, -1 ); | ||
* // returns 'World' | ||
*/ | ||
declare function sliceGraphemeClusters( str: string, start: number, end: number ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = sliceGraphemeClusters; |
72 changes: 72 additions & 0 deletions
72
lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import sliceGraphemeClusters = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a string... | ||
{ | ||
sliceGraphemeClusters( 'beep', 0, 2 ); // $ExpectType string | ||
sliceGraphemeClusters( 'Boop', 1, 3 ); // $ExpectType string | ||
sliceGraphemeClusters( 'foo bar', 4, 7 ); // $ExpectType string | ||
sliceGraphemeClusters( '🐶🐮🐷🐰🐸', 1, 3 ); // $ExpectType string | ||
sliceGraphemeClusters( '🐶🐮🐷🐰🐸', -3, -1 ); // $ExpectType string | ||
} | ||
|
||
// The compiler throws an error if the function is provided a first argument that is not a string... | ||
{ | ||
sliceGraphemeClusters( true, 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( false, 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( null, 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( undefined, 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( 5, 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( [], 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( {}, 1, 2 ); // $ExpectError | ||
sliceGraphemeClusters( ( x: number ): number => x, 1, 2 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a second argument that is not a number... | ||
{ | ||
sliceGraphemeClusters( 'abc', true, 2 ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', false, 2 ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', null, 2 ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 'abc', 2 ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', [], 2 ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', {}, 2 ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', ( x: number ): number => x, 2 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a third argument that is not a number... | ||
{ | ||
sliceGraphemeClusters( 'abc', 1, true ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, false ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, null ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, 'abc' ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, [] ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, {} ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
sliceGraphemeClusters(); // $ExpectError | ||
sliceGraphemeClusters( 'abc' ); // $ExpectError | ||
sliceGraphemeClusters( 'abc', 1, 2, {} ); // $ExpectError | ||
} |
36 changes: 36 additions & 0 deletions
36
lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var sliceGraphemeClusters = require( './../lib' ); | ||
|
||
console.log( sliceGraphemeClusters( 'Hello World', 0, 5 ) ); | ||
// => 'Hello' | ||
|
||
console.log( sliceGraphemeClusters( 'Hello World', -5, -1 ) ); | ||
// => 'Worl' | ||
|
||
console.log( sliceGraphemeClusters( '👋👋👋', 0, 2 ) ); | ||
// => '👋👋' | ||
|
||
console.log( sliceGraphemeClusters( '六书/六書', 1, 5 ) ); | ||
// => '书/六書' | ||
|
||
console.log( sliceGraphemeClusters( '👨👩👧👦👨👩👧👦👨👩👧👦', 0, 2 ) ); | ||
// => '👨👩👧👦👨👩👧👦' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.