You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 lines
684 B
JavaScript

3 years ago
"use strict";
// TODO: Can probably be made more performant by precomputing a key -> item mapping, at the cost of multiple items with the same key becoming impossible
module.exports = function diffLists(oldArray, newArray, keyFunction = (item) => item) {
// NOTE: This only detects additions and removals, *not* order changes! The order is not relevant for our usecase.
let oldSet = new Set(oldArray.map((item) => keyFunction(item)));
let newSet = new Set(newArray.map((item) => keyFunction(item)));
let removed = oldArray.filter((item) => !newSet.has(keyFunction(item)));
let added = newArray.filter((item) => !oldSet.has(keyFunction(item)));
return { removed, added };
};