Skip to content

detailedDiff returning confusing data #62

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

Closed
sdetweil opened this issue Apr 19, 2021 · 6 comments
Closed

detailedDiff returning confusing data #62

sdetweil opened this issue Apr 19, 2021 · 6 comments

Comments

@sdetweil
Copy link

i look at the results of the diff and see

{added:{}, deleted:{}, updated:{}}

I compare against an object like that, and get not equal.

I json.stringify() the object and it looks the same
I check the number of keys in each section and see some number of keys in one of more areas (usually deleted)
but those keys are not in the object being compared.

BUT
I compare this object (old)

{
		compliments: {
			anytime: ["Hey there sexy!"],
			morning: ["Good morning, handsome!", "Enjoy your day!", "How was your sleep?"],
			afternoon: ["Hello, beauty!", "You look sexy!", "Looking good today!"],
			evening: ["Wow, you look hot!", "You look nice!", "Hi, sexy!"],
			"....-01-01": ["Happy new year!"]
		},
		updateInterval: 30000,
		remoteFile: null,
		fadeSpeed: 4000,
		morningStartTime: 3,
		morningEndTime: 12,
		afternoonStartTime: 12,
		afternoonEndTime: 17,
		random: true,
		mockDate: null
	}

against this (new)

compliments: {
            anytime: [
              "Hey there sexy!"
            ],
            morning: [
              "Good morning, handsome!",
              "Enjoy your day!",
              "How was your sleep?"
            ],
            afternoon: [
              "Hello, beauty!",
              "You look sexy!",
              "Looking good today!"
            ],
            evening: [
              "Wow, you look hot!",
              "You look nice!",
              "Hi, sexy!"
            ],
            "-01-01": [
              "Happy new year!"
            ]
          },
          updateInterval: 30000,
          fadeSpeed: 4000,
          morningStartTime: 3,
          morningEndTime: 12,
          afternoonStartTime: 12,
          afternoonEndTime: 17,
          random: true
        }

and get this

{
  "added": {
    "compliments": {
      "-01-01": [
        "Happy new year!"
      ]
    }
  },
  "deleted": {
    "compliments": {}
  },
  "updated": {}

but deleted is incomplete
"....-01-01" is not there

note this text is presented by JSON.stringify() on the objects..

@sdetweil sdetweil changed the title detailtedDiff returning confusing data detailedDiff returning confusing data Apr 19, 2021
@anko
Copy link
Contributor

anko commented Apr 22, 2021

Could you post a minimal example of runnable code that demonstrates the problem?

I tried this—

const { detailedDiff } = require('deep-object-diff')

const prev = { '....-01-01': ['Happy new year!'] }
const next = { '-01-01': ['Happy new year!'] }

console.log(detailedDiff(prev, next))

—and its output seems correct:

{
  added: { '-01-01': [ 'Happy new year!' ] },
  deleted: { '....-01-01': undefined },
  updated: {}
}

I also tried the same snippet as above but copy-pasted your objects. I got this output, which also seems correct logically, but it's very different from your result:

{
  added: { compliments: { '-01-01': [Array] } },
  deleted: {
    compliments: { '....-01-01': undefined },
    remoteFile: undefined,
    mockDate: undefined
  },
  updated: {}
}

@sdetweil
Copy link
Author

sdetweil commented Apr 23, 2021

here you go... remove the .txt on all three
have to npm install
node testdiff.js

testdiff.js.txt
old.json.txt
new.json.txt
results

diff = {
  "added": {
    "properties": {
      "compliments": {
        "properties": {
          "-01-01": {
            "type": "array",
            "title": "-01-01",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "deleted": {
    "properties": {
      "compliments": {
        "properties": {}
      }
    }
  },
  "updated": {}
}
deleted property length = 1
deleted property=.....-01-01

added, but not deleted, altho something was
but stringify output is incomplete.. but the property value is undefined..

@anko
Copy link
Contributor

anko commented Apr 24, 2021

Thanks for the example. I think I see what's going on now. JSON.stringify ignores properties that are undefined, because they are not allowed in valid JSON according to the spec:

A JSON value MUST be an object, array, number, or string, or one of the following three literal names:

 false null true

undefined is none of those, so JSON.stringify({ x: undefined }) evaluates to '{}'. The diff result is correct, but in converting to JSON, the undefined values that are part of the deleted.properties.compliments.properties object are omitted.

If you must get the diff output into JSON format specifically, you'll have to transform it such that undefined isn't used.

If you don't need valid JSON but just want to turn the object into a representative string for debugging purposes, consider util.inspect, which can stringify any JavaScript value, but not in a way that JSON.parse can necessarily read back in.

@sdetweil
Copy link
Author

I guess the point is that the OLD object DOES have the definition.. so it shouldn't be undefined..
yes, its not in the new , but,.. it WAS in the old..

@anko
Copy link
Contributor

anko commented Apr 24, 2021

Every value in the deleted part of a detailedDiff is undefined, and the documentation examples show this. It's expected behaviour.

I think undefined makes sense there. A property's value after deletion is undefined as listed in its the deleted entry, just as a property's value after addition is the one listed in the corresponding added entry.

@sdetweil
Copy link
Author

working as designed, but not right..

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

No branches or pull requests

2 participants