npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

levenshtein-lite

v1.0.9

Published

A lightweight implementation of the Levenshtein distance algorithm.

Downloads

2,464

Readme

Levenshtein Lite (Minimum Edit Distance Algorithm) Build Status

A lightweight implementation of the Levenshtein distance algorithm.

Keeping things simple, lightweight, and efficient.

What is this?

The Levenshtein distance, also know as the minimum edit distance, is the amount of characters between two strings that would need to be changed for them to be considered equal. This can also be considered as an approximate string matching algorithm.

Cool beans.. so how is it implemented?

This algorithm is inspired by the dynamic programming approach and tries to conserve as much memory as possible. It's a derivation of Wagner–Fischer algorithm, but instead of using an n x m matrix, memory is conserved by using two auxiliary arrays of size n, which act as columns within the Wagner–Fischer matrix. Here, we take advantage of the fact that the only useful data within the Wagner–Fischer matrix is the previous column and the current column. So the idea here is instead of creating an entire matrix, we only create the previous and current column, and just overwrite them as we traverse through our imaginary matrix. Doing this, we save massive amounts of memory, going from O(nm) memory usage all the way down to O(2n), which is poetic.

Alright Jack, but what's the time and space complexity?

This particular implementation of the Levenshtein distance has an asymptotic upper bound of O(nm) and uses O(n) space.

How fast does it actually run?

See for yourself by checking out some benchmarks.

Installation

Simply install the module as a package through npm.

$ npm install levenshtein-lite --save

Usage

Levenshtein Lite is a simple module; it only consists of the actual edit distance calculation function. Simply import the package and start calculating. Here's an overview on how things work.

When you import the library, you're importing this function directly so the name here is pretty arbitrary. Let's call it levenshtein for now:

levenshtein(< string >a, < string >b, [< number >k], [< bool >p]) - number - Returns the Levenshtein edit distance between the two input strings a and b.

  • a - The first string argument.

  • b - The second string argument.

  • k - An optional distance cap. If the edit distance between the two input strings ever exceed this argument during the calculation, the process will terminate and -1 will be returned. This is helpful for speeding up calculations between strings when you only care about matches within a certain edit distance.

  • p - An optional computational limit flag for partial string matching. When this flag is enabled, the distance of the two strings a and b will only be computed up to the shortest length of the two strings. This allows for native support of partial fuzzy string matching. For example, trying to match com and computer with this flag enabled, the computation would stop after 3 characters and return an edit distance of 0.

Examples

import getDistance from 'levenshtein-lite';

// Find edit distance between two strings
getDistance('summertime', 'spring'); // => 7

// Add a cap to stop calculation if edit distance is too high,
// this process will return -1 the moment we exceed the cap during
// calculation. Helps make things more efficient.
getDistance('summertime', 'spring', 5); // => -1

// Add a computational limit to enable partial string matching.
getDistance('com', 'computer', false, true); // => 0

License

MIT

Copyright (c) 2016 Nick Zuber