Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/README.md
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 -->
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();
});
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
--------
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 'Worl'
*/
declare function sliceGraphemeClusters( str: string, start: number, end: number ): string;


// EXPORTS //

export = sliceGraphemeClusters;
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
}
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 ) );
// => '👨‍👩‍👧‍👦👨‍👩‍👧‍👦'
Loading