levenshtein
Advanced tools
Comparing version 1.0.2 to 1.0.4
{ | ||
"name": "levenshtein", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"main": "lib/levenshtein.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
@@ -13,45 +13,28 @@ (function(root, factory){ | ||
var forEach; | ||
// Generics | ||
if ( ! Array.forEach ) { | ||
forEach = function ( array, iterator, context ) { | ||
iterator = context | ||
? iterator.bind( context ) | ||
: iterator | ||
Array.prototype.forEach.call( array, iterator ) | ||
} | ||
} else { | ||
forEach = Array.forEach; | ||
function forEach( array, fn ) { var i, length | ||
i = -1 | ||
length = array.length | ||
while ( ++i < length ) | ||
fn( array[ i ], i, array ) | ||
} | ||
// Levenshtein distance | ||
return function Levenshtein( str_m, str_n ) { var previous, current, matrix | ||
// Instance methods | ||
this.valueOf = function() { | ||
return this.distance | ||
} | ||
function map( array, fn ) { var result | ||
result = Array( array.length ) | ||
forEach( array, function ( val, i, array ) { | ||
result.push( fn( val, i, array ) ) | ||
}) | ||
return result | ||
} | ||
this.toString = this.inspect = function inspect ( no_print ) { var max, buff, sep, rows | ||
max = matrix.reduce( function( m, o ) { | ||
return Math.max( m, o.reduce( Math.max, 0 ) ) | ||
}, 0 ) | ||
buff = Array( ( max + '' ).length ).join( ' ' ) | ||
function reduce( array, fn, accumulator ) { | ||
forEach( array, function( val, i, array ) { | ||
accumulator = fn( val, i, array ) | ||
}) | ||
return accumulator | ||
} | ||
sep = [] | ||
while ( sep.length < (matrix[0] && matrix[0].length || 0) ) | ||
sep[ sep.length ] = Array( buff.length + 1 ).join( '-' ) | ||
sep = sep.join( '-+' ) + '-' | ||
rows = matrix.map( function( row ) { var cells | ||
cells = row.map( function( cell ) { | ||
return ( buff + cell ).slice( - buff.length ) | ||
}) | ||
return cells.join( ' |' ) + ' ' | ||
}) | ||
return rows.join( "\n" + sep + "\n" ) | ||
} | ||
// Levenshtein distance | ||
function Levenshtein( str_m, str_n ) { var previous, current, matrix | ||
// Constructor | ||
matrix = [] | ||
matrix = this._matrix = [] | ||
@@ -91,2 +74,35 @@ // Sanity checks | ||
} | ||
Levenshtein.prototype.toString = Levenshtein.prototype.inspect = function inspect ( no_print ) { var matrix, max, buff, sep, rows | ||
matrix = this.getMatrix() | ||
max = reduce( matrix,function( m, o ) { | ||
return Math.max( m, reduce( o, Math.max, 0 ) ) | ||
}, 0 ) | ||
buff = Array( ( max + '' ).length ).join( ' ' ) | ||
sep = [] | ||
while ( sep.length < (matrix[0] && matrix[0].length || 0) ) | ||
sep[ sep.length ] = Array( buff.length + 1 ).join( '-' ) | ||
sep = sep.join( '-+' ) + '-' | ||
rows = map( matrix, function( row ) { var cells | ||
cells = map( row, function( cell ) { | ||
return ( buff + cell ).slice( - buff.length ) | ||
}) | ||
return cells.join( ' |' ) + ' ' | ||
}) | ||
return rows.join( "\n" + sep + "\n" ) | ||
} | ||
Levenshtein.prototype.getMatrix = function () { | ||
return this._matrix.slice() | ||
} | ||
Levenshtein.prototype.valueOf = function() { | ||
return this.distance | ||
} | ||
return Levenshtein | ||
})); |
@@ -5,3 +5,3 @@ { | ||
"author": "Gianni Chiappetta <gianni@runlevel6.org> (http://gf3.ca)", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"main": "./lib/levenshtein", | ||
@@ -8,0 +8,0 @@ "engines": [ |
# Levenshtein | ||
[Levenshtein](http://en.wikipedia.org/wiki/Levenshtein_distance) string difference in Javascript. | ||
[Levenshtein][wikipedia] string difference in Javascript. | ||
![Screenshot](http://f.cl.ly/items/0E3B0c3H3z3F1A2t1437/Levenshtein.png) | ||
![Screenshot][screenshot] | ||
@@ -11,2 +11,3 @@ `Levenshtein` also does some neat things like coerce to a number and string | ||
## API | ||
@@ -26,21 +27,66 @@ | ||
* `Levenshtein#toString()` → `String` | ||
* Alias of: `Levenshtein#inspect`. | ||
* Alias of: `Levenshtein#inspect()`. | ||
* `Levenshtein#valueOf()` → `Number` | ||
* Alias of: `Levenshtein#distance`. | ||
## Installation | ||
With npm: | ||
Levenshtein works in both the browser and [node.js][node]. | ||
npm install levenshtein | ||
With git: | ||
### Browser | ||
git clone git://github.com/gf3/Levenshtein.git | ||
Simply include `levenshtein.js`: | ||
``` html | ||
<script src="/javascripts/levenshtein.js"></script> | ||
``` | ||
### NPM | ||
Install via npm: | ||
``` sh | ||
npm install levenshtein | ||
``` | ||
Or put it in your `package.json`: | ||
``` json | ||
{ "levenshtein": "~1.0" } | ||
``` | ||
### Bower | ||
``` sh | ||
bower install levenshtein | ||
``` | ||
### Git | ||
``` sh | ||
git clone git://github.com/gf3/Levenshtein.git | ||
``` | ||
## License | ||
Levenshtein is [UNLICENSED](http://unlicense.org/). | ||
Levenshtein is [UNLICENSED][unlicense]. | ||
## Author | ||
Written by [Gianni Chiappetta](https://github.com/gf3) – [gf3.ca](http://gf3.ca) | ||
Written by [Gianni Chiappetta][github] – [gf3.ca][gf3] | ||
[gf3]: http://gf3.ca | ||
[github]: https://github.com/gf3 | ||
[node]: http://nodejs.org/ | ||
[screenshot]: http://f.cl.ly/items/3g1m0u401j0m2H2k0s2X/levenshtein.PNG | ||
[unlicense]: http://unlicense.org/ | ||
[wikipedia]: http://en.wikipedia.org/wiki/Levenshtein_distance | ||
@@ -24,1 +24,7 @@ // Tests | ||
exports['test Levenshtein matrix can be retrieved'] = function() { | ||
var l1 = new Levenshtein( 'kitten', 'sitting' ) | ||
assert.ok( l1.getMatrix() instanceof Array ) | ||
assert.ok( l1.getMatrix()[0].length ) | ||
assert.ok( l1.getMatrix()[0] instanceof Array ) | ||
} |
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
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
6377
128
91
0