levenshtein-distance.js

/**
 * Calculate the levenshtein distance between two strings.
 * @param {string} a - The first string to compare.
 * @param {string} b - The second string to compare.
 * @returns {number|*} - The percentage of the levenshtein distance.
 */
function levenshteinDistance(a, b) {
    if (a.length === 0) return b.length;
    if (b.length === 0) return a.length;

    let matrix = [];

    // increment along the first column of each row
    let i;
    for (i = 0; i <= b.length; i++) {
        matrix[i] = [i];
    }

    // increment each column in the first row
    let j;
    for (j = 0; j <= a.length; j++) {
        matrix[0][j] = j;
    }

    // Fill in the rest of the matrix
    for (i = 1; i <= b.length; i++) {
        for (j = 1; j <= a.length; j++) {
            if (b.charAt(i - 1) === a.charAt(j - 1)) {
                matrix[i][j] = matrix[i - 1][j - 1];
            } else {
                matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
                    Math.min(matrix[i][j - 1] + 1, // insertion
                        matrix[i - 1][j] + 1)); // deletion
            }
        }
    }

    // return the percentage of the levenshtein distance
    return (1 - (matrix[b.length][a.length] / Math.max(a.length, b.length))) * 100
}

// Expose to the global scope
if (globalThis.window !== undefined) {
    globalThis.levenshteinDistance = levenshteinDistance;
}

module.exports = levenshteinDistance;