Javascript: Get New/Changed/Deleted items of lists

Date: 2019-01-18
const getNew = (oldList, newList, getId) => {
        return newList.filter(i => !oldList.some(o => getId(o) === getId(i)));
    };
    const getChanged = (oldList, newList, getId, getLastModified) => {
        return newList.filter(i => {
            const old = oldList.filter(o => getId(o) == getId(i))[0];
            if (old && getLastModified(i) > getLastModified(old)) {
                return true;
            }
        });
    };
    // deleted items is just the lists reversed
    const getDeleted = (oldList, newList, getId) => {
        return getNew(newList, oldList, getId);
    };

    const getId = (item) => {
        return item.id;
    };

    const getLastModified = (item) => {
        return item.lastModified;
    };
    
    const dateAdd = function (date, ms) {
        return new Date(date.getTime() + ms);
    };

    const run = () => {
        let newDate = new Date();
    
        let oldList = [
            { id: 1, lastModified: newDate },
            { id: 2, lastModified: newDate },
            { id: 3, lastModified: newDate },
            { id: 4, lastModified: newDate },
            { id: 5, lastModified: newDate },
        ];
        let newList = [            
            { id: 6, lastModified: newDate },
            { id: 2, lastModified: newDate },
            { id: 3, lastModified: dateAdd(newDate, 10000) },
            { id: 5, lastModified: newDate },
            { id: 7, lastModified: newDate },
        ];
        
        console.log('new', getNew(oldList, newList, getId));
        console.log('changed', getChanged(oldList, newList, getId, getLastModified));
        console.log('deleted', getDeleted(oldList, newList, getId));
    };    
    run();
18190cookie-checkJavascript: Get New/Changed/Deleted items of lists