@linkdotnet/stringoperations
Advanced tools
Comparing version 0.2.0 to 0.2.5
{ | ||
"name": "@linkdotnet/stringoperations", | ||
"version": "0.2.0", | ||
"version": "0.2.5", | ||
"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", |
@@ -8,2 +8,6 @@ export class Trie { | ||
/** | ||
* Adds a word to the trie | ||
* @param word Word to add to the trie | ||
*/ | ||
public addWord (word: string) { | ||
@@ -23,2 +27,7 @@ let current = this.children | ||
/** | ||
* Check whether a word is contained in the trie | ||
* @param word Word to check whether it exists in the trie | ||
* @returns Returns true when the word is contained, otherwise false | ||
*/ | ||
public contains (word: string): boolean { | ||
@@ -34,2 +43,7 @@ if (word.length === 0) { | ||
/** | ||
* Determines whether this trie starts with the specified character | ||
* @param text Character to compare | ||
* @returns True, when the text matches the beginning of the trie, otherwise false | ||
*/ | ||
public startsWith (text: string): boolean { | ||
@@ -43,2 +57,8 @@ if (text.length === 0) { | ||
/** | ||
* Returns all words in the trie which starts with the given prefix | ||
* @param prefix Starting sequence which all returned words have to match | ||
* @returns All words in the trie which start with the given prefix | ||
*/ | ||
public getWordsWithPrefix (prefix: string): string[] { | ||
@@ -57,2 +77,11 @@ const node = this.findNode(prefix) | ||
/** | ||
* Deletes the key out of the trie. When multiple words matches this key all of them get deleted | ||
* @param key Word to delete | ||
* @remarks If trie contains out of 'Hello', 'Helsinki' and delete('Hel') is called, the trie is empty | ||
*/ | ||
public delete (key: string) { | ||
Trie.deleteInternal(this, key, 0) | ||
} | ||
private createOrGetNode (character: string, children: { [key: string] : Trie }): Trie { | ||
@@ -87,3 +116,3 @@ let node: Trie | ||
private stringToCharArray(prefix: string) { | ||
private stringToCharArray (prefix: string) { | ||
const prefixes: string[] = [] | ||
@@ -108,2 +137,15 @@ for (let i = 0; i < prefix.length; i++) { | ||
} | ||
private static deleteInternal (node: Trie | null, word: string, index: number): boolean { | ||
if (index === word.length) { | ||
node = null | ||
} else { | ||
const char = word[index] | ||
if (node?.children[char] && Trie.deleteInternal(node.children[char], word, index + 1)) { | ||
delete node.children[char] | ||
} | ||
} | ||
return node === null || (node !== null && Object.keys(node.children).length === 0) | ||
} | ||
} |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Computes and returns the largest common subsequence of two strings | ||
* @param one First string | ||
* @param two Second string | ||
* @param ignoreCase If true, the string compares ignoring the case. So 'd' and 'D' would match | ||
* @returns Largest common subsequence | ||
* @remarks If ignoreCase is true, the casing of one will be returned as largest common subsequence. | ||
* If word one is 'HeLlO' and word two is 'Hallo' then 'HLlO' will be returned | ||
*/ | ||
export function getLargestCommonSubsequence (one: string, two: string, ignoreCase = false): string { | ||
@@ -2,0 +11,0 @@ const lcsMatrix = createLargestCommonSubsequenceMatrix(one, two, ignoreCase) |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Gets the largest common substring of two given strings | ||
* @param one First word | ||
* @param two Second word | ||
* @param ignoreCase If true, the string compares ignoring the case. So 'd' and 'D' would match | ||
* @returns Largest common substring | ||
* @remarks If ignoreCase is true, the casing of one will be returned as largest common substring. | ||
* If word one is 'typeSCRIPT' and word two is 'JAVAscript' then 'SCRIPT' will be returned | ||
*/ | ||
export function getLargestCommonSubstring (one: string, two: string, ignoreCase = false) { | ||
@@ -2,0 +11,0 @@ const lcsMatrix = getLargestCommonSubstringMatrix(one, two, ignoreCase) |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Calculates the Levenshtein distance of two given strings | ||
* @param one First word | ||
* @param two Second word | ||
* @param ignoreCase Ignore case, when comparing each character | ||
* @param substitutionCost Cost of a substitution. Per default set to 1 | ||
* @param abortCost If abortCost is met, the function will immediately return (with abortCost as return value) | ||
* @returns Levenshtein distance | ||
*/ | ||
export function getLevenshteinDistance (one: string, two: string, ignoreCase = false, substitutionCost = 1, abortCost = Number.MAX_SAFE_INTEGER): number { | ||
@@ -2,0 +11,0 @@ if (one.length === 0) { |
@@ -51,2 +51,21 @@ import { Trie } from '../../src/data-structure/trie' | ||
}) | ||
it('should delete word in trie', () => { | ||
const trie = new Trie() | ||
trie.addWord('Hello') | ||
trie.addWord('Melone') | ||
trie.delete('Hello') | ||
expect(false).toBe(trie.contains('Hello')) | ||
}) | ||
it('should only delete word in trie when whole sequence matches', () => { | ||
const trie = new Trie() | ||
trie.addWord('Hello') | ||
trie.delete('Hallo') | ||
expect(true).toBe(trie.contains('Hello')) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25612
486