diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/README.md b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/README.md new file mode 100644 index 000000000000..d236fdf720ae --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/README.md @@ -0,0 +1,134 @@ + + +# sliceGraphemeClusters + +> Slice a string based on grapheme cluster (i.e., user-perceived character) indices. + + + +
+ +
+ + + + + +
+ +## 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). + +
+ + + + + +
+ +
+ + + + + +
+ +## 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 ) ); +// => '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ' +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/benchmark/benchmark.js new file mode 100644 index 000000000000..9e9c7cec8419 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/repl.txt b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/repl.txt new file mode 100644 index 000000000000..58dca427a3df --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/repl.txt @@ -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 + -------- diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/index.d.ts new file mode 100644 index 000000000000..541305ad60ab --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/index.d.ts @@ -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; diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/test.ts new file mode 100644 index 000000000000..45ee98156ef9 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/test.ts @@ -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 +} diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/examples/index.js b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/examples/index.js new file mode 100644 index 000000000000..c3d88355e9fa --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/examples/index.js @@ -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 ) ); +// => '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ' diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/index.js b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/index.js new file mode 100644 index 000000000000..d33d427481a8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/index.js @@ -0,0 +1,49 @@ +/** +* @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'; + +/** +* Slice a string based on grapheme cluster (i.e., user-perceived character) indices. +* +* @module @stdlib/string/base/slice-grapheme-clusters +* +* @example +* var sliceGraphemeClusters = require( '@stdlib/string/base/slice-grapheme-clusters' ); +* +* 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' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/main.js new file mode 100644 index 000000000000..02956908637a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/main.js @@ -0,0 +1,97 @@ +/** +* @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 nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' ); +var numGraphemeClusters = require( '@stdlib/string/num-grapheme-clusters' ); +var max = require( '@stdlib/math/base/special/fast/max' ); + + +// MAIN // + +/** +* Slices a string based on grapheme cluster (i.e., user-perceived character) indices. +* +* @param {string} str - input string +* @param {integer} start - the `ith` grapheme cluster to start a slice (inclusive) +* @param {integer} end - the `jth` grapheme cluster to end a slice (exclusive) +* @returns {string} 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' +*/ +function sliceGraphemeClusters( str, start, end ) { + var numClusters; + var result; + var idx; + var brk; + var i; + + if ( str === '' ) { + return ''; + } + numClusters = numGraphemeClusters( str ); + if ( start < 0 ) { + start = max( start + numClusters, 0 ); + } + if ( end < 0 ) { + end = max( end + numClusters, 0 ); + } + if ( start >= numClusters || start >= end ) { + return ''; + } + if ( end > numClusters ) { + end = numClusters; + } + result = ''; + idx = 0; + i = 0; + while ( idx < str.length ) { + brk = nextGraphemeClusterBreak( str, idx ); + if ( brk === -1 ) { + brk = str.length; + } + if ( i >= start && i < end ) { + result += str.substring( idx, brk ); + } + idx = brk; + i += 1; + if ( i >= end ) { + break; + } + } + return result; +} + + +// EXPORTS // + +module.exports = sliceGraphemeClusters; diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/package.json b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/package.json new file mode 100644 index 000000000000..24f482d0fee6 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/string/base/slice-grapheme-clusters", + "version": "0.0.0", + "description": "Slice a string based on grapheme cluster (i.e., user-perceived character) indices.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "slice", + "substring", + "substr", + "unicode", + "grapheme", + "emojis" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/test/test.js b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/test/test.js new file mode 100644 index 000000000000..f93167a085c9 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/test/test.js @@ -0,0 +1,157 @@ +/** +* @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 tape = require( 'tape' ); +var sliceGraphemeClusters = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sliceGraphemeClusters, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( sliceGraphemeClusters.length, 3, 'the function has an arity of 3' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided an empty input string', function test( t ) { + var out; + + out = sliceGraphemeClusters( '', 0, 1 ); + t.strictEqual( out, '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns an empty string if the starting index is greater than or equal to the ending index', function test( t ) { + var out; + + out = sliceGraphemeClusters( 'hello', 2, 2 ); + t.strictEqual( out, '', 'returns expected value' ); + + out = sliceGraphemeClusters( 'hello', 3, 2 ); + t.strictEqual( out, '', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty string if the starting index is greater than or equal to the string length', function test( t ) { + var out; + + out = sliceGraphemeClusters( 'hello', 5, 6 ); + t.strictEqual( out, '', 'returns expected value' ); + + out = sliceGraphemeClusters( 'hello', 10, 12 ); + t.strictEqual( out, '', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function slices an input string based on grapheme cluster indices', function test( t ) { + var out; + + out = sliceGraphemeClusters( 'hello', 0, 3 ); + t.strictEqual( out, 'hel', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐ŸŒท๐Ÿ•๐Ÿ‘‰๐Ÿฟ', 1, 2 ); + t.strictEqual( out, '๐Ÿ•', 'returns expected value' ); + + out = sliceGraphemeClusters( 'เค…เคจเฅเคšเฅเค›เฅ‡เคฆ', 1, 5 ); + t.strictEqual( out, 'เคจเฅเคšเฅเค›เฅ‡เคฆ', 'returns expected value' ); + + out = sliceGraphemeClusters( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', 1, 5 ); + t.strictEqual( out, 'ไนฆ/ๅ…ญๆ›ธ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ๏ธ๐ŸŒท', 1, 2 ); + t.strictEqual( out, '๐ŸŒท', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function slices an input string based on grapheme cluster indices (skin-tone emojis)', function test( t ) { + var out; + + out = sliceGraphemeClusters( '๐ŸŒท๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘‰๐Ÿฟ', 1, 2 ); + t.strictEqual( out, '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ๏ธ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 1, 2 ); + t.strictEqual( out, '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ‘‹๐Ÿพ๐Ÿคฆ๐Ÿฝโ€โ™€๏ธ๐Ÿง‘๐Ÿฟ', 1, 2 ); + t.strictEqual( out, '๐Ÿคฆ๐Ÿฝโ€โ™€๏ธ', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing negative indices', function test( t ) { + var out; + + out = sliceGraphemeClusters( 'hello', -5, -2 ); + t.strictEqual( out, 'hel', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐ŸŒท๐Ÿ•๐Ÿ‘‰๐Ÿฟ', -2, -1 ); + t.strictEqual( out, '๐Ÿ•', 'returns expected value' ); + + out = sliceGraphemeClusters( 'เค…เคจเฅเคšเฅเค›เฅ‡เคฆ', -4, -1 ); + t.strictEqual( out, 'เคจเฅเคšเฅเค›เฅ‡', 'returns expected value' ); + + out = sliceGraphemeClusters( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', -3, 5 ); + t.strictEqual( out, '/ๅ…ญๆ›ธ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ๏ธ๐ŸŒท', -1, 2 ); + t.strictEqual( out, '๐ŸŒท', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing negative indices (skin-tone emojis)', function test( t ) { + var out; + + out = sliceGraphemeClusters( '๐ŸŒท๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘‰๐Ÿฟ', -2, -1 ); + t.strictEqual( out, '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ๏ธ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', -1, 4 ); + t.strictEqual( out, '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ‘‹๐Ÿพ๐Ÿคฆ๐Ÿฝโ€โ™€๏ธ๐Ÿง‘๐Ÿฟโ€๐Ÿฆฑ', -2, -1 ); + t.strictEqual( out, '๐Ÿคฆ๐Ÿฝโ€โ™€๏ธ', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates the end index to the string length', function test( t ) { + var out; + + out = sliceGraphemeClusters( 'hello', 0, 10 ); + t.strictEqual( out, 'hello', 'returns expected value' ); + + out = sliceGraphemeClusters( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', 1, 10 ); + t.strictEqual( out, 'ไนฆ/ๅ…ญๆ›ธ', 'returns expected value' ); + + out = sliceGraphemeClusters( '๐Ÿ๏ธ๐ŸŒท', 0, 5 ); + t.strictEqual( out, '๐Ÿ๏ธ๐ŸŒท', 'returns expected value' ); + + t.end(); +});