Skip to content

outputKeys should output the property value even if it is in excludeKeys #127

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ class JsonDiff {
let score = 0
let equal = true

for (const [key, value] of Object.entries(obj1)) {
if (!this.options.outputNewOnly) {
const postfix = '__deleted'

if (!(key in obj2) && !(this.options.excludeKeys.includes(key))) {
result[`${key}${postfix}`] = value
score -= 30
equal = false
}
}
}

for (const [key, value] of Object.entries(obj2)) {
const postfix = !this.options.outputNewOnly ? '__added' : ''

Expand All @@ -41,21 +29,36 @@ class JsonDiff {
}

for (const [key, value1] of Object.entries(obj1)) {
let keyChanged = false;
if (key in obj2) {
if (this.options.excludeKeys.includes(key)) {
continue
}
score += 20
const value2 = obj2[key]
const change = this.diff(value1, value2)
if (!change.equal) {
result[key] = change.result
equal = false
} else if (this.options.full || this.options.outputKeys.includes(key)) {
result[key] = value1
if (!this.options.excludeKeys.includes(key)) {
score += 20;
const value2 = obj2[key];
const change = this.diff(value1, value2);
if (!change.equal) {
result[key] = change.result;
equal = false;
keyChanged = true;
}
score += Math.min(20, Math.max(-10, change.score / 5)); // BATMAN!
}
// console.log(`key ${key} change.score=${change.score} ${change.result}`)
score += Math.min(20, Math.max(-10, change.score / 5)) // BATMAN!
} else if (
!this.options.outputNewOnly &&
!(key in obj2) &&
!this.options.excludeKeys.includes(key)
) {
const postfix = "__deleted";
result[`${key}${postfix}`] = value1;
score -= 30;
equal = false;
keyChanged = true;
}

if (
!keyChanged &&
(this.options.full || this.options.outputKeys.includes(key))
) {
result[key] = value1;
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/diff_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ describe 'diff({ excludeKeys: foo,bar }', ->
it "shouldn't return keys foo and bar (with addition) ", ->
assert.deepEqual undefined, diff({ bbar: 5 }, { foo: 42, bar: 10, bbar: 5 }, {excludeKeys: ["foo", "bar"]})

describe 'diff({ excludeKeys: foo, bar outputKeys: foo }', ->

it "Should return foo even though it is in excludeKeys", ->
assert.deepEqual { bbar__added: 5, foo: 42 }, diff({ foo: 42 }, { bar: 10, bbar: 5 }, {excludeKeys: ["foo", "bar"], outputKeys: ["foo"]})

it "Should return foo even though it has not changed", ->
assert.deepEqual { bbar__added: 5, foo: 42 }, diff({ foo: 42 }, { foo: 42, bar: 10, bbar: 5 }, {excludeKeys: ["foo", "bar"], outputKeys: ["foo"]})

it "shouldn't return keys foo because it doesn't exist in value 1", ->
assert.deepEqual { bbar__added: 5 }, diff({ bar: 10 }, { foo: 42, bar: 10, bbar: 5 }, {excludeKeys: ["foo", "bar"], outputKeys: ["foo"]})

it "should return foo of value 1 event though it has changed", ->
assert.deepEqual { bbar: { __new: 6, __old: 5 }, foo: 42 }, diff({ bbar: 5, foo: 42 }, { foo: 43, bar: 10, bbar: 6 }, {excludeKeys: ["foo", "bar"], outputKeys: ["foo"]})

describe 'diff({keysOnly: true})', ->

describe 'with simple scalar values', ->
Expand Down