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

qald

v1.0.2

Published

QWERTY-Adjusted Levenshtein Distance computation on JS

Downloads

4

Readme

QWERTY-Adjusted Levenshtein Distance algorythm on JS

Comes with a handy character-by-character strings comparison module.

Installation & usage

Via npm:

npm i qald

To use on a Node app, import the library using import or require approach:

import QALDistance from 'qald';

const qald = new QALDistance();
console.log(qald.compute('hootmal.com', 'hotmail.com'));

Or use on the web: either import the library as a module or just load it as a regular script:

<script src="dist/qald.min.js"></script>

<script>
    const qald = new QALDistance();
    console.log(qald.compute('hootmal.com', 'hotmail.com'));
</script>

How does this work

Let's take a look on a basic calulation of Levenshtein distance:

some_levenshtein_algo('glum', 'drum');
// Distance: 2

But in some scenarios we don't need just a distance. We want to know, how many typos the user has made and how severe they are. So now let's calulate the distance based on QWERTY (or AZERTY, or any other) keyboard layout:

qald.compute('glum', 'drum');

/* 
Result:
{
    "distance": 8,
    "changes": {
        "0": {
            "chars": [
                "d"
            ],
            "type": "replacement"
        },
        "1": {
            "chars": [
                "r"
            ],
            "type": "replacement"
        }
    },
    "comparison": "<u>d</u><u>r</u>um"
}
*/

For each replacement this algorithm calculates the distance between the keys on the keyboard and increases the resulting distance by that number.

This logic works only for replacements. When the algo detects additions and deletions of characters, the distance remains the same as typical Levenshtein implementation:

qald.compute('mial.com', 'mail.com');

/* 
Result:
{
    "distance": 2,
    "changes": {
        "0": {
            "chars": [
                "a"
            ],
            "type": "insertion"
        },
        "2": {
            "chars": [
                "a"
            ],
            "type": "deletion"
        }
    },
    "comparison": "mil.com"
}
*/

Configuration

You can optionally override some options when initializing the GAP Distance class:

const opts = {
    // Adjust typical misspells for your needs.
    // The keyboard distance between these characters will always end up to 1.
    misspells: {
        "a": "o",
        "e": "a",
    },

    // Pick the right keyboard layout for your user's location.
    // Available layouts are: 
    // — "qwerty" (default)
    // — "azerty"
    // — "йцукен" (cyrillic)
    layout: "qwerty",

    // Tell the library how to output changes (or disable it if you don't need them).
    comparer: {
        enabled: true,

        // Pass a function that returns a wrapped value 
        // or pass false to disable output for a specific type of change.
        // These settings are default.
        tags: {
            addition: (k) => `<u>${k}</u>`,
            deletion: false,
            replacement: (k) => `<u>${k}</u>`
        }
    }
}
const qald = new QALDistance(opts);

Contribution & Support

Feel free to make a PR or to open an issue if you're facing troubles.