/*! * editDistance is a TypeScript port of leven 4.1.0 (https://github.com/sindresorhus/leven). * * MIT License * * Copyright (c) Sindre Sorhus (https://sindresorhus.com) * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * Row of the distance matrix, reused across calls to avoid allocations */ const row: number[] = []; /** * UTF-16 code units of the shorter string, reused across calls */ const characterCodeCache: number[] = []; /** * Calculates the Levenshtein distance between two strings * * Counts UTF-16 code units, so a surrogate pair or a combining mark counts as * the number of code units it occupies. The result does not depend on the * argument order. * * @param firstArg - The first string * @param secondArg - The second string * @returns The minimum number of single code unit insertions, deletions and substitutions */ export const editDistance = (firstArg: string, secondArg: string): number => { if (firstArg === secondArg) { return 0; } // Keep the shorter string in `first` let first = firstArg; let second = secondArg; if (first.length > second.length) { first = secondArg; second = firstArg; } let firstLength = first.length; let secondLength = second.length; // A common suffix does not change the distance while (firstLength > 0 && first.charCodeAt(firstLength - 1) === second.charCodeAt(secondLength - 1)) { firstLength--; secondLength--; } // A common prefix does not change the distance let start = 0; while (start < firstLength && first.charCodeAt(start) === second.charCodeAt(start)) { start++; } firstLength -= start; secondLength -= start; if (firstLength === 0) { return secondLength; } let index = 0; while (index < firstLength) { characterCodeCache[index] = first.charCodeAt(start + index); row[index] = ++index; } let result = 0; let index2 = 0; while (index2 < secondLength) { const secondCharacterCode = second.charCodeAt(start + index2); let diagonal = index2++; result = index2; for (index = 0; index < firstLength; index++) { const substitution = secondCharacterCode === characterCodeCache[index] ? diagonal : diagonal + 1; diagonal = row[index]; result = row[index] = diagonal > result ? substitution > result ? result + 1 : substitution : substitution > diagonal ? diagonal + 1 : substitution; } } // Shrink the reused arrays to this call's length, so they do not keep growing row.length = firstLength; characterCodeCache.length = firstLength; return result; };