changelog-guru
Version:
Git changelog generator
29 lines (28 loc) • 985 B
JavaScript
const MAX_DIFF_PERCENT = 0.2;
const MAX_DIFF_DISTANCE = 1;
export const unify = (key) => key.trim().toLowerCase();
export const isSame = (a, b) => {
const ax = unify(a);
const bx = unify(b);
if (ax === bx)
return true;
if (Math.abs(ax.length - bx.length) > MAX_DIFF_DISTANCE)
return false;
let i = 0;
let j = 0;
let m;
let n;
const mx = [];
const distance = () => Math.min(mx[m][n] + 1, Math.min(mx[i][n] + 1, mx[m][j] + 1));
while (i <= bx.length)
mx[i] = [i++];
while (j <= ax.length)
mx[0][j] = j++;
for (i = 1, m = 0; i <= bx.length; i++, m++) {
for (j = 1, n = 0; j <= ax.length; j++, n++) {
mx[i][j] = (bx.charAt(m) === ax.charAt(n) ? mx[m][n] : distance());
}
}
return mx[bx.length][ax.length] <= MAX_DIFF_DISTANCE || mx[bx.length][ax.length] / ax.length <= MAX_DIFF_PERCENT;
};
export const findSame = (key, items) => items.find(item => isSame(item, key));