2.1.0 • Published 3 years ago

crud-compare v2.1.0

Weekly downloads
19
License
MIT
Repository
github
Last release
3 years ago

CRUD Compare - JS State Comparison Helper

npm npm bundle size Coverage GitHub PRs Welcome CircleCI

This is a very ✌ lightweight and ⚡️ fast library for comparing objects and arrays (mostly before save) to get the created, updated, and deleted values for state comparisons.

  • Provides Created, Updated, and Deleted values (via separate arrays) from comparing two arrays of objects given a related key, using the compareObjectVals function.

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

  • Lastly, has a deep Object and Array comparing helper functions for quick equivalence checks using the isEqualObject or isEqualArray function. *Note: Object.is() is used for primative data type comparison checks.

Install

npm install crud-compare

Usage

const compare = require("crud-compare");

const { createdVals, updatedVals, deletedVals } = compare.compareObjectVals(
  [
    originalArrayOfObjects,
    newArrayOfObjects
  ],
  relatedObjectKey
);

// perform crud operations...

const { createdVals, deletedVals } = compare.compareArrayVals(
  [
    originalArrayItem,
    updatedArrayItem
  ]
);
// perform further operations...

const areObjectsEquivalent = compare.isEqualObject(
  obj1,
  obj2
);

The values of the returned created, updated, or deleted values for compareObjectVals / compareArrayVals will be null if they do not exist. A shallow compare is performed on objects within the compareObjectVals function.

Contribute

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am "Add some feature"
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT