diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/README.md b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/README.md new file mode 100644 index 000000000000..2c15c7839f96 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/README.md @@ -0,0 +1,116 @@ + + +# hammingGraphemeClusters + +> Calculate the [Hamming distance][hamming-distance] between two strings, treating grapheme clusters as the unit of comparison + + + +
+ +## Usage + +```javascript +var hammingGraphemeClusters = require( '@stdlib/string/base/distances/hamming-grapheme-clusters' ); +``` + +#### hammingGraphemeClusters( s1, s2 ) + +Calculates the [Hamming distance][hamming-distance] between two strings, treating [grapheme clusters][grapheme-cluster] (as opposed to characters) as a single unit + +```javascript +var dist = hammingGraphemeClusters( 'café', 'cafe' ); +// returns 1 + +dist = hammingGraphemeClusters( '🍕🍕🍕', '❤🍕🍕' ); +// returns 1 + +dist = hammingGraphemeClusters( 'tooth', 'froth' ); +// returns 2 + +dist = hammingGraphemeClusters( '', '' ); +// returns 0 + +dist = hammingGraphemeClusters( 'Später', 'Spæter' ); +// returns 1 +``` + +
+ + + + + +
+ +## Notes + +- If the two strings differ in length, the [Hamming distance][hamming-distance] is not defined. Consequently, when provided two input strings of unequal number of grapheme units, the function returns a sentinel value of `-1`. +- As the function calculates the [Hamming distance][hamming-distance] by comparing grapheme clusters, when the character(s) are composed of multiple Unicode code points, it will treat the difference between the characters as a unit difference. + +
+ + + + + +
+ +## Examples + +```javascript +var hammingGraphemeClusters = require( '@stdlib/string/base/distances/hamming-grapheme-clusters' ); + +var dist = hammingGraphemeClusters( '🅰nimate', '🅰niⓂ🅰te' ); +// returns 2 + +dist = hammingGraphemeClusters( 'elephant', 'Tashkent' ); +// returns 6 + +dist = hammingGraphemeClusters( 'Résumé', 'Resume' ); +// returns 2 + +dist = hammingGraphemeClusters( '', '' ); +// returns 0 +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/benchmark/benchmark.js new file mode 100644 index 000000000000..af5a9dae4573 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/benchmark/benchmark.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 pkg = require( './../package.json' ).name; +var hammingGraphemeClusters = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var value; + var out; + var i; + + values = [ + [ 'algorithms', 'altruistic' ], + [ '😊😃😄😁', '😊😃😁😄' ], + [ '', '' ], + [ 'résumé', 'resume' ], + [ 'a🅰b', 'a🅰b' ], + [ '🌑🌒🌓🌔🌕', '..🌓🌔🌕' ], + [ 'प्रेम', 'विश्व' ], + [ 'fránçáis!', 'francais?' ], + [ '1️⃣2️⃣3️⃣4️⃣', '123️⃣4' ], + [ '🚗🚕🚙🚌🚎', '🚎🚌🚙🚕🚗' ], + [ '∑x²y', 'Σx^2' ], + [ 'congratulations', 'conmgeautlatins' ] + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + value = values[ i%values.length ]; + out = hammingGraphemeClusters( value[0], value[1] ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/repl.txt b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/repl.txt new file mode 100644 index 000000000000..813aa6bd32e2 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( s1, s2 ) + Calculates the Hamming distance between two input strings by + comparing their grapheme clusters + + The function returns a sentinel value of -1 if the input strings are + unequal in grapheme clusters count + + Parameters + ---------- + s1: string + First input string. + + s2: string + Second input string. + + Returns + ------- + out: number + Hamming distance. + + Examples + -------- + > var d = {{alias}}( 'Résumé', 'Resume' ) + 2 + > d = {{alias}}( '✔✔', '❌✔' ) + 1 + > d = {{alias}}( 'abc', '🅰🅰c' ) + 2 + > d = {{alias}}( 'El Niño', 'La Niña' ) + 3 + > d = {{alias}}( 'sacrifice', 'paradisal' ) + 8 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/types/index.d.ts new file mode 100644 index 000000000000..c6a1d7515d8c --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 + +/** +* Calculates the Hamming distance between two strings by comparing their grapheme clusters +* +* ## Notes +* +* - The function returns a sentinel value of `-1` if the input string lengths differ. +* +* @param str1 - first input string +* @param str2 - second input string +* @returns Hamming distance +* +* @example +* var dist = hammingGraphemeClusters( 'La Niña', 'El Niño' ); +* // returns 3 +* +* @example +* var dist = hammingGraphemeClusters( 'algorithms', 'altruistic' ); +* // returns 7 +* +* @example +* var dist = hammingGraphemeClusters( '✔✔', '✔❌' ); +* // returns 1 +*/ +declare function hammingGraphemeClusters( str1: string, str2: string ): number; + + +// EXPORTS // + +export = hammingGraphemeClusters; diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/types/test.ts new file mode 100644 index 000000000000..44c2a91ea76f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 hammingGraphemeClusters = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + hammingGraphemeClusters( '', '' ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a string... +{ + hammingGraphemeClusters( true, '' ); // $ExpectError + hammingGraphemeClusters( false, '' ); // $ExpectError + hammingGraphemeClusters( null, '' ); // $ExpectError + hammingGraphemeClusters( undefined, '' ); // $ExpectError + hammingGraphemeClusters( 5, '' ); // $ExpectError + hammingGraphemeClusters( [], '' ); // $ExpectError + hammingGraphemeClusters( {}, '' ); // $ExpectError + hammingGraphemeClusters( ( x: number ): number => x, '' ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a string... +{ + hammingGraphemeClusters( '', true ); // $ExpectError + hammingGraphemeClusters( '', false ); // $ExpectError + hammingGraphemeClusters( '', null ); // $ExpectError + hammingGraphemeClusters( '', undefined ); // $ExpectError + hammingGraphemeClusters( '', 5 ); // $ExpectError + hammingGraphemeClusters( '', [] ); // $ExpectError + hammingGraphemeClusters( '', {} ); // $ExpectError + hammingGraphemeClusters( '', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + hammingGraphemeClusters(); // $ExpectError + hammingGraphemeClusters( '' ); // $ExpectError + hammingGraphemeClusters( '', '', 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/examples/index.js b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/examples/index.js new file mode 100644 index 000000000000..fc6444823c79 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 hammingGraphemeClusters = require( './../lib' ); + +console.log( hammingGraphemeClusters( 'algorithms', 'altruistic' ) ); +// => 7 + +console.log( hammingGraphemeClusters( '😊😃😄😁', '😊😃😁😄' ) ); +// => 2 + +console.log( hammingGraphemeClusters( '', '' ) ); +// => 0 + +console.log( hammingGraphemeClusters( 'résumé', 'resume' ) ); +// => 2 + +console.log( hammingGraphemeClusters( 'a🅰b', 'a🅰b' ) ); +// => 0 + +console.log( hammingGraphemeClusters( '🌑🌒🌓🌔🌕', '..🌓🌔🌕' ) ); +// => 2 + +console.log( hammingGraphemeClusters( 'प्रेम', 'विश्व' ) ); +// => 3 + +console.log( hammingGraphemeClusters( 'fránçáis!', 'francais?' ) ); +// => 4 + +console.log( hammingGraphemeClusters( '1️⃣2️⃣3️⃣4️⃣', '123️⃣4' ) ); +// => 3 + +console.log( hammingGraphemeClusters( '🚗🚕🚙🚌🚎', '🚎🚌🚙🚕🚗' ) ); +// => 4 + +console.log( hammingGraphemeClusters( 'x²y', 'x^2' ) ); +// => 2 + +console.log( hammingGraphemeClusters( 'hola', 'hold' ) ); +// => 1 diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/lib/index.js b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/lib/index.js new file mode 100644 index 000000000000..88e05f2542f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +/** +* Calculate the Hamming distance between two strings with equal number of grapheme clusters +* +* @module @stdlib/string/base/distances/hamming-grapheme-clusters +* +* @example +* var hammingGraphemeClusters = require( '@stdlib/string/base/distances/hamming-grapheme-clusters' ); +* +* var dist = hammingGraphemeClusters( 'प्रेम', 'विश्व' ); +* // returns 2 +* +* dist = hammingGraphemeClusters( 'fránçáis!', 'francais?' ); +* // returns 4 +* +* dist = hammingDistance( 'javascript', 'typescript' ); +* // returns 4 +*/ + + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/lib/main.js b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/lib/main.js new file mode 100644 index 000000000000..690b61f3ee3e --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/lib/main.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var format = require( '@stdlib/string/format' ); +var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' ); + + +// MAIN // + +/** +* Calculates the Hamming distance between two strings with equal number of grapheme clusters. +* +* ## Notes +* +* - The function returns a sentinel value of `-1` if the input string differ in number of grapheme clusters. +* +* @param {string} s1 - first input string +* @param {string} s2 - second input string +* @throws {TypeError} first argument must be a string +* @throws {TypeError} second argument must be a string +* @returns {integer} Hamming distance +* +* @example +* var distance = hammingGraphemeClusters( 'cafe', 'café' ); +* // returns 1 +*/ +function hammingGraphemeClusters( s1, s2 ) { + var iend; + var jend; + var out; + var i; + var j; + + if ( !isString( s1 ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', s1 ) ); + } + if ( !isString( s2 ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', s2 ) ); + } + + out = 0; + i = 0; + j = 0; + iend = nextGraphemeClusterBreak(s1, i); + jend = nextGraphemeClusterBreak(s2, j); + while ( iend !== -1 ) { + if ( s1.substring(i, iend) !== s2.substring(j, jend) ) { + out += 1; + } + i = iend; + j = jend; + iend = nextGraphemeClusterBreak(s1, i); + jend = nextGraphemeClusterBreak(s2, j); + } + if (jend !== -1) { // s1 and s2 have unequal number of grapheme clusters + return -1; + } + if ( i < s1.length && s1.substring(i) !== s2.substring(j) ) { + out += 1; + } + return out; +} + + +// EXPORTS // + +module.exports = hammingGraphemeClusters; diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/package.json b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/package.json new file mode 100644 index 000000000000..0d278ff6d306 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/string/base/distances/hamming-grapheme-clusters", + "version": "0.0.0", + "description": "Calculate the Hamming distance between two strings, treating grapheme clusters as the unit of comparison", + "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", + "base", + "string", + "str", + "distances", + "distance", + "hamming", + "edit" + ], + "__stdlib__": {} + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/test/test.js b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/test/test.js new file mode 100644 index 000000000000..e65e408c519d --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/distances/hamming-grapheme-clusters/test/test.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 hammingGraphemeClusters = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof hammingGraphemeClusters, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a string as its first argument', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + hammingGraphemeClusters( value, 'foo' ); + }; + } +}); + +tape( 'the function throws an error if not provided a string as its second argument', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + hammingGraphemeClusters( 'foo', value ); + }; + } +}); + +tape( 'the function returns -1 as a sentinel value if provided strings of unequal number of grapheme clusters', function test( t ) { + t.strictEqual( hammingGraphemeClusters( 'length', 'differs' ), -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the function calculates the Hamming distance between two strings with equal number of grapheme clusters', function test( t ) { + var expected; + var values; + var i; + + values = [ + [ '1638452297', '4444884442' ], // 10 + [ '', '' ], // 0 + [ 'a', 'a' ], // 0 + [ 'a', '🅰' ], // 1 + [ 'xy', 'xy' ], // 0 + [ '✔✔', '❌✔' ], // 1 + [ 'Résumé', 'Resume' ], // 2 + [ 'Déjà vu', 'Deja vu' ], // 2 + [ 'Naive', 'Naïve' ], // 1 + [ 'El Niño', 'La Niña' ] // 3 + ]; + + expected = [ 10, 0, 0, 1, 0, 1, 2, 2, 1, 3 ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( hammingGraphemeClusters( values[i][0], values[i][1] ), expected[i], 'returns expected value' ); + } + t.end(); +});