levenshtein-edit-distance
Advanced tools
Comparing version 0.1.3 to 0.1.4
63
cli.js
@@ -8,9 +8,17 @@ #!/usr/bin/env node | ||
var levenshteinEditDistance, | ||
var levenshtein, | ||
pack; | ||
levenshteinEditDistance = require('./'); | ||
levenshtein = require('./'); | ||
pack = require('./package.json'); | ||
/* | ||
* Detect if a value is expected to be piped in. | ||
*/ | ||
var expextPipeIn; | ||
expextPipeIn = !process.stdin.isTTY; | ||
/* | ||
* Arguments. | ||
@@ -32,3 +40,15 @@ */ | ||
/** | ||
* Get the distance for words. | ||
* | ||
* @param {Array.<string>} values | ||
* @return {number} | ||
*/ | ||
function distance(values) { | ||
return levenshtein(values[0], values[1]); | ||
} | ||
/** | ||
* Help. | ||
* | ||
* @return {string} | ||
*/ | ||
@@ -38,3 +58,3 @@ function help() { | ||
'', | ||
'Usage: ' + command + ' [options] words...', | ||
'Usage: ' + command + ' [options] word word', | ||
'', | ||
@@ -50,9 +70,10 @@ pack.description, | ||
'', | ||
'# output distance between values', | ||
'# output distance', | ||
'$ ' + command + ' sitting kitten', | ||
'# 3', | ||
'# ' + distance(['sitting', 'kitten']), | ||
'', | ||
'# output distance between values from stdin', | ||
'# output distance from stdin', | ||
'$ echo "saturday,sunday" | ' + command, | ||
'# 3' | ||
'# ' + distance(['saturday', 'sunday']), | ||
'' | ||
].join('\n ') + '\n'; | ||
@@ -62,9 +83,15 @@ } | ||
/** | ||
* Get the levenshtein distance of an array of strings. | ||
* Get the edit distance for a list containing two word. | ||
* | ||
* @param {Array.<string>} values | ||
* @param {string?} value | ||
*/ | ||
function getDistance(values) { | ||
if (values.length === 2) { | ||
console.log(levenshteinEditDistance(values[0], values[1])); | ||
function getDistance(value) { | ||
var values; | ||
if (value) { | ||
values = value.split(',').join(' ').split(/\s+/); | ||
} | ||
if (values && values.length === 2) { | ||
console.log(distance(values)); | ||
} else { | ||
@@ -90,8 +117,6 @@ process.stderr.write(help()); | ||
console.log(pack.version); | ||
} else if (argv[0]) { | ||
if (argv[0].indexOf(',') !== -1) { | ||
argv = argv[0].split(','); | ||
} | ||
getDistance(argv); | ||
} else if (argv.length) { | ||
getDistance(argv.join(' ')); | ||
} else if (!expextPipeIn) { | ||
getDistance(); | ||
} else { | ||
@@ -101,4 +126,4 @@ process.stdin.resume(); | ||
process.stdin.on('data', function (data) { | ||
getDistance(data.trim().split(/ |,/g)); | ||
getDistance(data.trim()); | ||
}); | ||
} |
{ | ||
"name": "levenshtein-edit-distance", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Levenshtein edit distance. No cruft. Real fast.", | ||
@@ -23,6 +23,6 @@ "license": "MIT", | ||
"devDependencies": { | ||
"eslint": "^0.10.0", | ||
"eslint": "^0.11.0", | ||
"istanbul": "^0.3.0", | ||
"jscs": "^1.0.0", | ||
"jscs-jsdoc": "^0.3.0", | ||
"jscs-jsdoc": "^0.4.0", | ||
"matcha": "^0.6.0", | ||
@@ -32,17 +32,18 @@ "mocha": "^2.0.0" | ||
"scripts": { | ||
"test": "node_modules/.bin/_mocha --reporter spec --check-leaks -u exports test.js", | ||
"test-api": "_mocha --check-leaks test.js", | ||
"test-cli": "bash ./test.sh", | ||
"test-coveralls": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec --check-leaks -u exports test.js", | ||
"test-travis": "npm run test-cli && npm run test-coveralls", | ||
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -- test.js", | ||
"lint-api": "node_modules/.bin/eslint index.js --rule 'no-cond-assign: false, strict: false'", | ||
"lint-benchmark": "node_modules/.bin/eslint benchmark.js --global suite,set,bench", | ||
"lint-cli": "node_modules/.bin/eslint cli.js --rule 'no-process-exit: false'", | ||
"lint-test": "node_modules/.bin/eslint test.js --env mocha", | ||
"lint-style": "node_modules/.bin/jscs benchmark.js cli.js test.js --reporter=inline", | ||
"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 --rule no-cond-assign:false,strict:false index.js", | ||
"lint-benchmark": "eslint --global suite,set,bench benchmark.js", | ||
"lint-cli": "eslint --rule no-process-exit:false cli.js", | ||
"lint-test": "eslint --env mocha test.js", | ||
"lint-style": "jscs --reporter inline benchmark.js cli.js test.js", | ||
"lint": "npm run lint-api && npm run lint-benchmark && npm run lint-cli && npm run lint-test && npm run lint-style", | ||
"make": "npm run lint && npm run coverage", | ||
"make": "npm run lint && npm run test-coverage", | ||
"install-benchmark": "npm install natural fast-levenshtein levenshtein levenshtein-component levenshtein-deltas leven", | ||
"benchmark": "node_modules/.bin/matcha benchmark.js" | ||
"benchmark": "matcha benchmark.js" | ||
} | ||
} |
@@ -7,3 +7,4 @@ # levenshtein-edit-distance [![Build Status](https://img.shields.io/travis/wooorm/levenshtein-edit-distance.svg?style=flat)](https://travis-ci.org/wooorm/levenshtein-edit-distance) [![Coverage Status](https://img.shields.io/coveralls/wooorm/levenshtein-edit-distance.svg?style=flat)](https://coveralls.io/r/wooorm/levenshtein-edit-distance?branch=master) | ||
npm: | ||
[npm](https://docs.npmjs.com/cli/install): | ||
```bash | ||
@@ -13,3 +14,4 @@ $ npm install levenshtein-edit-distance | ||
Component: | ||
[Component.js](https://github.com/componentjs/component): | ||
```bash | ||
@@ -19,3 +21,4 @@ $ component install wooorm/levenshtein-edit-distance | ||
Bower: | ||
[Bower](http://bower.io/#install-packages): | ||
```bash | ||
@@ -25,17 +28,23 @@ $ bower install levenshtein-edit-distance | ||
[Duo](http://duojs.org/#getting-started): | ||
```javascript | ||
var levenshtein = require('wooorm/levenshtein-edit-distance'); | ||
``` | ||
## Usage | ||
```js | ||
var levenshteinDistance = require('levenshtein-edit-distance'); | ||
```javascript | ||
var levenshtein = require('levenshtein-edit-distance'); | ||
levenshteinDistance('levenshtein', 'levenshtein'); // 0 | ||
levenshteinDistance('sitting', 'kitten'); // 3 | ||
levenshteinDistance('gumbo', 'gambol'); // 2 | ||
levenshteinDistance('saturday', 'sunday'); // 3 | ||
levenshtein('levenshtein', 'levenshtein'); // 0 | ||
levenshtein('sitting', 'kitten'); // 3 | ||
levenshtein('gumbo', 'gambol'); // 2 | ||
levenshtein('saturday', 'sunday'); // 3 | ||
/* Case sensitive! */ | ||
levenshteinDistance('DwAyNE', 'DUANE') !== levenshteinDistance('dwayne', 'DuAnE'); // true | ||
levenshtein('DwAyNE', 'DUANE') !== levenshtein('dwayne', 'DuAnE'); // true | ||
/* Order insensitive */ | ||
levenshteinDistance('aarrgh', 'aargh') === levenshteinDistance('aargh', 'aarrgh'); // true | ||
levenshtein('aarrgh', 'aargh') === levenshtein('aargh', 'aarrgh'); // true | ||
``` | ||
@@ -46,2 +55,3 @@ | ||
Install: | ||
```bash | ||
@@ -52,5 +62,6 @@ $ npm install --global levenshtein-edit-distance | ||
Use: | ||
``` | ||
Usage: levenshtein-edit-distance [options] words... | ||
```text | ||
Usage: levenshtein-edit-distance [options] word word | ||
Levenshtein edit distance. No cruft. Real fast. | ||
@@ -65,10 +76,11 @@ | ||
# output distance between values | ||
# output distance | ||
$ levenshtein-edit-distance sitting kitten | ||
# 3 | ||
# output distance between values from stdin | ||
# output distance from stdin | ||
$ echo "saturday,sunday" | levenshtein-edit-distance | ||
# 3 | ||
``` | ||
## Other Levenshtein libraries | ||
@@ -87,3 +99,3 @@ | ||
``` | ||
```text | ||
Levenshtein — to be fair, it lets you inspect a matrix | ||
@@ -113,2 +125,2 @@ 113 op/s » op/s * 1,000 | ||
MIT © [Titus Wormer](http://wooorm.com) | ||
[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) |
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
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
9426
152
118