UNPKG

fast-array-diff

Version:

Implementation of paper 'An O(ND) Difference Algorithm and Its Variations' on array

51 lines (50 loc) 1.76 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyPatch = void 0; function applyPatch(a, patch) { var _a; var segments = []; var sameStart = 0; for (var i = 0; i < patch.length; ++i) { var patchItem = patch[i]; sameStart !== patchItem.oldPos && segments.push(a.slice(sameStart, patchItem.oldPos)); if (patchItem.type === 'add') { segments.push(patchItem.items); sameStart = patchItem.oldPos; } else if (patchItem.items) { sameStart = patchItem.oldPos + patchItem.items.length; } else { sameStart = patchItem.oldPos + patchItem.length; } } sameStart !== a.length && segments.push(a.slice(sameStart)); return (_a = []).concat.apply(_a, __spreadArray([], __read(segments), false)); } exports.applyPatch = applyPatch;