@linkdotnet/stringoperations
Advanced tools
Comparing version 0.3.8 to 0.4.0
@@ -0,1 +1,3 @@ | ||
export declare function getClosestWord(base: string, ignoreCase: boolean, words: string[]): string | undefined; | ||
export declare function getClosestWords(base: string, count: number, ignoreCase: boolean, words: string[]): string[]; | ||
/** | ||
@@ -2,0 +4,0 @@ * Computes and returns the longest common subsequence of two strings |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getLongestCommonSubsequence = void 0; | ||
exports.getLongestCommonSubsequence = exports.getClosestWords = exports.getClosestWord = void 0; | ||
function getClosestWord(base, ignoreCase, words) { | ||
const closestWords = getClosestWords(base, 1, ignoreCase, words); | ||
if (closestWords.length === 0) { | ||
return undefined; | ||
} | ||
return closestWords[0]; | ||
} | ||
exports.getClosestWord = getClosestWord; | ||
function getClosestWords(base, count, ignoreCase, words) { | ||
if (base.length === 0) { | ||
return []; | ||
} | ||
if (!words || words.length === 0) { | ||
return []; | ||
} | ||
const wordToSimiliarity = {}; | ||
for (let i = 0; i < words.length; i++) { | ||
wordToSimiliarity[words[i]] = getLongestCommonSubsequence(base, words[i], ignoreCase).length; | ||
} | ||
const sorted = Object.entries(wordToSimiliarity).sort((a, b) => { | ||
return b[1] - a[1]; | ||
}); | ||
return sorted.map(v => v[0]).slice(0, count); | ||
} | ||
exports.getClosestWords = getClosestWords; | ||
/** | ||
@@ -5,0 +30,0 @@ * Computes and returns the longest common subsequence of two strings |
export { getLongestCommonSubstring } from './edit-distance/longest-common-substring'; | ||
export { getLongestCommonSubsequence } from './edit-distance/longest-common-subsequence'; | ||
export { getLongestCommonSubsequence, getClosestWord, getClosestWords } from './edit-distance/longest-common-subsequence'; | ||
export { getLevenshteinDistance } from './edit-distance/levenshtein'; | ||
@@ -4,0 +4,0 @@ export { getHammingDistance } from './edit-distance/hamming-distance'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.findAll = exports.contains = exports.Rope = exports.Trie = exports.getHammingDistance = exports.getLevenshteinDistance = exports.getLongestCommonSubsequence = exports.getLongestCommonSubstring = void 0; | ||
exports.findAll = exports.contains = exports.Rope = exports.Trie = exports.getHammingDistance = exports.getLevenshteinDistance = exports.getClosestWords = exports.getClosestWord = exports.getLongestCommonSubsequence = exports.getLongestCommonSubstring = void 0; | ||
var longest_common_substring_1 = require("./edit-distance/longest-common-substring"); | ||
@@ -8,2 +8,4 @@ Object.defineProperty(exports, "getLongestCommonSubstring", { enumerable: true, get: function () { return longest_common_substring_1.getLongestCommonSubstring; } }); | ||
Object.defineProperty(exports, "getLongestCommonSubsequence", { enumerable: true, get: function () { return longest_common_subsequence_1.getLongestCommonSubsequence; } }); | ||
Object.defineProperty(exports, "getClosestWord", { enumerable: true, get: function () { return longest_common_subsequence_1.getClosestWord; } }); | ||
Object.defineProperty(exports, "getClosestWords", { enumerable: true, get: function () { return longest_common_subsequence_1.getClosestWords; } }); | ||
var levenshtein_1 = require("./edit-distance/levenshtein"); | ||
@@ -10,0 +12,0 @@ Object.defineProperty(exports, "getLevenshteinDistance", { enumerable: true, get: function () { return levenshtein_1.getLevenshteinDistance; } }); |
{ | ||
"name": "@linkdotnet/stringoperations", | ||
"version": "0.3.8", | ||
"version": "0.4.0", | ||
"description": "Collection of string utilities. Edit-Distances, Search and Data structures. Offers for example trie, levenshtein distance.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -43,2 +43,11 @@ # String Operations for TypeScript | ||
With the longest common subsequence we can also determine the most similar word. | ||
This helps for example if you want to find out the closest word to an user given input | ||
```ts | ||
import { getClosestWord } from '@linkdotnet/stringoperations' | ||
const closestWord = getClosestWord(userInput, false, ['...']) | ||
console.log(`Did you mean ${closestWord} instead of ${userInput}?') | ||
``` | ||
### Search | ||
@@ -45,0 +54,0 @@ If you want to find all occurrences of a string use the `findAll` method. The function implements the Boyer-Moore algorithm with Bad-Character table. |
35386
710
72