Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
didyoumean2
Advanced tools
a library for matching human-quality input to a list of potential matches using the Levenshtein distance algorithm
The didyoumean2 npm package is a utility for suggesting corrections to misspelled words or phrases. It is particularly useful for command-line interfaces, chatbots, and other applications where user input may contain typos. The package uses a combination of string similarity algorithms to provide the best possible suggestions.
Basic Usage
This feature allows you to provide a list of possible correct words and get suggestions based on the input. In this example, 'appl' is corrected to 'apple'.
const didYouMean = require('didyoumean2');
const input = 'appl';
const suggestions = didYouMean(input, ['apple', 'banana', 'grape']);
console.log(suggestions); // Output: ['apple']
Custom Threshold
This feature allows you to set a custom threshold for the similarity score. The threshold determines how close the input needs to be to the suggestions. In this example, a threshold of 0.4 is used.
const didYouMean = require('didyoumean2');
const input = 'appl';
const options = { threshold: 0.4 };
const suggestions = didYouMean(input, ['apple', 'banana', 'grape'], options);
console.log(suggestions); // Output: ['apple']
Multiple Suggestions
This feature allows you to get multiple suggestions instead of just the best match. In this example, both 'apple' and 'applet' are returned as suggestions for 'appl'.
const didYouMean = require('didyoumean2');
const input = 'appl';
const options = { returnType: 'all-matches' };
const suggestions = didYouMean(input, ['apple', 'applet', 'banana', 'grape'], options);
console.log(suggestions); // Output: ['apple', 'applet']
The 'leven' package calculates the Levenshtein distance between two strings, which is a measure of the difference between them. It is useful for finding the closest match to a given input but does not provide a built-in suggestion mechanism like didyoumean2.
Fuse.js is a powerful, lightweight fuzzy-search library that allows for flexible and advanced searching capabilities. It can be used to find approximate string matches within a list of items, similar to didyoumean2, but offers more advanced search options and configurations.
The 'string-similarity' package finds the degree of similarity between two strings, based on the Dice's Coefficient. It can be used to find the closest match to a given input but does not provide a built-in suggestion mechanism like didyoumean2.
didyoumean2
is a library for matching human-quality input to a list of potential matches using the Levenshtein distance algorithm.
It is inspired by didyoumean.js.
Based on fastest-levenshtein, the fastest JS implementation of the Levenshtein distance algorithm
~100% faster than didyoumean.js
Well tested with 100% coverage
Static type checking with TypeScript
More control on what kind of matches you want to return
Support matching object's path
instead of just key
npm install didyoumean2
const didYouMean = require('didyoumean2').default
// or if you are using TypeScript or ES module
import didYouMean from 'didyoumean2'
// you can also access to Enums via:
const {
default: didYouMean,
ReturnTypeEnums,
ThresholdTypeEnums,
} = require('didyoumean2')
// or
import didYouMean, { ReturnTypeEnums, ThresholdTypeEnums } from 'didyoumean2'
We are using corepack to manage the yarn
version
corepack enable
didYouMean(input, matchList[, options])
input {string}
: A string that you are not sure and want to match with matchList
matchList {Object[]|string[]}
: A List for matching with input
options {Object}
(optional): An options that allows you to modify the behavior
@return {Array|null|Object|string}
: A list of or single matched result(s), return object if match
is {Object[]}
caseSensitive {boolean}
default: false
Perform case-sensitive matching
deburr {boolean}
default: true
Perform combining diacritical marks insensitive matching
Refer to lodash _.deburr for how it works
matchPath {Array}
default: []
If your matchList
is an array of object, you must use matchPath
to point to the string that you want to match
Refer to ramda R.path for how to define the path, e.g. ['obj', 'array', 0, 'key']
returnType {string}
ReturnTypeEnums.FIRST_CLOSEST_MATCH
returnType | Description |
---|---|
ReturnTypeEnums.ALL_CLOSEST_MATCHES | Return all matches with the closest value to the input in array |
ReturnTypeEnums.ALL_MATCHES | Return all matches in array |
ReturnTypeEnums.ALL_SORTED_MATCHES | Return all matches in array, sorted from closest to furthest |
ReturnTypeEnums.FIRST_CLOSEST_MATCH | Return first match from ReturnTypeEnums.ALL_CLOSEST_MATCHES |
ReturnTypeEnums.FIRST_MATCH | Return first match (FASTEST) |
threshold {integer|number}
depends on thresholdType
type: {number}
(similarity
) or {integer}
(edit-distance
)
default: 0.4
(similarity
) or 20
(edit-distance
)
If the result is larger (similarity
) or smaller (edit-distance
) than or equal to the threshold
, that result is matched
thresholdType {string}
ThresholdTypeEnums.SIMILARITY
thresholdType | Description |
---|---|
ThresholdTypeEnums.EDIT_DISTANCE | Refer to Levenshtein distance algorithm, must be integer , lower value means more similar |
ThresholdTypeEnums.SIMILARITY | l = max(input.length, matchItem.length), similarity = (l - editDistance) / l , number from 0 to 1 , higher value means more similar |
trimSpaces {boolean}
default: true
Remove noises when matching
Trim all starting and ending spaces, and concatenate all continuous spaces to one space
Before all:
npm install -g yarn
yarn install
Unit test and coverage:
yarn test
Linter:
yarn lint
FAQs
a library for matching human-quality input to a list of potential matches using the Levenshtein distance algorithm
The npm package didyoumean2 receives a total of 97,524 weekly downloads. As such, didyoumean2 popularity was classified as popular.
We found that didyoumean2 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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.