npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crud-object-diff

v2.3.6

Published

Helper utilities to compare objects or arrays for obtaining created, updated, & deleted values.

Downloads

2,134

Readme

CRUD Compare - JS State Comparison Helper

npm npm bundle size Coverage GitHub Dependencies PRs Welcome CircleCI

This is a very ✌ lightweight and ⚡️ fast library for comparing objects, arrays, and arrays of objects to get the Created, Updated, & Deleted values (helpful for state comparisons).

Install

npm install crud-object-diff

Usage

Comparing Arrays of Objects

  • Method: compareObjectVals

  • Parameters: toCompareVals: [Object[], Object[]], key?: string|string[]

  • Returns: { createdVals: Object[]|null, updatedVals: Object[]|null, deletedVals: Object[]|null }

  • Provides: Created, Updated, and Deleted values (via separate arrays) from comparing two arrays of objects.

    • It is recommended to provide a related unique key (i.e. primary key on data) between the objects to be compared using the compareObjectVals function. A single or composite (several keys) key can be provided in an array for relatedObjectKey.
    • Without giving a related key, the algorithm traverses every single key in each provided object looking for when a single object key is matched along with a value with equivalence between the matching found key.
    • The values of the returned created, updated, or deleted arrays from compareObjectVals / compareArrayVals functions will be null if they do not exist.
const originalArrayOfObjects = [{ one: 1, two: 2 }, { test: undefined }];
const newArrayOfObjects = [{ one: 1, two: null }, { one: 22, five: 5 }]
const relatedObjectKey = 'one';

const { createdVals, updatedVals, deletedVals } = compareObjectVals(
  [originalArrayOfObjects, newArrayOfObjects],
  relatedObjectKey // Not required, but suggested for speed.
);

console.log(createdVals); // [{ one: 22, five: 5 }]
console.log(updatedVals); // [{ one: 1, two: null }]
console.log(deletedVals); // [{ test: undefined }]

See further examples here.

Comparing Arrays

  • Method: compareArrayVals

  • Parameters: toCompareVals: [any[], any[]]

  • Returns: { createdVals: any[] | null, deletedVals: any[] | null }

  • Provides: Created and Deleted values between two arrays of primitives (strings, numbers, etc.) using the compareArrayVals function.

const originalArrayItem = [1, 2, 'five', true, 33];
const updatedArrayItem = [1, 'seven', true, 33];

const { createdVals, deletedVals } = compareArrayVals(
  [ originalArrayItem, updatedArrayItem ]
);
console.log(createdVals); // ['seven']
console.log(deletedVals); // [2, 'five']

See further examples here.

Comparing Two Arrays or Two Objects for Equivalence via helper functions

isEqualObject(a: Object, b: Object): Boolean

isEqualArray(a: any[], b: any[]): Boolean

  • Deep Object and Array comparing helper functions are provided for quick equivalence checks using the isEqualObject or isEqualArray functions. *Note: Object.is() is used for primative data type comparison checks.
const obj1 = {a: 1, b: 2, c: {'one': 1, 'two': [{ 2: 1, 44:3 }]}};
const obj2 = {a: 1, b: 2, c: {'one': 1, 'two': [{ 2: 1, 44:3 }]}};
const areObjectsEquivalent = isEqualObject(obj1, obj2); // true
const array1 = [Symbol('33')];
const array2 = ['one', 'two'];
const areArraysEquivalent = isEqualArray(array1, array2); // false

See further examples here

To support, Please  if you used / like this library!

Todo

  • [ ] Add more tests for edge cases.
  • [ ] Update prettier and fix the linter.
  • [ ] Get the Google Closure Compiler working w/ the advanced compile setting, and fix types in the code to reach ~99%.

Contributing

Please see the contributing guidelines here for further information. All contributions are appreciated, even if they are just comments from issues.

License

MIT