New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

js-deep-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

js-deep-diff

Javascript object deep diff.

latest
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

js-deep-diff

An lightly library for comparing two javascript object structure, 1kb after gziped.

Install

$ npm install js-deep-diff
$ yarn add js-deep-diff

Notice

Something you should know before using:

  • new String('string') is NOT equivalent to 'string'
  • new Number(1) is NOT equivalent to 1
  • new Boolean(false) is NOT equivalent to false
  • {0: 1} is NOT equivalent to [1]
  • new RegExp('\\s') is EQUIVALENT to /\s/
  • new Date("1970-01-01T00:00:00.000Z") is EQUIVALENT to new Date("Thu, 01 Jan 1970 00:00:00 GMT")

It's different with deep-object-diff and some library else. (deep-object-diff regard {0: 1} as [1]).

Usage

const diff = require('js-deep-diff')

const lhs = {
  name: 'my object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'elements']
  },
  others: {
    distance: 0,
    hight: 1
  }
}

const rhs = {
  name: 'updated object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'more', 'elements', {
      than: 'before'
    }]
  },
  others: {
    distance: 0
  }
}

diff(lhs, rhs)

Then got the differences:

[
    {
        "type": "EDIT",
        "path": [
            "name"
        ],
        "lhs": "my object",
        "rhs": "updated object"
    },
    {
        "type": "EDIT",
        "path": [
            "details",
            "with",
            2
        ],
        "lhs": "elements",
        "rhs": "more"
    },
    {
        "type": "ADD",
        "path": [
            "details",
            "with",
            3
        ],
        "rhs": "elements"
    },
    {
        "type": "ADD",
        "path": [
            "details",
            "with",
            4
        ],
        "rhs": {
            "than": "before"
        }
    },
    {
        "type": "EDIT",
        "path": [
            "others"
        ],
        "lhs": {
            "distance": 0,
            "hight": 1
        },
        "rhs": {
            "distance": 0
        }
    }
]

Configuration

enableDeleteAction default: false

[
    {
        "type": "EDIT",
        "path": [
            "name"
        ],
        "lhs": "my object",
        "rhs": "updated object"
    },
    {
        "type": "EDIT",
        "path": [
            "details",
            "with",
            2
        ],
        "lhs": "elements",
        "rhs": "more"
    },
    {
        "type": "ADD",
        "path": [
            "details",
            "with",
            3
        ],
        "rhs": "elements"
    },
    {
        "type": "ADD",
        "path": [
            "details",
            "with",
            4
        ],
        "rhs": {
            "than": "before"
        }
    },
    {
        "type": "EDIT",
        "path": [
            "others"
        ],
        "lhs": {
            "distance": 0,
            "hight": 1
        },
        "rhs": {
            "distance": 0
        }
    },
+   {
+       "type": "DEL",
+       "path": [
+           "others",
+           "hight"
+       ],
+       "lhs": 1
+   }
]

Test

$ npm run test

diff ✓ Same value ✓ Empty value: null ✓ Empty value: undefind ✓ Date ✓ Boolean ✓ Number ✓ String ✓ RegExp ✓ Pirmitive value ✓ Add action in array ✓ Edit action in array ✓ Delete action in array ✓ Add action in object ✓ Delete action in object ✓ Edit action in object ✓ Nested object ✓ enableDeleteAction: Array ✓ enableDeleteAction: Object

18 passing (26ms)

Coverage

$ npm run test:coverage
Statements   : 100% ( 56/56 )
Branches     : 58.7% ( 27/46 )
Functions    : 100% ( 16/16 )
Lines        : 100% ( 56/56 )

Benchmark

js-deep-diffdeep-diffdeep-object-diff
5.36937ms5.87379ms3.06766ms

Keywords

js

FAQs

Package last updated on 20 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts