@beenotung/tslib
Version:
utils library in Typescript
40 lines • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.edit_distance = void 0;
const array_wrapper_1 = require("./array-wrapper");
const memorize_1 = require("./memorize");
// const last = memorize(_last);
const last = array_wrapper_1.wrappedLast;
const pop = memorize_1.memorize(array_wrapper_1.pop);
const wrapArray = memorize_1.memorize(array_wrapper_1.wrapArray);
/**
* @description this can consume lot of memory, need to manually invoke edit_distance.clear() to free the caches
* */
exports.edit_distance = memorize_1.memorize((s, t, loop) => {
if (s.length === 0) {
return t.length;
}
if (t.length === 0) {
return s.length;
}
if (loop) {
s = s;
t = t;
}
else {
s = typeof s === 'string' || Array.isArray(s) ? wrapArray(s) : s;
t = typeof t === 'string' || Array.isArray(t) ? wrapArray(t) : t;
}
return Math.min(exports.edit_distance(pop(s), pop(t), true) + (last(s) === last(t) ? 0 : 1), exports.edit_distance(pop(s), t, true) + 1, exports.edit_distance(s, pop(t), true) + 1);
});
const edit_distance_clear = exports.edit_distance.clear.bind(exports.edit_distance);
exports.edit_distance.clear = () => {
if (typeof last.clear === 'function') {
;
last.clear();
}
pop.clear();
wrapArray.clear();
edit_distance_clear();
};
//# sourceMappingURL=edit-distance.js.map