-
Notifications
You must be signed in to change notification settings - Fork 512
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
Conversation
…stringify parameter this is similar to
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. |
There was a problem hiding this comment.
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.
Conflicts: README.md
I ran into this because
undefined
keys in the diff don't show up, so I needed to supply an undefined replacement