Skip to content

Option to take in account undefined values in diffJson #155

@ewnd9

Description

@ewnd9

I'm using jsdiff to show a diff if two objects are not equal.

const { diffJson } = require('diff');
const isEqual = require('lodash.isequal');

check({ x: 1, y: undefined }, { x: 1 });

function check(expected, actual) {
  if (!isEqual(expected, actual)) { //=> false
    console.log(diffJson(expected, actual)); //=> [ { value: '{\n  "x": 1\n}', count: 3 } ]
  }
}

Now it's ignoring undefined value due to default JSON.stringify behaviour.
I propose to add an option to diffJson method to show changes. The simplest way I found is to add a custom replacer function (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) in JSON.stringify (see ewnd9@c6daea7)

I would love to make a PR if the change is welcomed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions