arrays-otherness
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

⚡ Arrays otherness

Version Small size Testing Building

Get and handle a matching, missing and excess items of an array based on target array

.

  • Install

npm i arrays-otherness
  • Usage

arraysOtherness<T>(
    // Array for checking
    originArray: T[], 
    // Etalon array
    targetArray: T[], 
    // Function of matching arrays items
    matchFn: (res) => boolean
): {
    // Execute callback if items in two arrays
    match: (callback: (res) => void) => this,
    // Execute callback if target item missing in origin array
    missing: (callback: (res) => void) => this,
    // Execute callback if origin item is excess in target array
    excess: (callback: (res) => void) => this,
}
  • Example

const arraysOtherness = require('arrays-otherness');

const originArray = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Sam' },
    { id: 3, name: 'Nick' },
];

const targetArray = [
    { id: 1, name: 'Jonny' },
    { id: 3, name: 'Nick' },
    { id: 4, name: 'Lisa' },
    { id: 5, name: 'Frank' },
];

arraysOtherness(originArray, targetArray, ({ origin, target }) => (origin.id === target.id))
    .match(({ target, origin }) => {
        console.log('matched =', target, 'and', origin);
        
        // matched = { id: 1, name: 'John' } and { id: 1, name: 'Jonny' }
        // matched = { id: 3, name: 'Nick' } and { id: 3, name: 'Nick' }
    })
    .missing(({ target }) => {
        console.log('to create =', target);

        // to create = { id: 4, name: 'Lisa' }
        // to create = { id: 5, name: 'Frank' }
    })
    .excess(({ origin }) => {
        console.log('to remove =', origin);

        // to remove = { id: 2, name: 'Sam' }
    });

Dependencies (0)

    Dev Dependencies (7)

    Package Sidebar

    Install

    npm i arrays-otherness

    Weekly Downloads

    9

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    10.6 kB

    Total Files

    13

    Last publish

    Collaborators

    • neki-development