string-differ
Version:
A Javascript library to compare and transform strings.
31 lines • 1.24 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateEditScript = void 0;
const generateEditScript = ({ store, a, b }) => {
const max = a.length + b.length;
store.initialize(-1);
store.set(-1, 1, 0); // to make sure x for the 0th step is assigned 0
for (let d = 0; d <= max; d++) {
store.initialize(d);
for (let k = -d; k <= d; k += 2) {
let x = d === -k || (d !== k && ((store.get(d - 1, k + 1) ?? -1) > (store.get(d - 1, k - 1) ?? -1)))
? store.get(d - 1, k + 1) // x doesn't change when we move downward
: store.get(d - 1, k - 1) + 1; // x increases by 1 when we move rightward
if (x == null) {
throw new Error("Invalid x coordinate!");
}
let y = x - k;
// diagonal moves
while (a.length > x && b.length > y && a[x] === b[y]) {
x += 1;
y += 1;
}
store.set(d, k, x); // we only care about storing x values
if (x >= a.length && y >= b.length) {
return;
}
}
}
};
exports.generateEditScript = generateEditScript;
//# sourceMappingURL=ses.js.map
;