levenshtein-edit-distance
Advanced tools
Comparing version 1.0.0 to 2.0.0
197
cli.js
#!/usr/bin/env node | ||
'use strict'; | ||
/* eslint-disable no-process-exit */ | ||
/* | ||
* Dependencies. | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2014 Titus Wormer | ||
* @license MIT | ||
* @module levenshtein-edit-distance | ||
* @fileoverview CLI for `levenshtein-edit-distance`. | ||
*/ | ||
var levenshtein, | ||
pack; | ||
'use strict'; | ||
levenshtein = require('./'); | ||
pack = require('./package.json'); | ||
/* Dependencies. */ | ||
var pack = require('./package.json'); | ||
var levenshtein = require('./'); | ||
/* | ||
* Detect if a value is expected to be piped in. | ||
*/ | ||
/* Arguments. */ | ||
var argv = process.argv.slice(2); | ||
var insensitive = false; | ||
var expextPipeIn; | ||
['--insensitive', '-i'].forEach(function (flag) { | ||
var pos = argv.indexOf(flag); | ||
if (pos !== -1) { | ||
argv.splice(pos, 1); | ||
insensitive = true; | ||
} | ||
}); | ||
expextPipeIn = !process.stdin.isTTY; | ||
/* | ||
* Arguments. | ||
*/ | ||
var argv; | ||
argv = process.argv.slice(2); | ||
/* | ||
* Command. | ||
*/ | ||
var command; | ||
command = Object.keys(pack.bin)[0]; | ||
/* | ||
* Whether to ignore case. | ||
*/ | ||
var insensitive = false; | ||
/** | ||
* Get the distance for words. | ||
* | ||
* @param {Array.<string>} values | ||
* @return {number} | ||
*/ | ||
function distance(values) { | ||
return levenshtein(values[0], values[1], insensitive); | ||
/* Program. */ | ||
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) { | ||
getDistance(argv.join(' ')); | ||
} else { | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { | ||
getDistance(data.trim()); | ||
}); | ||
} | ||
@@ -62,25 +55,24 @@ | ||
function help() { | ||
return [ | ||
'', | ||
'Usage: ' + command + ' [options] word word', | ||
'', | ||
pack.description, | ||
'', | ||
'Options:', | ||
'', | ||
' -h, --help output usage information', | ||
' -v, --version output version number', | ||
' -i, --insensitive ignore casing', | ||
'', | ||
'Usage:', | ||
'', | ||
'# output distance', | ||
'$ ' + command + ' sitting kitten', | ||
'# ' + distance(['sitting', 'kitten']), | ||
'', | ||
'# output distance from stdin', | ||
'$ echo "saturday,sunday" | ' + command, | ||
'# ' + distance(['saturday', 'sunday']), | ||
'' | ||
].join('\n ') + '\n'; | ||
return [ | ||
'', | ||
'Usage: ' + pack.name + ' [options] <word> <word>', | ||
'', | ||
pack.description, | ||
'', | ||
'Options:', | ||
'', | ||
' -h, --help output usage information', | ||
' -v, --version output version number', | ||
' -i, --insensitive ignore casing', | ||
'', | ||
'Usage:', | ||
'', | ||
'# output distance', | ||
'$ ' + pack.name + ' sitting kitten', | ||
'# ' + distance(['sitting', 'kitten']), | ||
'', | ||
'# output distance from stdin', | ||
'$ echo "saturday,sunday" | ' + pack.name, | ||
'# ' + distance(['saturday', 'sunday']) | ||
].join('\n ') + '\n'; | ||
} | ||
@@ -94,59 +86,20 @@ | ||
function getDistance(value) { | ||
var values; | ||
var values = value.split(',').join(' ').split(/\s+/); | ||
if (value) { | ||
values = value.split(',').join(' ').split(/\s+/); | ||
} | ||
if (values && values.length === 2) { | ||
console.log(distance(values)); | ||
} else { | ||
process.stderr.write(help()); | ||
process.exit(1); | ||
} | ||
if (values.length === 2) { | ||
console.log(distance(values)); | ||
} else { | ||
process.stderr.write(help()); | ||
process.exit(1); | ||
} | ||
} | ||
/* | ||
* Program. | ||
/** | ||
* Get the distance for words. | ||
* | ||
* @param {Array.<string>} values | ||
* @return {number} | ||
*/ | ||
var index = argv.indexOf('--insensitive'); | ||
if (index === -1) { | ||
index = argv.indexOf('-i'); | ||
function distance(values) { | ||
return levenshtein(values[0], values[1], insensitive); | ||
} | ||
if ( | ||
argv.indexOf('--help') !== -1 || | ||
argv.indexOf('-h') !== -1 | ||
) { | ||
console.log(help()); | ||
return; | ||
} | ||
if ( | ||
argv.indexOf('--version') !== -1 || | ||
argv.indexOf('-v') !== -1 | ||
) { | ||
console.log(pack.version); | ||
return; | ||
} | ||
if (index !== -1) { | ||
argv.splice(index, 1); | ||
insensitive = true; | ||
} | ||
if (argv.length) { | ||
getDistance(argv.join(' ')); | ||
} else if (!expextPipeIn) { | ||
getDistance(); | ||
} else { | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { | ||
getDistance(data.trim()); | ||
}); | ||
} |
108
index.js
@@ -1,71 +0,73 @@ | ||
var cache, | ||
codes; | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2014 Titus Wormer | ||
* @license MIT | ||
* @module levenshtein-edit-distance | ||
* @fileoverview Detect edit distance. | ||
*/ | ||
cache = []; | ||
codes = []; | ||
'use strict'; | ||
function levenshtein(value, other, insensitive) { | ||
var length, | ||
lengthOther, | ||
code, | ||
result, | ||
distance, | ||
distanceOther, | ||
index, | ||
indexOther; | ||
/* Expose. */ | ||
module.exports = levenshtein; | ||
if (value === other) { | ||
return 0; | ||
} | ||
/* eslint-disable no-nested-ternary */ | ||
length = value.length; | ||
lengthOther = other.length; | ||
var cache = []; | ||
var codes = []; | ||
if (length === 0) { | ||
return lengthOther; | ||
} | ||
function levenshtein(value, other, insensitive) { | ||
var length; | ||
var lengthOther; | ||
var code; | ||
var result; | ||
var distance; | ||
var distanceOther; | ||
var index; | ||
var indexOther; | ||
if (lengthOther === 0) { | ||
return length; | ||
} | ||
if (value === other) { | ||
return 0; | ||
} | ||
if (insensitive) { | ||
value = value.toLowerCase(); | ||
other = other.toLowerCase(); | ||
} | ||
length = value.length; | ||
lengthOther = other.length; | ||
index = 0; | ||
if (length === 0) { | ||
return lengthOther; | ||
} | ||
while (index < length) { | ||
codes[index] = value.charCodeAt(index); | ||
cache[index] = ++index; | ||
} | ||
if (lengthOther === 0) { | ||
return length; | ||
} | ||
indexOther = 0; | ||
if (insensitive) { | ||
value = value.toLowerCase(); | ||
other = other.toLowerCase(); | ||
} | ||
while (indexOther < lengthOther) { | ||
code = other.charCodeAt(indexOther); | ||
index = 0; | ||
result = distance = indexOther++; | ||
while (index < length) { | ||
codes[index] = value.charCodeAt(index); | ||
cache[index] = ++index; | ||
} | ||
index = -1; | ||
indexOther = 0; | ||
while (++index < length) { | ||
distanceOther = code === codes[index] ? distance : distance + 1; | ||
while (indexOther < lengthOther) { | ||
code = other.charCodeAt(indexOther); | ||
result = distance = indexOther++; | ||
index = -1; | ||
distance = cache[index]; | ||
cache[index] = result = distance > result | ||
? distanceOther > result | ||
? result + 1 | ||
: distanceOther | ||
: distanceOther > distance | ||
? distance + 1 | ||
: distanceOther; | ||
} | ||
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; | ||
} | ||
} | ||
return result; | ||
return result; | ||
} | ||
module.exports = levenshtein; |
{ | ||
"name": "levenshtein-edit-distance", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Levenshtein edit distance. No cruft. Real fast.", | ||
@@ -14,32 +14,53 @@ "license": "MIT", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/levenshtein-edit-distance.git" | ||
"repository": "https://github.com/wooorm/levenshtein-edit-distance", | ||
"bugs": "https://github.com/wooorm/levenshtein-edit-distance/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)" | ||
], | ||
"engines": { | ||
"node": ">=0.11.0" | ||
}, | ||
"author": "Titus Wormer <tituswormer@gmail.com>", | ||
"bin": { | ||
"levenshtein-edit-distance": "cli.js" | ||
}, | ||
"bin": "cli.js", | ||
"files": [ | ||
"index.js", | ||
"cli.js" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"eslint": "^0.23.0", | ||
"istanbul": "^0.3.0", | ||
"jscs": "^1.0.0", | ||
"jscs-jsdoc": "^1.0.0", | ||
"matcha": "^0.6.0", | ||
"mocha": "^2.0.0" | ||
"browserify": "^13.0.0", | ||
"esmangle": "^1.0.0", | ||
"execa": "^0.4.0", | ||
"nyc": "^8.3.0", | ||
"remark-cli": "^2.0.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.16.0" | ||
}, | ||
"scripts": { | ||
"test-api": "_mocha --check-leaks test.js", | ||
"test-cli": "bash ./test.sh", | ||
"test-coveralls": "istanbul cover _mocha --report lcovonly -- --check-leaks test.js", | ||
"test-coverage": "istanbul cover _mocha -- --check-leaks test.js", | ||
"test-travis": "npm run test-coveralls && npm run test-cli", | ||
"test": "npm run test-api && npm run test-cli", | ||
"lint-api": "eslint .", | ||
"lint-style": "jscs --reporter inline .", | ||
"lint": "npm run lint-api && npm run lint-style", | ||
"make": "npm run lint && npm run test-coverage", | ||
"install-benchmark": "npm install natural fast-levenshtein levenshtein levenshtein-component levenshtein-deltas leven", | ||
"benchmark": "matcha benchmark.js" | ||
"build-md": "remark . --quiet --frail", | ||
"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", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"xo": { | ||
"space": true, | ||
"ignores": [ | ||
"levenshtein-edit-distance.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"presets": "wooorm" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
1
8413
8
5
151
98
1