@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
35 lines • 1.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function diff(value, oldKeyMap, keyfunc) {
var additions = [];
var deletions = [];
var moves = [];
var newKeyMap = Object.entries(value).reduce(function (map, _a) {
var index = _a[0], item = _a[1];
var _key = keyfunc(item);
map[_key] = { index: index, item: item };
if (!(_key in oldKeyMap))
additions.push({ index: index, item: item });
return map;
}, {});
Object.entries(oldKeyMap).forEach(function (_a) {
var _key = _a[0], entry = _a[1];
if (!(_key in newKeyMap))
deletions.push(entry);
else {
var _newEntry = newKeyMap[_key];
if (_newEntry.index != entry.index)
moves.push({
oldIndex: entry.index,
newIndex: _newEntry.index,
item: entry.item
});
}
});
return {
changes: { additions: additions, deletions: deletions, moves: moves },
newKeyMap: newKeyMap,
};
}
exports.diff = diff;
//# sourceMappingURL=keyed-array-diff.js.map