UNPKG

@beenotung/tslib

Version:
39 lines (38 loc) 1.4 kB
"use strict"; 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 = (0, memorize_1.memorize)(array_wrapper_1.pop); const wrapArray = (0, 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 = (0, 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((0, exports.edit_distance)(pop(s), pop(t), true) + (last(s) === last(t) ? 0 : 1), (0, exports.edit_distance)(pop(s), t, true) + 1, (0, 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(); };