Skip to content

Commit 8415105

Browse files
committed
[minor] ignore sort ordering with objects
1 parent 923d88c commit 8415105

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ function isArray(toCheck) {
77
return typeof toCheck === 'object' && Boolean(toCheck.length);
88
}
99

10-
const defaultOptions = {
10+
function isPrimitive(toCheck) {
11+
return toCheck !== Object(toCheck);
12+
}
13+
14+
function toPrimitive(val) {
15+
return isPrimitive(val) ? val : JSON.stringify(val);
16+
}
17+
18+
var defaultOptions = {
1119
respectArrayOrder: true,
1220
};
1321

@@ -39,11 +47,11 @@ function diffler(obj1, obj2, options = defaultOptions) {
3947

4048
// If property is an object then we need to recursively go down the rabbit hole
4149
else if (typeof obj1Val === 'object') {
42-
let obj1ValForDiff = obj1Val;
43-
let obj2ValForDiff = obj2Val;
50+
var obj1ValForDiff = obj1Val;
51+
var obj2ValForDiff = obj2Val;
4452
if (!options.respectArrayOrder && isArray(obj1Val) && isArray(obj2Val)) {
45-
obj1ValForDiff = obj1Val.sort();
46-
obj2ValForDiff = obj2Val.sort();
53+
obj1ValForDiff = obj1Val.map(toPrimitive).sort();
54+
obj2ValForDiff = obj2Val.map(toPrimitive).sort();
4755
}
4856
var tempDiff = diffler(obj1ValForDiff, obj2ValForDiff);
4957
if (Object.keys(tempDiff).length > 0) {

tests/index.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,43 +58,56 @@ describe('getDiff', () => {
5858
const testObjectB = { arr: ['three', 'two', 'one'] };
5959
const difference = diffler(testObjectA, testObjectB);
6060
assert.strictEqual(Object.keys(difference).length, 1);
61-
assert.deepStrictEqual(difference, { arr: { 0: { from: 'one', to: 'three' }, 2: { from: 'three', to: 'one' } } });
61+
assert.deepStrictEqual(difference, {
62+
arr: {
63+
0: { from: 'one', to: 'three' },
64+
2: { from: 'three', to: 'one' },
65+
},
66+
});
6267
});
6368

6469
it('returns false when array order shifted but respectArrayOrder is false', () => {
6570
const testObjectA = { arr: ['one', 'two', 'three'] };
6671
const testObjectB = { arr: ['three', 'two', 'one'] };
67-
const difference = diffler(testObjectA, testObjectB, { respectArrayOrder: false });
72+
const difference = diffler(testObjectA, testObjectB, {
73+
respectArrayOrder: false,
74+
});
6875
assert.strictEqual(Object.keys(difference).length, 0);
6976
assert.deepStrictEqual(difference, {});
7077
});
7178

7279
it('returns false when array order shifted but respectArrayOrder is false as numbers', () => {
7380
const testObjectA = { arr: [1, 2, 3] };
7481
const testObjectB = { arr: [2, 3, 1] };
75-
const difference = diffler(testObjectA, testObjectB, { respectArrayOrder: false });
82+
const difference = diffler(testObjectA, testObjectB, {
83+
respectArrayOrder: false,
84+
});
7685
assert.strictEqual(Object.keys(difference).length, 0);
7786
assert.deepStrictEqual(difference, {});
7887
});
7988

8089
it('returns false when array order shifted but respectArrayOrder is false as mixed', () => {
8190
const testObjectA = { arr: [1, 'two', 3] };
8291
const testObjectB = { arr: ['two', 3, 1] };
83-
const difference = diffler(testObjectA, testObjectB, { respectArrayOrder: false });
92+
const difference = diffler(testObjectA, testObjectB, {
93+
respectArrayOrder: false,
94+
});
8495
assert.strictEqual(Object.keys(difference).length, 0);
8596
assert.deepStrictEqual(difference, {});
8697
});
8798

88-
it('returns diff when array order shifted for non-primitives and respectArrayOrder is false', () => {
99+
it('returns no diff when array order shifted for non-primitives and respectArrayOrder is false', () => {
89100
const testObjectA = {
90-
myArray: [{ foo: 'bar' }, { baz: 'bat' }],
101+
myArray: ['a string', { foo: 'bar' }, 1, { baz: 'bat' }],
91102
};
92103

93104
const testObjectB = {
94-
myArray: [{ baz: 'bat' }, { foo: 'bar' }],
105+
myArray: [{ baz: 'bat' }, { foo: 'bar' }, 1, 'a string'],
95106
};
96-
const difference = diffler(testObjectA, testObjectB, { respectArrayOrder: false });
97-
assert.strictEqual(Object.keys(difference).length, 1);
107+
const difference = diffler(testObjectA, testObjectB, {
108+
respectArrayOrder: false,
109+
});
110+
assert.strictEqual(Object.keys(difference).length, 0);
98111
assert.deepStrictEqual(difference, {});
99112
});
100113

@@ -103,23 +116,32 @@ describe('getDiff', () => {
103116
const testObjectB = { arr: ['one', 'two', 'three'] };
104117
const difference = diffler(testObjectA, testObjectB);
105118
assert.strictEqual(Object.keys(difference).length, 1);
106-
assert.deepStrictEqual(difference, { arr: { 2: { from: null, to: 'three' } } });
119+
assert.deepStrictEqual(difference, {
120+
arr: { 2: { from: null, to: 'three' } },
121+
});
107122
});
108123

109124
it('returns change when array item removed', () => {
110125
const testObjectA = { arr: ['one', 'two', 'three'] };
111126
const testObjectB = { arr: ['one', 'two'] };
112127
const difference = diffler(testObjectA, testObjectB);
113128
assert.strictEqual(Object.keys(difference).length, 1);
114-
assert.deepStrictEqual(difference, { arr: { 2: { from: 'three', to: null } } });
129+
assert.deepStrictEqual(difference, {
130+
arr: { 2: { from: 'three', to: null } },
131+
});
115132
});
116133

117134
it('returns change and removal when array item removed from middle', () => {
118135
const testObjectA = { arr: ['one', 'two', 'three'] };
119136
const testObjectB = { arr: ['one', 'three'] };
120137
const difference = diffler(testObjectA, testObjectB);
121138
assert.strictEqual(Object.keys(difference).length, 1);
122-
assert.deepStrictEqual(difference, { arr: { 1: { from: 'two', to: 'three' }, 2: { from: 'three', to: null } } });
139+
assert.deepStrictEqual(difference, {
140+
arr: {
141+
1: { from: 'two', to: 'three' },
142+
2: { from: 'three', to: null },
143+
},
144+
});
123145
});
124146
});
125147

0 commit comments

Comments
 (0)