update-data
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

update-data

Simple data comparsion and composition library

build status npm bundlephobia minzip npm version

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',
  },
];

Package Sidebar

Install

npm i update-data

Weekly Downloads

12

Version

0.0.3

License

MIT

Unpacked Size

11.6 kB

Total Files

15

Last publish

Collaborators

  • ibit-cy