
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
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.
Using Node.js install the package using shell command:
npm install cmpstr
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'
] );
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:
levenshteinDistance( a, b )
Calculates the difference between two strings a
and b
and returns the Levenshtein distance as an integer value.
levenshtein( a, b )
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 )
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 )
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.
diceCoefficient( a, b )
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 )
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 )
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.
FAQs
CmpStr is a lightweight, fast and well performing package for calculating string similarity
The npm package cmpstr receives a total of 463 weekly downloads. As such, cmpstr popularity was classified as not popular.
We found that cmpstr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.