1.1.0 • Published 6 years ago
levenshteiner v1.1.0
Levenshteiner
TL;DR
This is one of the fastest packages to calculate Levenshtein distance available today for the Node.js. It utilizes features of worker_threads module which is released with Node.js 12+. It becomes LTS version of Node on October 2019, so you should be able to use this package in production right now.
Installation
Just run npm install --save levenshteiner or yarn add levenshteiner and you should be ready to go. To use the package in your app, import is as
import levenshteiner form 'levenshteiner'or
const levenshteiner = require('levenshteiner');Usage
This package provides three main methods:
levenshtein(strA: string, strB: string): numberwhich calculates Levenshtein distance between two strings.levenshteinOnArray(givenString: string, dictionary: string[]): { value: string, distance: number} | nullwhich finds the item indictionarywith the lowest distance togivenStringlevenshteinOnArrayAsync(givenString: string, dictionary: string[]): Promise<{value: string, distance: number} | null>which also finds the closest to thegivenStringvalue from thedictionaryarray. However, this method utilizesworker_threadsto parallelize calculations, so performance is significantly better with new threds capability of Node.js
Two last methods will return null in case if dictionary is not an Array of non-zero length all elements of which are strings.
import { levenshtein, levenshteinOnArray, levenshteinOnArrayAsync } from 'levenshteiner';
const possibleValues = [
'Apple',
'Banana',
'Pineaple',
'Watermelon',
];
const inputString = 'Bnana';
const distanceInputToApple = levenshtein(inputString, possibleValues[0]); // distanceInputToApple = 3
const possibleMatch = levenshteinOnArray(inputStringm, possibleValues); // { value: 'Banana', distance: 1 }License
This package is released under the MIT License.