pax_global_header00006660000000000000000000000064141044670270014517gustar00rootroot0000000000000052 comment=447058af4df87e45724ad608c8892eb263aa1276 leven-4.0.0/000077500000000000000000000000001410446702700126315ustar00rootroot00000000000000leven-4.0.0/.editorconfig000066400000000000000000000002571410446702700153120ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 leven-4.0.0/.gitattributes000066400000000000000000000000231410446702700155170ustar00rootroot00000000000000* text=auto eol=lf leven-4.0.0/.github/000077500000000000000000000000001410446702700141715ustar00rootroot00000000000000leven-4.0.0/.github/funding.yml000066400000000000000000000001571410446702700163510ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/leven custom: https://sindresorhus.com/donate leven-4.0.0/.github/security.md000066400000000000000000000002631410446702700163630ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. leven-4.0.0/.github/workflows/000077500000000000000000000000001410446702700162265ustar00rootroot00000000000000leven-4.0.0/.github/workflows/main.yml000066400000000000000000000006261410446702700177010ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test leven-4.0.0/.gitignore000066400000000000000000000000271410446702700146200ustar00rootroot00000000000000node_modules yarn.lock leven-4.0.0/.npmrc000066400000000000000000000000231410446702700137440ustar00rootroot00000000000000package-lock=false leven-4.0.0/bench.js000066400000000000000000000053041410446702700142500ustar00rootroot00000000000000/* globals bench suite */ import levenshteinEditDistance from 'levenshtein-edit-distance'; import {get as fastLevenshtein} from 'fast-levenshtein'; import levenshteinComponent from 'levenshtein-component'; import {computeDistance as ld} from 'ld'; import levdist from 'levdist'; import {LevenshteinDistance as natural} from 'natural'; import levenshtein from 'levenshtein'; import talisman from 'talisman/metrics/distance/levenshtein'; import leven from './index.js'; function run(function_) { function_('a', 'b'); function_('ab', 'ac'); function_('ac', 'bc'); function_('abc', 'axc'); function_('kitten', 'sitting'); function_('xabxcdxxefxgx', '1ab2cd34ef5g6'); function_('cat', 'cow'); function_('xabxcdxxefxgx', 'abcdefg'); function_('javawasneat', 'scalaisgreat'); function_('example', 'samples'); function_('sturgeon', 'urgently'); function_('levenshtein', 'frankenstein'); function_('distance', 'difference'); function_('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'); function_('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.'); } suite('leven', () => { bench('leven', () => { run(leven); }); bench('talisman', () => { run(talisman); }); bench('levenshtein-edit-distance', () => { run(levenshteinEditDistance); }); bench('fast-levenshtein', () => { run(fastLevenshtein); }); bench('levenshtein-component', () => { run(levenshteinComponent); }); bench('ld', () => { run(ld); }); bench('levenshtein', () => { run(levenshtein); }); bench('levdist', () => { run(levdist); }); bench('natural', () => { run(natural); }); }); leven-4.0.0/index.d.ts000066400000000000000000000003031410446702700145260ustar00rootroot00000000000000/** Measure the difference between two strings. @example ``` import leven from 'leven'; leven('cat', 'cow'); //=> 2 ``` */ export default function leven(first: string, second: string): number; leven-4.0.0/index.js000066400000000000000000000035121410446702700142770ustar00rootroot00000000000000const array = []; const characterCodeCache = []; export default function leven(first, second) { if (first === second) { return 0; } const swap = first; // Swapping the strings if `a` is longer than `b` so we know which one is the // shortest & which one is the longest if (first.length > second.length) { first = second; second = swap; } let firstLength = first.length; let secondLength = second.length; // Performing suffix trimming: // We can linearly drop suffix common to both strings since they // don't increase distance at all // Note: `~-` is the bitwise way to perform a `- 1` operation while (firstLength > 0 && (first.charCodeAt(~-firstLength) === second.charCodeAt(~-secondLength))) { firstLength--; secondLength--; } // Performing prefix trimming // We can linearly drop prefix common to both strings since they // don't increase distance at all let start = 0; while (start < firstLength && (first.charCodeAt(start) === second.charCodeAt(start))) { start++; } firstLength -= start; secondLength -= start; if (firstLength === 0) { return secondLength; } let bCharacterCode; let result; let temporary; let temporary2; let index = 0; let index2 = 0; while (index < firstLength) { characterCodeCache[index] = first.charCodeAt(start + index); array[index] = ++index; } while (index2 < secondLength) { bCharacterCode = second.charCodeAt(start + index2); temporary = index2++; result = index2; for (index = 0; index < firstLength; index++) { temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1; temporary = array[index]; // eslint-disable-next-line no-multi-assign result = array[index] = temporary > result ? (temporary2 > result ? result + 1 : temporary2) : (temporary2 > temporary ? temporary + 1 : temporary2); } } return result; } leven-4.0.0/index.test-d.ts000066400000000000000000000001611410446702700155050ustar00rootroot00000000000000import {expectType} from 'tsd'; import leven from './index.js'; expectType(leven('kitten', 'sitting')); leven-4.0.0/license000066400000000000000000000021351410446702700141770ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. leven-4.0.0/package.json000066400000000000000000000022251410446702700151200ustar00rootroot00000000000000{ "name": "leven", "version": "4.0.0", "description": "Measure the difference between two strings using the Levenshtein distance algorithm", "license": "MIT", "repository": "sindresorhus/leven", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd", "bench": "matcha bench.js" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "leven", "levenshtein", "distance", "algorithm", "string", "difference", "diff", "fast", "fuzzy", "similar", "similarity", "compare", "comparison", "edit", "text", "match", "matching" ], "devDependencies": { "ava": "^3.15.0", "fast-levenshtein": "^3.0.0", "ld": "^0.1.0", "levdist": "^2.2.10", "levenshtein": "^1.0.5", "levenshtein-component": "^0.0.1", "levenshtein-edit-distance": "^3.0.0", "matcha": "^0.7.0", "natural": "^5.0.4", "talisman": "^1.1.4", "tsd": "^0.17.0", "xo": "^0.44.0" } } leven-4.0.0/readme.md000066400000000000000000000014231410446702700144100ustar00rootroot00000000000000# leven > Measure the difference between two strings using the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm ## Install ``` $ npm install leven ``` ## Usage ```js import leven from 'leven'; leven('cat', 'cow'); //=> 2 ``` ## Related - [leven-cli](https://github.com/sindresorhus/leven-cli) - CLI for this module ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
leven-4.0.0/test.js000066400000000000000000000012621410446702700141470ustar00rootroot00000000000000import test from 'ava'; import leven from './index.js'; test('main', t => { t.is(leven('a', 'b'), 1); t.is(leven('ab', 'ac'), 1); t.is(leven('ac', 'bc'), 1); t.is(leven('abc', 'axc'), 1); t.is(leven('kitten', 'sitting'), 3); t.is(leven('xabxcdxxefxgx', '1ab2cd34ef5g6'), 6); t.is(leven('cat', 'cow'), 2); t.is(leven('xabxcdxxefxgx', 'abcdefg'), 6); t.is(leven('javawasneat', 'scalaisgreat'), 7); t.is(leven('example', 'samples'), 3); t.is(leven('sturgeon', 'urgently'), 6); t.is(leven('levenshtein', 'frankenstein'), 6); t.is(leven('distance', 'difference'), 5); t.is(leven('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'), 2); });