Skip to content
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

making date diffs work #106

Merged
merged 1 commit into from
Feb 12, 2023
Merged
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
10 changes: 7 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class JsonDiff {
// console.log(`objectDiff(${JSON.stringify(obj1, null, 2)} <=> ${JSON.stringify(obj2, null, 2)}) == ${JSON.stringify({score, result, equal})}`)
return { score, result, equal }
}

findMatchingObject (item, index, fuzzyOriginals) {
// console.log("findMatchingObject: " + JSON.stringify({item, fuzzyOriginals}, null, 2))
let bestMatch = null
Expand Down Expand Up @@ -291,7 +291,7 @@ class JsonDiff {
switch (type1) {
case 'object':
return this.objectDiff(obj1, obj2)

case 'array':
return this.arrayDiff(obj1, obj2)
}
Expand All @@ -302,7 +302,11 @@ class JsonDiff {
let result = obj1
let equal
if (!this.options.keysOnly) {
equal = obj1 === obj2
if(type1 === 'date' && type2 === 'date') {
equal = obj1.getTime() === obj2.getTime()
} else {
equal = obj1 === obj2
}
if (!equal) {
score = 0

Expand Down
2 changes: 2 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const extendedTypeOf = function (obj) {
return 'null'
} else if (result === 'object' && obj.constructor === Array) {
return 'array'
} else if (result === 'object' && obj instanceof Date) {
return 'date'
} else {
return result
}
Expand Down
10 changes: 10 additions & 0 deletions test/diff_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ describe 'diff', ->
it "should return undefined for two identical strings", ->
assert.deepEqual undefined, diff("foo", "foo")

it "should return undefined for two identical dates", ->
date = new Date()
assert.deepEqual undefined, diff(date, date)

it "should return { __old: <old value>, __new: <new value> } object for two different numbers", ->
assert.deepEqual { __old: 42, __new: 10 }, diff(42, 10)

it "should return { __old: <old value>, __new: <new value> } object for two different dates", ->
oldDate = new Date()
newDate = new Date()
newDate.setFullYear(oldDate.getFullYear()-4)
assert.deepEqual { __old: oldDate, __new: newDate }, diff(oldDate, newDate)

describe 'with objects', ->

it "should return undefined for two empty objects", ->
Expand Down