Skip to content

Commit a0ad69f

Browse files
committed
Retiring diffDeep.
This function has a number of bugs, and lodash does it better. closes #814
1 parent 2af4909 commit a0ad69f

2 files changed

Lines changed: 0 additions & 99 deletions

File tree

lib/config.js

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -546,61 +546,6 @@ util.attachProtoDeep = function(toObject, depth) {
546546
return toObject;
547547
};
548548

549-
/**
550-
* Returns an object containing all elements that differ between two objects.
551-
* <p>
552-
* This method was designed to be used to create the runtime.json file
553-
* contents, but can be used to get the diffs between any two Javascript objects.
554-
* </p>
555-
* <p>
556-
* It works best when object2 originated by deep copying object1, then
557-
* changes were made to object2, and you want an object that would give you
558-
* the changes made to object1 which resulted in object2.
559-
* </p>
560-
*
561-
* <b>This function has a number of issues and you may be better off with lodash</b>
562-
*
563-
* @protected
564-
* @deprecated please investigate lodash or similar tools for object traversal
565-
* @method diffDeep
566-
* @param object1 {Object} The base object to compare to
567-
* @param object2 {Object} The object to compare with
568-
* @param depth {integer} An optional depth to prevent recursion. Default: 20.
569-
* @return {Object} A differential object, which if extended onto object1 would
570-
* result in object2.
571-
*/
572-
util.diffDeep = function(object1, object2, depth) {
573-
// Recursion detection
574-
const diff = {};
575-
depth = (depth === null ? DEFAULT_CLONE_DEPTH : depth);
576-
if (depth < 0) {
577-
return {};
578-
}
579-
580-
// Process each element from object2, adding any element that's different
581-
// from object 1.
582-
for (const parm in object2) {
583-
const value1 = object1[parm];
584-
const value2 = object2[parm];
585-
if (value1 && value2 && Util.isObject(value2)) {
586-
if (!(Util.equalsDeep(value1, value2))) {
587-
diff[parm] = util.diffDeep(value1, value2, depth - 1);
588-
}
589-
}
590-
else if (Array.isArray(value1) && Array.isArray(value2)) {
591-
if(!Util.equalsDeep(value1, value2)) {
592-
diff[parm] = value2;
593-
}
594-
}
595-
else if (value1 !== value2){
596-
diff[parm] = value2;
597-
}
598-
}
599-
600-
// Return the diff object
601-
return diff;
602-
};
603-
604549
/**
605550
* Returns a new deep copy of the current config object, or any part of the config if provided.
606551
*

test/1-protected-test.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,6 @@ describe('Protected (hackable) utilities test', function() {
5555
});
5656
});
5757

58-
describe('diffDeep() tests', function() {
59-
it('The function exists', function() {
60-
assert.strictEqual(typeof config.util.diffDeep, 'function');
61-
});
62-
63-
it('Returns an empty object if no differences', function() {
64-
let a = {value_3: 14, hello:'world', value_1: 29};
65-
let b = {value_1: 29, hello:'world', value_3: 14};
66-
67-
assert.strictEqual(typeof config.util.diffDeep(a,b), 'object');
68-
assert.strictEqual(Object.keys(config.util.diffDeep(a, b)).length, 0);
69-
});
70-
71-
it('Returns an empty object if no differences (deep)', function() {
72-
let a = { value_3: 14, hello:'world', value_1: 29, value_4: [1,'hello',2], deepObj: { a: 22, b: { c: 45, a: 44 } } };
73-
let b = { value_1: 29, hello:'world', value_3: 14, value_4: [1,'hello',2], deepObj: { a: 22, b: { a: 44, c: 45 } } };
74-
75-
assert.strictEqual(typeof(config.util.diffDeep(a,b)), 'object');
76-
assert.strictEqual(Object.keys(config.util.diffDeep(a, b)).length, 0);
77-
});
78-
79-
it('Returns just the diff values', function() {
80-
let a = { value_3: 14, hello:'wurld', value_1: 29, deepObj: { a: 22, b: { c: 45, a: 44 } } };
81-
let b = { value_1: 29, hello:'world', value_3: 14, deepObj: { a: 22, b: { a: 44, c: 45 } } };
82-
let diff = config.util.diffDeep(a,b);
83-
84-
assert.strictEqual(Object.keys(diff).length, 1);
85-
assert.strictEqual(diff.hello, 'world');
86-
});
87-
88-
it('Returns just the diff values (deep)', function() {
89-
let a = { value_3: 14, hello: 'wurld', value_1: 29, value_4: [1,'hello',2], deepObj: { a:22, b: { c: 45, a: 44} } };
90-
let b = { value_1: 29, hello: 'wurld', value_3: 14, value_4: [1,'goodbye',2], deepObj: { a:22, b: { a: 45, c: 44} } };
91-
let diff = config.util.diffDeep(a,b);
92-
93-
assert.strictEqual(Object.keys(diff).length, 2);
94-
assert.strictEqual(Object.keys(diff.deepObj).length, 1);
95-
assert.strictEqual(Object.keys(diff.deepObj.b).length, 2);
96-
assert.strictEqual(diff.deepObj.b.a, 45);
97-
assert.strictEqual(diff.deepObj.b.c, 44);
98-
assert.deepEqual(diff.value_4, [1, 'goodbye', 2]);
99-
})
100-
});
101-
10258
describe('loadFileConfigs() tests', function() {
10359
let configs;
10460

0 commit comments

Comments
 (0)