update-data
v0.0.3
Published
Simple data comparsion and composition library
Downloads
23
Maintainers
Readme
update-data
Simple data comparsion and composition library
Install
# npm
npm i update-data
# yarn
yarn add update-data
Usage
updateList
Updates array with a new array by match, and compete functions between records
function updateList<T>(
listOld: T[],
listNew: T[],
matchFn: (itemOld: T, nitemNew: T) => boolean,
competeFn?: (itemOld: T, itemNew: T) => boolean,
): T[];
Example
const docs = [
{ id: '1', title: 'Document 1', status: 'old', updated: new Date(1000) },
{ id: '2', title: 'Document 2', status: 'old', updated: new Date(2000) },
];
const update = [
{ id: '1', title: 'Document 1', status: 'upd', updated: new Date(3000) },
{ id: '3', title: 'Document 3', status: 'new', updated: new Date(4000) },
];
console.log(
updateList(
docs,
update,
(docOld, docNew) => docNew.id === docOld.id,
(docOld, docNew) => docNew.updated.getTime() > docOld.updated.getTime(),
),
);
Result
[
{
id: '1',
title: 'Document 1',
status: 'upd', // <--- New value
updated: '1970-01-01T00:00:03.000Z', // <--- New value
},
{
id: '2',
title: 'Document 2',
status: 'old',
updated: '1970-01-01T00:00:02.000Z',
},
// New record
{
id: '3',
title: 'Document 3',
status: 'new',
updated: '1970-01-01T00:00:04.000Z',
},
];