levenshtein-edit-distance
Advanced tools
Comparing version 2.0.2 to 2.0.3
88
cli.js
#!/usr/bin/env node | ||
'use strict'; | ||
'use strict' | ||
var pack = require('./package.json'); | ||
var levenshtein = require('./'); | ||
var pack = require('./package.json') | ||
var levenshtein = require('.') | ||
var argv = process.argv.slice(2); | ||
var insensitive = false; | ||
var argv = process.argv.slice(2) | ||
var insensitives = ['--insensitive', '-i'] | ||
var insensitive = false | ||
['--insensitive', '-i'].forEach(function (flag) { | ||
var pos = argv.indexOf(flag); | ||
insensitives.forEach(function(flag) { | ||
var pos = argv.indexOf(flag) | ||
if (pos !== -1) { | ||
argv.splice(pos, 1); | ||
insensitive = true; | ||
argv.splice(pos, 1) | ||
insensitive = true | ||
} | ||
}); | ||
}) | ||
if ( | ||
argv.indexOf('--help') !== -1 || | ||
argv.indexOf('-h') !== -1 | ||
) { | ||
console.log(help()); | ||
} else if ( | ||
argv.indexOf('--version') !== -1 || | ||
argv.indexOf('-v') !== -1 | ||
) { | ||
console.log(pack.version); | ||
if (argv.indexOf('--help') !== -1 || argv.indexOf('-h') !== -1) { | ||
console.log(help()) | ||
} else if (argv.indexOf('--version') !== -1 || argv.indexOf('-v') !== -1) { | ||
console.log(pack.version) | ||
} else if (argv.length === 0) { | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { | ||
getDistance(data.trim()); | ||
}); | ||
process.stdin.resume() | ||
process.stdin.setEncoding('utf8') | ||
process.stdin.on('data', function(data) { | ||
getDistance(data.trim()) | ||
}) | ||
} else { | ||
getDistance(argv.join(' ')); | ||
getDistance(argv.join(' ')) | ||
} | ||
@@ -41,32 +36,35 @@ | ||
'', | ||
'Usage: ' + pack.name + ' [options] <word> <word>', | ||
' Usage: ' + pack.name + ' [options] <word> <word>', | ||
'', | ||
pack.description, | ||
' ' + pack.description, | ||
'', | ||
'Options:', | ||
' Options:', | ||
'', | ||
' -h, --help output usage information', | ||
' -v, --version output version number', | ||
' -i, --insensitive ignore casing', | ||
' -h, --help output usage information', | ||
' -v, --version output version number', | ||
' -i, --insensitive ignore casing', | ||
'', | ||
'Usage:', | ||
' Usage:', | ||
'', | ||
'# output distance', | ||
'$ ' + pack.name + ' sitting kitten', | ||
'# ' + distance(['sitting', 'kitten']), | ||
' # output distance', | ||
' $ ' + pack.name + ' sitting kitten', | ||
' ' + distance(['sitting', 'kitten']), | ||
'', | ||
'# output distance from stdin', | ||
'$ echo "saturday,sunday" | ' + pack.name, | ||
'# ' + distance(['saturday', 'sunday']) | ||
].join('\n ') + '\n'; | ||
' # output distance from stdin', | ||
' $ echo "saturday,sunday" | ' + pack.name, | ||
' ' + distance(['saturday', 'sunday']) | ||
].join('\n') | ||
} | ||
function getDistance(value) { | ||
var values = value.split(',').join(' ').split(/\s+/); | ||
var values = value | ||
.split(',') | ||
.join(' ') | ||
.split(/\s+/) | ||
if (values.length === 2) { | ||
console.log(distance(values)); | ||
console.log(distance(values)) | ||
} else { | ||
process.stderr.write(help()); | ||
process.exit(1); | ||
process.stderr.write(help()) | ||
process.exit(1) | ||
} | ||
@@ -76,3 +74,3 @@ } | ||
function distance(values) { | ||
return levenshtein(values[0], values[1], insensitive); | ||
return levenshtein(values[0], values[1], insensitive) | ||
} |
71
index.js
@@ -1,64 +0,67 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = levenshtein; | ||
module.exports = levenshtein | ||
/* eslint-disable no-nested-ternary */ | ||
var cache = [] | ||
var codes = [] | ||
var cache = []; | ||
var codes = []; | ||
function levenshtein(value, other, insensitive) { | ||
var length; | ||
var lengthOther; | ||
var code; | ||
var result; | ||
var distance; | ||
var distanceOther; | ||
var index; | ||
var indexOther; | ||
var length | ||
var lengthOther | ||
var code | ||
var result | ||
var distance | ||
var distanceOther | ||
var index | ||
var indexOther | ||
if (value === other) { | ||
return 0; | ||
return 0 | ||
} | ||
length = value.length; | ||
lengthOther = other.length; | ||
length = value.length | ||
lengthOther = other.length | ||
if (length === 0) { | ||
return lengthOther; | ||
return lengthOther | ||
} | ||
if (lengthOther === 0) { | ||
return length; | ||
return length | ||
} | ||
if (insensitive) { | ||
value = value.toLowerCase(); | ||
other = other.toLowerCase(); | ||
value = value.toLowerCase() | ||
other = other.toLowerCase() | ||
} | ||
index = 0; | ||
index = 0 | ||
while (index < length) { | ||
codes[index] = value.charCodeAt(index); | ||
cache[index] = ++index; | ||
codes[index] = value.charCodeAt(index) | ||
cache[index] = ++index | ||
} | ||
indexOther = 0; | ||
indexOther = 0 | ||
while (indexOther < lengthOther) { | ||
code = other.charCodeAt(indexOther); | ||
result = distance = indexOther++; | ||
index = -1; | ||
code = other.charCodeAt(indexOther) | ||
result = distance = indexOther++ | ||
index = -1 | ||
while (++index < length) { | ||
distanceOther = code === codes[index] ? distance : distance + 1; | ||
distance = cache[index]; | ||
cache[index] = result = distance > result ? | ||
distanceOther > result ? result + 1 : distanceOther : | ||
distanceOther > distance ? distance + 1 : distanceOther; | ||
distanceOther = code === codes[index] ? distance : distance + 1 | ||
distance = cache[index] | ||
cache[index] = result = | ||
distance > result | ||
? distanceOther > result | ||
? result + 1 | ||
: distanceOther | ||
: distanceOther > distance | ||
? distance + 1 | ||
: distanceOther | ||
} | ||
} | ||
return result; | ||
return result | ||
} |
{ | ||
"name": "levenshtein-edit-distance", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Levenshtein edit distance. No cruft. Real fast.", | ||
@@ -28,20 +28,20 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"esmangle": "^1.0.0", | ||
"execa": "^0.8.0", | ||
"nyc": "^11.0.0", | ||
"remark-cli": "^4.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"browserify": "^16.0.0", | ||
"execa": "^1.0.0", | ||
"nyc": "^13.0.0", | ||
"prettier": "^1.14.2", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.18.0" | ||
"tinyify": "^2.4.3", | ||
"xo": "^0.22.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . -qfo", | ||
"build-bundle": "browserify index.js -s levenshteinEditDistance > levenshtein-edit-distance.js", | ||
"build-mangle": "esmangle levenshtein-edit-distance.js > levenshtein-edit-distance.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint": "xo", | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"build-bundle": "browserify . -s levenshteinEditDistance -o levenshtein-edit-distance.js", | ||
"build-mangle": "browserify . -s levenshteinEditDistance -p tinyify -o levenshtein-edit-distance.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
}, | ||
@@ -54,4 +54,12 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
@@ -58,0 +66,0 @@ "rules": { |
@@ -17,17 +17,17 @@ # levenshtein-edit-distance [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
```js | ||
var levenshtein = require('levenshtein-edit-distance'); | ||
var levenshtein = require('levenshtein-edit-distance') | ||
levenshtein('levenshtein', 'levenshtein'); //=> 0 | ||
levenshtein('sitting', 'kitten'); //=> 3 | ||
levenshtein('gumbo', 'gambol'); //=> 2 | ||
levenshtein('saturday', 'sunday'); //=> 3 | ||
levenshtein('levenshtein', 'levenshtein') // => 0 | ||
levenshtein('sitting', 'kitten') // => 3 | ||
levenshtein('gumbo', 'gambol') // => 2 | ||
levenshtein('saturday', 'sunday') // => 3 | ||
/* Case sensitive! */ | ||
levenshtein('DwAyNE', 'DUANE') !== levenshtein('dwayne', 'DuAnE'); //=> true | ||
levenshtein('DwAyNE', 'DUANE') !== levenshtein('dwayne', 'DuAnE') // => true | ||
/* Insensitive: */ | ||
levenshtein('DwAyNE', 'DUANE', true) === levenshtein('dwayne', 'DuAnE', true); //=> true | ||
levenshtein('DwAyNE', 'DUANE', true) === levenshtein('dwayne', 'DuAnE', true) // => true | ||
/* Order insensitive */ | ||
levenshtein('aarrgh', 'aargh') === levenshtein('aargh', 'aarrgh'); //=> true | ||
levenshtein('aarrgh', 'aargh') === levenshtein('aargh', 'aarrgh') // => true | ||
``` | ||
@@ -75,11 +75,11 @@ | ||
— Rust API | ||
* [`lancaster-stemmer`](https://github.com/wooorm/lancaster-stemmer) | ||
* [`lancaster-stemmer`](https://github.com/words/lancaster-stemmer) | ||
— Lancaster stemming algorithm | ||
* [`double-metaphone`](https://github.com/wooorm/double-metaphone) | ||
* [`double-metaphone`](https://github.com/words/double-metaphone) | ||
— Double Metaphone implementation | ||
* [`soundex-code`](https://github.com/wooorm/soundex-code) | ||
* [`soundex-code`](https://github.com/words/soundex-code) | ||
— Fast Soundex implementation | ||
* [`dice-coefficient`](https://github.com/wooorm/dice-coefficient) | ||
* [`dice-coefficient`](https://github.com/words/dice-coefficient) | ||
— Sørensen–Dice coefficient | ||
* [`syllable`](https://github.com/wooorm/syllable) | ||
* [`syllable`](https://github.com/words/syllable) | ||
— Syllable count in an English word | ||
@@ -101,3 +101,3 @@ | ||
[license]: LICENSE | ||
[license]: license | ||
@@ -104,0 +104,0 @@ [author]: http://wooorm.com |
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
8606
119
9