Skip to content

Document diffJson() options #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 18, 2023
Merged

Conversation

cincodenada
Copy link
Contributor

I ran into this because undefined keys in the diff don't show up, so I needed to supply an undefined replacement

@cincodenada cincodenada changed the title Document jsonDiff() options Document diffJson() options Oct 25, 2021
README.md Outdated
@@ -56,6 +56,10 @@ npm install diff --save
* `Diff.diffJson(oldObj, newObj[, options])` - diffs two JSON objects, comparing the fields defined on each. The order of fields, etc does not matter in this comparison.

Returns a list of change objects (See below).

Options
* `stringifyReplacer`: A custom replacer function. Operates similarly to the `replacer` parameter to [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter), but must be a function. `undefined` will _not_ be replaced by this function; see the next option.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined will not be replaced by this function

I don't think this is true. Looking at the code, it seems like the only place that undefinedReplacement gets used is in the default implementation of stringifyReplacer, and that if you provide both, then undefinedReplacement simply gets ignored. There doesn't seem to be any special logic preventing stringifyReplacer from running on an undefined value either:

> diff.diffJson(
...     {x: undefined},
...     {x: "bob"}
... )
[
  { count: 1, added: undefined, removed: true, value: '{}' },
  {
    count: 3,
    added: true,
    removed: undefined,
    value: '{\n  "x": "bob"\n}'
  }
]
> 
> diff.diffJson(
...     {x: undefined},
...     {x: "bob"},
...     {
...         undefinedReplacement: "bob",
...     }
... )
[ { value: '{\n  "x": "bob"\n}', count: 3 } ]
> 
> diff.diffJson(
...     {x: undefined},
...     {x: "bob"},
...     {
...         undefinedReplacement: "jim",
...         stringifyReplacer: (k, v) => k == "x" ? "bob" : v
...     }
... )
[ { value: '{\n  "x": "bob"\n}', count: 3 } ]
> 
> diff.diffJson(
...     {x: undefined},
...     {x: "bob"},
...     {
...         stringifyReplacer: (k, v) => k == "x" ? "bob" : v
...     }
... )
[ { value: '{\n  "x": "bob"\n}', count: 3 } ]

Maybe what you wrote here was true when you opened the PR - dunno, haven't checked - but it's wrong now.

@ExplodingCabbage ExplodingCabbage merged commit e0e960a into kpdecker:master Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants