Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

levenshtein-search

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

levenshtein-search - npm Package Compare versions

Comparing version 0.0.2 to 0.1.0

54

levenshtein-search.js

@@ -32,54 +32,4 @@ const { searchExact, reverse } = require('./utils')

if (maxDist * 5 >= a.length) {
return editDistance(a, b) <= maxDist
}
// use n-gram search
const ngramLen = Math.floor(a.length / (maxDist + 1))
for (let ngramStartIdx = 0; ngramStartIdx <= a.length - ngramLen; ngramStartIdx += ngramLen) {
const ngram = a.slice(ngramStartIdx, ngramStartIdx + ngramLen)
for (const bMatchIdx of searchExact(ngram, b)) {
let back
const maxBack = Math.min(ngramStartIdx, bMatchIdx)
for (back = 0; back < maxBack; back++) {
if (a[ngramStartIdx - (back + 1)] !== b[bMatchIdx - (back + 1)]) {
break
}
}
let forward
const maxForward = Math.min(
a.length - (ngramStartIdx + ngramLen),
b.length - (bMatchIdx + ngramLen)
)
for (forward = 0; forward < maxForward; forward++) {
if (
a[ngramStartIdx + ngramLen + (forward + 1)] !==
b[bMatchIdx + ngramLen + (forward + 1)]
) {
break
}
}
const aBefore = a.slice(0, ngramStartIdx - back)
const aAfter = a.slice(ngramStartIdx + ngramLen + forward)
const bBefore = b.slice(0, bMatchIdx - back)
const bAfter = b.slice(bMatchIdx + ngramLen + forward)
if (Math.min(aBefore.length, bBefore.length) < Math.min(aAfter.length, bAfter.length)) {
const dist = editDistance(aBefore, bBefore)
if (dist <= maxDist && isEditDistanceNoGreaterThan(aAfter, bAfter, maxDist - dist)) {
return true
}
} else {
const dist = editDistance(aAfter, bAfter)
if (dist <= maxDist && isEditDistanceNoGreaterThan(aBefore, bBefore, maxDist - dist)) {
return true
}
}
}
}
return false
const [ dist, length ] = _expand(a, b, maxDist)
return dist + (b.length - length) <= maxDist
}

@@ -86,0 +36,0 @@

4

package.json

@@ -25,7 +25,7 @@ {

"scripts": {
"check-only": "! grep 'test.only' tests/test.js -n",
"check-only": "grep -c \"test.only\" test.js | python -c \"import sys; sys.exit(0 if int(sys.stdin.read()) == 0 else 1)\"",
"lint": "standard",
"test": "npm run lint && npm run check-only && jest"
},
"version": "0.0.2"
"version": "0.1.0"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc