Skip to content

Commit

Permalink
chore(release): 1.0.0 [skip ci]
Browse files Browse the repository at this point in the history
# 1.0.0 (2022-04-27)

### Features

* init ([4fa3a85](4fa3a85))
  • Loading branch information
semantic-release-bot committed Apr 27, 2022
1 parent 4fa3a85 commit 77afb0b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 1.0.0 (2022-04-27)


### Features

* init ([4fa3a85](https://github.com/bent10/find-similar/commit/4fa3a85357746c226e22917be4bfbc6dcbdb3ab1))
65 changes: 65 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Finds similar word(s) in a list of words.
*
* ## Install
*
* ```bash
* npm i find-similar
* ```
*
* ## Usage
*
* This package is pure ESM, please read the
* [esm-package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
*
* ```js
* import { findSimilar, didYouMean } from 'find-similar'
*
* console.log(findSimilar('foos', ['bar', 'baz', 'foo']))
* // => ['foo']
*
* // Suggests similar words
* console.log(didYouMean('foos', ['bar', 'baz', 'foo']))
* // => 'Did you mean "foo"?'
* ```
*
* @module
*/
export declare type Options = {
/**
* Maximum levenshtein distance threshold.
*
* @default 3
*/
maxScore?: number;
/**
* The similarity threshold, a number between 0 and 1.
*
* @default 0.5
*/
criteria?: number;
/**
* A string to prepend to the suggested word.
*
* @default ''
*/
prefix?: string;
};
/**
* Finds similar word(s) in a list of words.
*
* @param word - The word to find similar words for.
* @param candidates - An array of words to compare against.
* @param options - Options for the function.
* @returns An array of similar words.
*/
export declare function findSimilar(word: string, candidates: string[], options?: Options): string[];
/**
* Suggests similar words in a list of words.
*
* @param word - The word to find similar words for.
* @param candidates - An array of words to compare against.
* @param options - Options for the function.
* @returns A string of suggested words.
*/
export declare function didYouMean(word: string, candidates: string[], options?: Options): string;
33 changes: 33 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import leven from "leven";
function findSimilar(word, candidates, options = {}) {
let { maxScore = 3 } = options;
const { criteria = 0.5, prefix = "" } = options;
const matches = [];
for (const candidate of candidates) {
const length = Math.max(word.length, candidate.length);
const score = leven(word, candidate);
const similarity = (length - score) / length;
if (similarity >= criteria && score <= maxScore) {
if (score < maxScore) {
maxScore = score;
matches.length = 0;
}
matches.push(prefix + candidate);
}
}
return matches;
}
function didYouMean(word, candidates, options = {}) {
const matches = findSimilar(word, candidates, options);
let message = "Did you mean ";
if (matches.length > 0) {
matches.length > 1 && (message += "one of ");
message += `"${matches.join(", ")}"?`;
return message;
}
return "";
}
export {
didYouMean,
findSimilar
};

0 comments on commit 77afb0b

Please sign in to comment.