levenshtein-edit-distance
Advanced tools
Comparing version 3.0.0 to 3.0.1
17
cli.js
#!/usr/bin/env node | ||
import fs from 'fs' | ||
import {URL} from 'url' | ||
import fs from 'node:fs' | ||
import {URL} from 'node:url' | ||
import process from 'node:process' | ||
import {levenshteinEditDistance} from './index.js' | ||
/** @type {Object.<string, unknown>} */ | ||
var pack = JSON.parse( | ||
String(fs.readFileSync(new URL('./package.json', import.meta.url))) | ||
const pack = JSON.parse( | ||
String(fs.readFileSync(new URL('package.json', import.meta.url))) | ||
) | ||
var argv = process.argv.slice(2) | ||
var insensitive = false | ||
var pos = argv.indexOf('--insensitive') | ||
const argv = process.argv.slice(2) | ||
let insensitive = false | ||
let pos = argv.indexOf('--insensitive') | ||
@@ -69,3 +70,3 @@ if (pos !== -1) { | ||
function getDistance(value) { | ||
var values = value.split(',').join(' ').split(/\s+/) | ||
const values = value.split(',').join(' ').split(/\s+/) | ||
@@ -72,0 +73,0 @@ if (values.length === 2) { |
/** | ||
* Levenshtein edit distance. | ||
* | ||
* @param {string} value | ||
* Primary value. | ||
* @param {string} other | ||
* @param {boolean} [insensitive] | ||
* Other value. | ||
* @param {boolean} [insensitive=false] | ||
* Compare insensitive to ASCII casing. | ||
* @returns {number} | ||
* Distance between `value` and `other`. | ||
*/ | ||
@@ -10,3 +16,3 @@ export function levenshteinEditDistance( | ||
other: string, | ||
insensitive?: boolean | ||
insensitive?: boolean | undefined | ||
): number |
50
index.js
@@ -1,26 +0,19 @@ | ||
/** @type {Array.<number>} */ | ||
var codes = [] | ||
/** @type {Array.<number>} */ | ||
var cache = [] | ||
/** @type {Array<number>} */ | ||
const codes = [] | ||
/** @type {Array<number>} */ | ||
const cache = [] | ||
/** | ||
* Levenshtein edit distance. | ||
* | ||
* @param {string} value | ||
* Primary value. | ||
* @param {string} other | ||
* @param {boolean} [insensitive] | ||
* Other value. | ||
* @param {boolean} [insensitive=false] | ||
* Compare insensitive to ASCII casing. | ||
* @returns {number} | ||
* Distance between `value` and `other`. | ||
*/ | ||
export function levenshteinEditDistance(value, other, insensitive) { | ||
/** @type {number} */ | ||
var code | ||
/** @type {number} */ | ||
var result | ||
/** @type {number} */ | ||
var distance | ||
/** @type {number} */ | ||
var distanceOther | ||
/** @type {number} */ | ||
var index | ||
/** @type {number} */ | ||
var indexOther | ||
if (value === other) { | ||
@@ -43,5 +36,6 @@ return 0 | ||
index = 0 | ||
let index = 0 | ||
while (index < value.length) { | ||
// eslint-disable-next-line unicorn/prefer-code-point | ||
codes[index] = value.charCodeAt(index) | ||
@@ -51,13 +45,17 @@ cache[index] = ++index | ||
indexOther = 0 | ||
let indexOther = 0 | ||
/** @type {number} */ | ||
let result | ||
while (indexOther < other.length) { | ||
code = other.charCodeAt(indexOther) | ||
result = distance = indexOther++ | ||
index = -1 | ||
// eslint-disable-next-line unicorn/prefer-code-point | ||
const code = other.charCodeAt(indexOther) | ||
let index = -1 | ||
let distance = indexOther++ | ||
result = distance | ||
while (++index < value.length) { | ||
distanceOther = code === codes[index] ? distance : distance + 1 | ||
const distanceOther = code === codes[index] ? distance : distance + 1 | ||
distance = cache[index] | ||
cache[index] = result = | ||
result = | ||
distance > result | ||
@@ -70,6 +68,8 @@ ? distanceOther > result | ||
: distanceOther | ||
cache[index] = result | ||
} | ||
} | ||
// @ts-expect-error: always assigned. | ||
return result | ||
} |
{ | ||
"name": "levenshtein-edit-distance", | ||
"version": "3.0.0", | ||
"description": "Levenshtein edit distance. No cruft. Real fast.", | ||
"version": "3.0.1", | ||
"description": "Levenshtein edit distance", | ||
"license": "MIT", | ||
@@ -36,19 +36,17 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.52.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -65,8 +63,3 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-multi-assign": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -73,0 +66,0 @@ "remarkConfig": { |
133
readme.md
@@ -8,13 +8,37 @@ # levenshtein-edit-distance | ||
[Levenshtein edit distance][wiki] (by [Vladimir Levenshtein][vlad]). | ||
No cruft. | ||
Real fast. | ||
[Levenshtein distance][wiki] (by [Vladimir Levenshtein][vlad]). | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`levenshteinEditDistance(value, other[, insensitive])`](#levenshteineditdistancevalue-other-insensitive) | ||
* [CLI](#cli) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [Security](#security) | ||
* [License](#license) | ||
## What is this? | ||
This package exposes a string similarity algorithm. | ||
That means it gets two strings (typically words), and turns it into the minimum | ||
number of single-character edits (insertions, deletions or substitutions) | ||
needed to turn one string into the other. | ||
## When should I use this? | ||
You’re probably dealing with natural language, and know you need this, if | ||
you’re here! | ||
## Install | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+, 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -24,7 +48,18 @@ npm install levenshtein-edit-distance | ||
## API | ||
In Deno with [`esm.sh`][esmsh]: | ||
This package exports the following identifiers: `levenshteinEditDistance`. | ||
There is no default export. | ||
```js | ||
import {levenshteinEditDistance} from 'https://esm.sh/levenshtein-edit-distance@3' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {levenshteinEditDistance} from 'https://esm.sh/levenshtein-edit-distance@3?bundle' | ||
</script> | ||
``` | ||
## Use | ||
```js | ||
@@ -38,12 +73,36 @@ import {levenshteinEditDistance} from 'levenshtein-edit-distance' | ||
// Case sensitive! | ||
// Insensitive to order: | ||
levenshteinEditDistance('aarrgh', 'aargh') === levenshtein('aargh', 'aarrgh') // => true | ||
// Sensitive to ASCII casing by default: | ||
levenshteinEditDistance('DwAyNE', 'DUANE') !== levenshtein('dwayne', 'DuAnE') // => true | ||
// Insensitive | ||
// Insensitive: | ||
levenshteinEditDistance('DwAyNE', 'DUANE', true) === levenshtein('dwayne', 'DuAnE', true) // => true | ||
// Order insensitive | ||
levenshteinEditDistance('aarrgh', 'aargh') === levenshtein('aargh', 'aarrgh') // => true | ||
``` | ||
## API | ||
This package exports the identifier `levenshteinEditDistance`. | ||
There is no default export. | ||
### `levenshteinEditDistance(value, other[, insensitive])` | ||
Levenshtein edit distance. | ||
###### `value` | ||
Primary value (`string`, required). | ||
###### `other` | ||
Other value (`string`, required). | ||
###### `insensitive` | ||
Compare insensitive to ASCII casing (`boolean`, default: `false`). | ||
##### Returns | ||
Distance between `value` and `other` (`number`). | ||
## CLI | ||
@@ -54,3 +113,3 @@ | ||
Levenshtein edit distance. No cruft. Real fast. | ||
Levenshtein edit distance. | ||
@@ -74,2 +133,13 @@ Options: | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
## Compatibility | ||
This package is at least compatible with all maintained versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
It also works in Deno and modern browsers. | ||
## Related | ||
@@ -84,14 +154,23 @@ | ||
* [`stemmer`](https://github.com/words/stemmer) | ||
— Porter stemming algorithm | ||
— porter stemming algorithm | ||
* [`lancaster-stemmer`](https://github.com/words/lancaster-stemmer) | ||
— Lancaster stemming algorithm | ||
— lancaster stemming algorithm | ||
* [`double-metaphone`](https://github.com/words/double-metaphone) | ||
— Double Metaphone implementation | ||
— double metaphone algorithm | ||
* [`soundex-code`](https://github.com/words/soundex-code) | ||
— Fast Soundex implementation | ||
— soundex algorithm | ||
* [`dice-coefficient`](https://github.com/words/dice-coefficient) | ||
— Sørensen–Dice coefficient | ||
— sørensen–dice coefficient | ||
* [`syllable`](https://github.com/words/syllable) | ||
— Syllable count in an English word | ||
— syllable count of English words | ||
## Contribute | ||
Yes please! | ||
See [How to Contribute to Open Source][contribute]. | ||
## Security | ||
This package is safe. | ||
## License | ||
@@ -121,2 +200,10 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[contribute]: https://opensource.guide/how-to-contribute/ | ||
[license]: license | ||
@@ -123,0 +210,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12377
8
157
209
1
237962