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

cmpstr

v1.0.3

Published

lightweight npm package to calculate string similarity

Downloads

205

Readme

cmpstr

This lightweight npm package can be used to calculate the similarity of strings. It supports both the best known Levenshtein distance and the slightly more accurate Sørensen dice coefficient.

Install

Using Node.js, install the package with the following shell command:

npm install cmpstr

Usage

Load the package into your project:

const cmpstr = require( 'cmpstr' );

Sample of how to use the package in your code:

let str1 = 'kitten';
let str2 = 'sitting';

/**
 * levenshteinDistance
 * expected: 3
 */
let distance = cmpstr.levenshteinDistance( str1, str2 );

/**
 * diceCoefficient
 * expected: 0.3636363636363636
 */
let dice = cmpstr.diceCoefficient( str1, str2 );

/**
 * diceClosest
 * expected: bestest
 */
let closest = cmpstr.diceClosest( 'best', [
  'better', 'bestest', 'well', 'good'
] );

/**
 * levenshteinMatch
 * expected: [
 *   { target: 'bestest', match: 0.5714285714285714 },
 *   { target: 'better', match: 0.5 },
 *   { target: 'well', match: 0.25 },
 *   { target: 'good', match: 0 }
 * ]
 */
let matches = cmpstr.levenshteinMatch( 'best', [
  'better', 'bestest', 'well', 'good'
] );

JavaScript

Using JavaScript load this package by embed this file via jsDelivr:

import cmpstr from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";

Remember: To use import you need to load your JavaScript file as type="module".

API

The npm package cmpstr supports two different methods for determining the similarity of two strings. The Levenshtein distance, as the minimum number of inserting, deleting and replacing operations to convert one string into another, and the Sørensen-Dice coefficient to measure the similarity of two samples.

Learn more about both by visiting these links:

Levenshtein distance

levenshteinDistance( a, b [, flags = null ] )

Calculates the difference between two strings a and b and returns the Levenshtein distance as an integer value.

levenshtein( a, b [, flags = null ] )

Returns the match percentage of two strings a and b. The output value is in the range 0..1 as a floating point number.

levenshteinClosest( str, arr [, flags = null ] )

Returns the best match of the string str against the array arr of passed strings. The function returns the most closely matched string found in the array.

levenshteinMatch( str, arr [, flags = null [, threshold = 0 ] ] )

Calculates the similarity of all strings contained in the array arr according to Levenshtein compared to str and returns an array of all samples sorted by matching in descending order. The threshold specifies the minimum required similarity.

Sørensen-Dice coefficient

diceCoefficient( a, b [, flags = null ] )

This function evaluates the similarity of two given strings a and b as percentage value according to the Sørensen-Dice coefficient and returns the result as floating point number.

diceClosest( str, arr [, flags = null ] )

As another way to find the best match between the string str and a given array arr of samples, this function uses the Sørensen-Dice coefficient. It returns the most matching string as well.

diceMatch( str, arr [, flags = null [, threshold = 0 ] ] )

Calculates the similarity of all strings contained in the array arr according to Sørensen-Dice coefficient compared to str and returns an array of all samples sorted by matching in descending order. The threshold specifies the minimum required similarity.

Flags

Each method can be passed the flags options listed below:

| Flag | Option | | ----- | ------------------------------ | | i | case insensitive | | s | non-whitespace characters only |

Patch notes

1.0.3

  • Add threshold to specify the minimum required similarity

1.0.2

  • Add normalize options i and s
  • Minor fixes

1.0.1

  • Minor fixes

1.0.0

  • Initial release