levenshtein
Advanced tools
Comparing version
@@ -1,78 +0,90 @@ | ||
// Generics | ||
if ( ! Array.forEach ) { | ||
Array.forEach = function forEach ( array, iterator, context ) { | ||
iterator = context | ||
? iterator.bind( context ) | ||
: iterator | ||
Array.prototype.forEach.call( array, iterator ) | ||
(function(root, factory){ | ||
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { | ||
define(function(){ | ||
return factory(root); | ||
}); | ||
} else if (typeof module == 'object' && module && module.exports) { | ||
module.exports = factory(root); | ||
} else { | ||
root.Levenshtein = factory(root); | ||
} | ||
} | ||
}(this, function(root){ | ||
// Levenshtein distance | ||
function Levenshtein( str_m, str_n ) { var previous, current, matrix | ||
// Instance methods | ||
this.valueOf = function() { | ||
return this.distance | ||
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; | ||
} | ||
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( ' ' ) | ||
// Levenshtein distance | ||
return function Levenshtein( str_m, str_n ) { var previous, current, matrix | ||
// Instance methods | ||
this.valueOf = function() { | ||
return this.distance | ||
} | ||
sep = [] | ||
while ( sep.length < (matrix[0] && matrix[0].length || 0) ) | ||
sep[ sep.length ] = Array( buff.length + 1 ).join( '-' ) | ||
sep = sep.join( '-+' ) + '-' | ||
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( ' ' ) | ||
rows = matrix.map( function( row ) { var cells | ||
cells = row.map( function( cell ) { | ||
return ( buff + cell ).slice( - buff.length ) | ||
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 cells.join( ' |' ) + ' ' | ||
}) | ||
return rows.join( "\n" + sep + "\n" ) | ||
} | ||
return rows.join( "\n" + sep + "\n" ) | ||
} | ||
// Constructor | ||
matrix = [] | ||
// Constructor | ||
matrix = [] | ||
// Sanity checks | ||
if ( str_m == str_n ) | ||
return this.distance = 0 | ||
else if ( str_m == '' ) | ||
return this.distance = str_n.length | ||
else if ( str_n == '' ) | ||
return this.distance = str_m.length | ||
else { | ||
// Danger Will Robinson | ||
previous = [ 0 ] | ||
Array.forEach( str_m, function( v, i ) { i++, previous[ i ] = i } ) | ||
// Sanity checks | ||
if ( str_m == str_n ) | ||
return this.distance = 0 | ||
else if ( str_m == '' ) | ||
return this.distance = str_n.length | ||
else if ( str_n == '' ) | ||
return this.distance = str_m.length | ||
else { | ||
// Danger Will Robinson | ||
previous = [ 0 ] | ||
forEach( str_m, function( v, i ) { i++, previous[ i ] = i } ) | ||
matrix[0] = previous | ||
Array.forEach( str_n, function( n_val, n_idx ) { | ||
current = [ ++n_idx ] | ||
Array.forEach( str_m, function( m_val, m_idx ) { | ||
m_idx++ | ||
if ( str_m.charAt( m_idx - 1 ) == str_n.charAt( n_idx - 1 ) ) | ||
current[ m_idx ] = previous[ m_idx - 1 ] | ||
else | ||
current[ m_idx ] = Math.min | ||
( previous[ m_idx ] + 1 // Deletion | ||
, current[ m_idx - 1 ] + 1 // Insertion | ||
, previous[ m_idx - 1 ] + 1 // Subtraction | ||
) | ||
matrix[0] = previous | ||
forEach( str_n, function( n_val, n_idx ) { | ||
current = [ ++n_idx ] | ||
forEach( str_m, function( m_val, m_idx ) { | ||
m_idx++ | ||
if ( str_m.charAt( m_idx - 1 ) == str_n.charAt( n_idx - 1 ) ) | ||
current[ m_idx ] = previous[ m_idx - 1 ] | ||
else | ||
current[ m_idx ] = Math.min | ||
( previous[ m_idx ] + 1 // Deletion | ||
, current[ m_idx - 1 ] + 1 // Insertion | ||
, previous[ m_idx - 1 ] + 1 // Subtraction | ||
) | ||
}) | ||
previous = current | ||
matrix[ matrix.length ] = previous | ||
}) | ||
previous = current | ||
matrix[ matrix.length ] = previous | ||
}) | ||
return this.distance = current[ current.length - 1 ] | ||
return this.distance = current[ current.length - 1 ] | ||
} | ||
} | ||
} | ||
// Export | ||
module.exports = Levenshtein | ||
})); |
@@ -1,15 +0,24 @@ | ||
{ "name" : "levenshtein" | ||
, "description" : "Javascript implementation of the L-diggity." | ||
, "author" : "Gianni Chiappetta <gianni@runlevel6.org> (http://gf3.ca)" | ||
, "version" : "1.0.1" | ||
, "main" : "./lib/levenshtein" | ||
, "engines" : [ "node >=0.2.0" ] | ||
, "repository" : | ||
{ "type" : "git" | ||
, "url" : "http://github.com/gf3/Levenshtein.git" | ||
{ | ||
"name": "levenshtein", | ||
"description": "Javascript implementation of the L-diggity.", | ||
"author": "Gianni Chiappetta <gianni@runlevel6.org> (http://gf3.ca)", | ||
"version": "1.0.2", | ||
"main": "./lib/levenshtein", | ||
"engines": [ | ||
"node >=0.2.0" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/gf3/Levenshtein.git" | ||
}, | ||
"license": { | ||
"type": "Public Domain", | ||
"url": "http://github.com/gf3/Levenshtein/raw/master/UNLICENSE" | ||
}, | ||
"devDependencies": { | ||
"expresso": "~0.9.2" | ||
}, | ||
"scripts": { | ||
"test": "expresso test" | ||
} | ||
, "license" : | ||
{ "type" : "Public Domain" | ||
, "url" : "http://github.com/gf3/Levenshtein/raw/master/UNLICENSE" | ||
} | ||
} |
@@ -7,3 +7,5 @@ # Levenshtein | ||
`Levenshtein` also does some neat things like coerce to a number and string approproately. So you can compare Levenshtein objects directly! Not to mention it has specs! | ||
`Levenshtein` also does some neat things like coerce to a number and string | ||
approproately. So you can compare Levenshtein objects directly! Not to mention | ||
it has specs! | ||
@@ -10,0 +12,0 @@ ## API |
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
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
45
4.65%5212
-41.1%1
Infinity%6
-25%111
-50%