Skip to content

Commit

Permalink
feat: v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arugaz committed Sep 22, 2023
0 parents commit d93bf9f
Show file tree
Hide file tree
Showing 8 changed files with 1,918 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "standard-with-typescript",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"@typescript-eslint/strict-boolean-expressions": "off"
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules/
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 hidden-finder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "@hidden-finder/didyoumean",
"version": "1.0.0",
"description": "A simple and lightweight matching input to a list of potential matches using the Levenshtein distance algorithm.",
"type": "module",
"source": "./src/index.ts",
"main": "./dist/index.cjs",
"umd:main": "./dist/index.umd.cjs",
"module": "./dist/index.js",
"exports": {
"require": "./dist/index.cjs",
"default": "./dist/index.modern.js"
},
"types": "./dist/index.d.ts",
"scripts": {
"prepublish": "npm run lint",
"lint": "eslint \"./src/**/*.ts\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/hidden-finder/didyoumean.git"
},
"keywords": [
"compare",
"comparison",
"didyoumean",
"diff",
"difference",
"distance",
"edit",
"find",
"fuzzy",
"leven",
"levenshtein",
"match",
"matching",
"similar",
"similarity",
"string"
],
"author": "hidden-finder",
"license": "MIT",
"bugs": {
"url": "https://github.com/hidden-finder/didyoumean/issues"
},
"homepage": "https://github.com/hidden-finder/didyoumean",
"files": [
"dist"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-standard-with-typescript": "^34.0.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"typescript": "^4.9.0"
}
}
125 changes: 125 additions & 0 deletions src/calculate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
export const calculateMyersDistanceShort = (text: string, pattern: string): number => {
const textLength = text.length
const patternLength = pattern.length
const lastSetBit = 1 << (textLength - 1)
const patternEqualityArray = new Uint32Array(65536)

let positiveVector = -1
let negativeVector = 0
let point = textLength

for (let i = textLength - 1; i >= 0; i--) {
patternEqualityArray[text.charCodeAt(i)] |= 1 << i
}

for (let i = 0; i < patternLength; i++) {
let equality = patternEqualityArray[pattern.charCodeAt(i)]
const mixedVector = equality | negativeVector

equality |= ((equality & positiveVector) + positiveVector) ^ positiveVector
negativeVector |= ~(equality | positiveVector)
positiveVector &= equality

if (negativeVector & lastSetBit) {
point++
}
if (positiveVector & lastSetBit) {
point--
}

negativeVector = (negativeVector << 1) | 1
positiveVector = (positiveVector << 1) | ~(mixedVector | negativeVector)
negativeVector &= mixedVector
}

return point
}

export const calculateMyersDistanceLong = (text: string, pattern: string): number => {
const textLength = text.length
const patternLength = pattern.length
const matchHighBits = new Array<number>(Math.ceil(textLength / 32))
const mismatchHighBits = new Array<number>(Math.ceil(textLength / 32))
const patternEqualityArray = new Uint32Array(65536)

for (let i = 0; i < matchHighBits.length; i++) {
matchHighBits[i] = 0
mismatchHighBits[i] = -1
}

for (let j = 0; j < Math.ceil(patternLength / 32); j++) {
let negativeVector = 0
let positiveVector = -1
const start = j * 32
const vectorLength = Math.min(32, patternLength - start) + start

for (let k = start; k < vectorLength; k++) {
patternEqualityArray[pattern.charCodeAt(k)] |= 1 << k
}

for (let i = 0; i < textLength; i++) {
const equality = patternEqualityArray[text.charCodeAt(i)]
const patternBit = (matchHighBits[(i / 32) | 0] >>> i) & 1
const textBit = (mismatchHighBits[(i / 32) | 0] >>> i) & 1
const mixedVector = equality | negativeVector

const mixedHighBit = ((((equality | textBit) & positiveVector) + positiveVector) ^ positiveVector) | equality | textBit
let patternHighBit = negativeVector | ~(mixedHighBit | positiveVector)
let textHighBit = positiveVector & mixedHighBit

if ((patternHighBit >>> 31) ^ patternBit) {
matchHighBits[(i / 32) | 0] ^= 1 << i
}
if ((textHighBit >>> 31) ^ textBit) {
mismatchHighBits[(i / 32) | 0] ^= 1 << i
}

patternHighBit = (patternHighBit << 1) | patternBit
textHighBit = (textHighBit << 1) | textBit
positiveVector = textHighBit | ~(mixedVector | patternHighBit)
negativeVector = patternHighBit & mixedVector
}

for (let k = start; k < vectorLength; k++) {
patternEqualityArray[pattern.charCodeAt(k)] = 0
}
}

let negativeVector = 0
let positiveVector = -1
const start = Math.ceil(patternLength / 32) * 32
const vectorLength = Math.min(32, patternLength - start) + start

for (let k = start; k < vectorLength; k++) {
patternEqualityArray[pattern.charCodeAt(k)] |= 1 << k
}

let point = patternLength
for (let i = 0; i < textLength; i++) {
const equality = patternEqualityArray[text.charCodeAt(i)]
const patternBit = (matchHighBits[(i / 32) | 0] >>> i) & 1
const textBit = (mismatchHighBits[(i / 32) | 0] >>> i) & 1
const mixedVector = equality | negativeVector

const mixedHighBit = ((((equality | textBit) & positiveVector) + positiveVector) ^ positiveVector) | equality | textBit
let patternHighBit = negativeVector | ~(mixedHighBit | positiveVector)
let textHighBit = positiveVector & mixedHighBit

point += (patternHighBit >>> (patternLength - 1)) & 1
point -= (textHighBit >>> (patternLength - 1)) & 1

if ((patternHighBit >>> 31) ^ patternBit) {
matchHighBits[(i / 32) | 0] ^= 1 << i
}
if ((textHighBit >>> 31) ^ textBit) {
mismatchHighBits[(i / 32) | 0] ^= 1 << i
}

patternHighBit = (patternHighBit << 1) | patternBit
textHighBit = (textHighBit << 1) | textBit
positiveVector = textHighBit | ~(mixedVector | patternHighBit)
negativeVector = patternHighBit & mixedVector
}

return point
}
38 changes: 38 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { calculateMyersDistanceShort, calculateMyersDistanceLong } from './calculate'

export const calculateDistance = (text: string, pattern: string): number => {
if (text.length < pattern.length) [pattern, text] = [text, pattern]

if (pattern.length === 0) return text.length

if (text.length === 0) return pattern.length

if (text.length <= 32) return calculateMyersDistanceShort(text, pattern)

return calculateMyersDistanceLong(text, pattern)
}

export const similarity = (text: string, pattern: string): number => {
if (!text || !pattern) return 0
if (text === pattern) return 1

const editDistance = calculateDistance(text, pattern)
const longestLength = Math.max(text.length, pattern.length)

return (longestLength - editDistance) / longestLength
}

export const didyoumean = (string: string, patterns: string[]): string => {
let minDistance = Infinity
let minIndex = 0

for (let i = 0; i < patterns.length; i++) {
const distance = calculateDistance(string, patterns[i])
if (distance < minDistance) {
minDistance = distance
minIndex = i
}
}

return patterns[minIndex]
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"baseUrl": "./",
"declaration": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"outDir": "./dist",
"removeComments": false,
"sourceMap": false,
"strict": true,
"target": "esnext"
},
"exclude": ["node_modules"],
"include": ["src/**/*"]
}
Loading

0 comments on commit d93bf9f

Please sign in to comment.