Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nested-object-diff

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested-object-diff - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

2

dist/index.js

@@ -1,1 +0,1 @@

(function(a,b){if("function"==typeof define&&define.amd)define(["exports"],b);else if("undefined"!=typeof exports)b(exports);else{var c={exports:{}};b(c.exports),a.index=c.exports}})(this,function(a){"use strict";function b(a){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},b(a)}function c(a,c){return c&&("object"===b(c)||"function"==typeof c)?c:d(a)}function d(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function e(a){return e=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},e(a)}function f(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&g(a,b)}function g(a,b){return g=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},g(a,b)}function h(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(a,"__esModule",{value:!0}),a.diff=void 0;var i=function a(b,c){h(this,a),this.type=b,this.path=c},j=/*#__PURE__*/function(a){function b(a,d,f){var g;return h(this,b),g=c(this,e(b).call(this,"E",a)),g.lhs=d,g.rhs=f,g}return f(b,a),b}(i),k=/*#__PURE__*/function(a){function b(a,d){var f;return h(this,b),f=c(this,e(b).call(this,"D",a)),f.lhs=d,f}return f(b,a),b}(i),l=/*#__PURE__*/function(a){function b(a,d){var f;return h(this,b),f=c(this,e(b).call(this,"A",a)),f.rhs=d,f}return f(b,a),b}(i),m=function(a,b){return a?"".concat(a,".").concat(b):b};a.diff=function diff(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},d=[],e=c.matchKey,f=function(a,b,c,e){a.forEach(function(a){var f=b.findIndex(function(b){return b[e]===a[e]});-1<f?g(a,b[f],m(c,f)):d.push(new k(c,a))}),b.forEach(function(b,f){var g=a.findIndex(function(a){return b[e]===a[e]});-1===g&&d.push(new l(m(c,f),b))})},g=function(a,b,c){var h=Object.prototype.toString.call(a),i=Object.prototype.toString.call(b);if(h!==i)return d.push(new j(c,a,b)),!1;if("[object Object]"===h)Object.getOwnPropertyNames(a).forEach(function(e){Object.prototype.hasOwnProperty.call(b,e)?g(a[e],b[e],m(c,e)):d.push(new k(m(c,e),a[e]))}),Object.getOwnPropertyNames(b).forEach(function(e){Object.prototype.hasOwnProperty.call(a,e)||d.push(new l(m(c,e),b[e]))});else if("[object Array]"!==h)a!==b&&d.push(new j(c,a,b));else if(!e){for(var n=a.length-1,o=b.length-1;n>o;)d.push(new k(m(c,n),a[n--]));for(;o>n;)d.push(new l(m(c,o),b[o--]));for(;0<=n;--n)g(a[n],b[n],m(c,n))}else f(a,b,c,e)};return g(a,b),d}});
(function(a,b){if("function"==typeof define&&define.amd)define(["exports"],b);else if("undefined"!=typeof exports)b(exports);else{var c={exports:{}};b(c.exports),a.index=c.exports}})(this,function(a){"use strict";function b(a){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},b(a)}function c(a,c){return c&&("object"===b(c)||"function"==typeof c)?c:d(a)}function d(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function e(a){return e=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},e(a)}function f(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&g(a,b)}function g(a,b){return g=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},g(a,b)}function h(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(a,"__esModule",{value:!0}),a.diff=void 0;var i=function a(b,c){h(this,a),this.type=b,this.path=c?c.toString():""},j=/*#__PURE__*/function(a){function b(a,d,f){var g;return h(this,b),g=c(this,e(b).call(this,"E",a)),g.lhs=d,g.rhs=f,g}return f(b,a),b}(i),k=/*#__PURE__*/function(a){function b(a,d,f,g){var i;return h(this,b),i=c(this,e(b).call(this,"M",a)),i.item=d,i.lhsIndex=f,i.rhsIndex=g,i}return f(b,a),b}(i),l=/*#__PURE__*/function(a){function b(a,d){var f;return h(this,b),f=c(this,e(b).call(this,"D",a)),f.lhs=d,f}return f(b,a),b}(i),m=/*#__PURE__*/function(a){function b(a,d){var f;return h(this,b),f=c(this,e(b).call(this,"A",a)),f.rhs=d,f}return f(b,a),b}(i),n=function(a,b){return a?"".concat(a,".").concat(b):b};a.diff=function diff(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},d=[],e=c.matchKey,f=c.types||["E","A","D","M"],g=function(a,b,c,e){a.forEach(function(a,g){var i=b.findIndex(function(b){return b[e]===a[e]});-1<i?(-1<f.indexOf("M")&&g!==i&&d.push(new k(c,a,g,i)),h(a,b[i],n(c,i))):-1<f.indexOf("D")&&d.push(new l(c,a))}),b.forEach(function(b,g){var h=a.findIndex(function(a){return b[e]===a[e]});-1<f.indexOf("A")&&-1===h&&d.push(new m(n(c,g),b))})},h=function(a,b,c){var i=Object.prototype.toString.call(a),k=Object.prototype.toString.call(b);if(-1<f.indexOf("E")&&i!==k)return d.push(new j(c,a,b)),!1;if("[object Object]"===i)Object.getOwnPropertyNames(a).forEach(function(e){Object.prototype.hasOwnProperty.call(b,e)?h(a[e],b[e],n(c,e)):-1<f.indexOf("D")&&d.push(new l(n(c,e),a[e]))}),Object.getOwnPropertyNames(b).forEach(function(e){-1<f.indexOf("A")&&!Object.prototype.hasOwnProperty.call(a,e)&&d.push(new m(n(c,e),b[e]))});else if("[object Array]"!==i)-1<f.indexOf("E")&&a!==b&&d.push(new j(c,a,b));else if(!e){var o=a.length-1,p=b.length-1;if(-1<f.indexOf("D"))for(;o>p;)d.push(new l(n(c,o),a[o--]));if(-1<f.indexOf("A"))for(;p>o;)d.push(new m(n(c,p),b[p--]));for(;0<=o;--o)h(a[o],b[o],n(c,o))}else g(a,b,c,e)};return h(a,b),d}});
{
"name": "nested-object-diff",
"version": "1.0.2",
"version": "1.1.0",
"description": "A lightweight javascript diff library for comparing two javascript object nested with supporting matching by value of the object's choosen key name in array.",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -11,14 +11,19 @@ <div align="center">

## Installation
`npm i --save nested-object-diff`
```js
npm i --save nested-object-diff
```
## Importing
NodeJs / ES5:
``` js
var diff = require('nested-object-diff').diff;
```js
var diff = require("nested-object-diff").diff;
```
ES6 / Babel:
``` js
import diff from 'nested-object-diff';
```js
import { diff } from "nested-object-diff";
```

@@ -29,9 +34,13 @@

```js
var diff = require('./dist/index').diff;
var diff = require("./dist/index").diff;
var lhs = {
shop: {
products: [{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }]
products: [
{ id: 1, name: "a" },
{ id: 2, name: "b" },
{ id: 3, name: "c" }
]
},
name: 'foo'
name: "foo"
};

@@ -43,20 +52,34 @@

{ id: 1, name: 'a' },
{ id: 2, name: 'bb' },
{ id: 4, name: 'd' },
{ id: 5, name: 'e' },
{ id: 3, name: 'cc' }
]
},
name: 'baz'
name: "baz"
};
var differences = diff(lhs, rhs, { matchKey: 'id' });
var differences = diff(lhs, rhs);
//The code snippet above would result only added and deleted propery/element in the following structure describing the differences:
[
{ type: 'A', path: 'shop.products.3', rhs: { id: 3, name: 'cc' } },
{ type: 'E', path: 'shop.products.2.id', lhs: 3, rhs: 5 },
{ type: 'E', path: 'shop.products.2.name', lhs: 'c', rhs: 'e' },
{ type: 'E', path: 'shop.products.1.id', lhs: 2, rhs: 4 },
{ type: 'E', path: 'shop.products.1.name', lhs: 'b', rhs: 'd' },
{ type: 'E', path: 'name', lhs: 'foo', rhs: 'baz' }
]
```
With match options
The code snippet above would result in the following structure describing the differences:
```js
var differences = diff(lhs, rhs, { matchKey: 'id' });
```js
//The code snippet above would result in the following structure describing the differences:
[
{ type: 'E', path: 'shop.products.1.name', lhs: 'b', rhs: 'bb' },
{ type: 'D', path: 'shop.products', lhs: { id: 2, name: 'b' } },
{ type: 'M', path: 'shop.products', item: { id: 3, name: 'c' }, lhsIndex: 2, rhsIndex: 3},
{ type: 'E', path: 'shop.products.3.name', lhs: 'c', rhs: 'cc' },
{ type: 'A', path: 'shop.products.2', rhs: { id: 4, name: 'd' } },
{ type: 'A', path: 'shop.products.1', rhs: { id: 4, name: 'd' } },
{ type: 'A', path: 'shop.products.2', rhs: { id: 5, name: 'e' } },
{ type: 'E', path: 'name', lhs: 'foo', rhs: 'baz' }

@@ -66,8 +89,22 @@ ]

With types options
```js
var differences = diff(lhs, rhs, { matchKey: 'id', types:['A','D'] });
//The code snippet above would result only added and deleted propery/element in the following structure describing the differences:
[
{ type: 'D', path: 'shop.products', lhs: { id: 2, name: 'b' } },
{ type: 'A', path: 'shop.products.1', rhs: { id: 4, name: 'd' } },
{ type: 'A', path: 'shop.products.2', rhs: { id: 5, name: 'e' } }
]
```
### Function Arguments
* `lhs` - the left-hand operand; the origin object.
* `rhs` - the right-hand operand; the object being compared structurally with the origin object.
* `options` - A configuration object that can have the following properties:
- `matchKey`: used to match objects when diffing arrays, by default only === operator is used
- `lhs` - the left-hand operand; the origin object.
- `rhs` - the right-hand operand; the object being compared structurally with the origin object.
- `options` - A configuration object that can have the following properties:
- `matchKey`: used to match objects when diffing arrays, by default only === operator is used; string
- `types`: calculates differences for given types; array

@@ -78,9 +115,13 @@ ### Differences

* `type` - indicates the kind of change; will be one of the following:
* `A` - indicates a newly added property
* `D` - indicates a property was deleted
* `E` - indicates a property was edited
* `path` - the property path
* `lhs` - the value on the left-hand-side of the comparison (undefined if kind === 'A')
* `rhs` - the value on the right-hand-side of the comparison (undefined if kind === 'D')
- `type` - indicates the kind of change; will be one of the following:
- `A` - indicates a newly added property/element
- `D` - indicates a property/element was deleted
- `E` - indicates a property/element was edited
- `M` - indicates a property/element was moved
- `path` - the property/element path
- `item` - when type === 'M', contains a nested change record indicating the change that occurred at the array index
- `lhsIndex` - when type === 'M', indicates the left-hand-side array index
- `rhsIndex` - when type === 'M', indicates the right-hand-side array index
- `lhs` - the value on the left-hand-side of the comparison (undefined if kind === 'A')
- `rhs` - the value on the right-hand-side of the comparison (undefined if kind === 'D')

@@ -94,2 +135,2 @@ ## Contributing

MIT
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc