From ab1efbd4c12a5792f55a1021903688d5aa864b15 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Fri, 17 Apr 2015 15:47:22 -0700 Subject: [PATCH] [ReactNative] kill for...in array iteration in deepDiffer --- .../differ/__tests__/deepDiffer-test.js | 102 ++++++++++++++++++ Libraries/Utilities/differ/deepDiffer.js | 29 +++-- 2 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 Libraries/Utilities/differ/__tests__/deepDiffer-test.js diff --git a/Libraries/Utilities/differ/__tests__/deepDiffer-test.js b/Libraries/Utilities/differ/__tests__/deepDiffer-test.js new file mode 100644 index 000000000..745b83ab0 --- /dev/null +++ b/Libraries/Utilities/differ/__tests__/deepDiffer-test.js @@ -0,0 +1,102 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +'use strict'; + +jest.dontMock('deepDiffer'); +var deepDiffer = require('deepDiffer'); + +describe('deepDiffer', function() { + it('should diff primitives of the same type', () => { + expect(deepDiffer(1, 2)).toBe(true); + expect(deepDiffer(42, 42)).toBe(false); + expect(deepDiffer('foo', 'bar')).toBe(true); + expect(deepDiffer('foo', 'foo')).toBe(false); + expect(deepDiffer(true, false)).toBe(true); + expect(deepDiffer(false, true)).toBe(true); + expect(deepDiffer(true, true)).toBe(false); + expect(deepDiffer(false, false)).toBe(false); + expect(deepDiffer(null, null)).toBe(false); + expect(deepDiffer(undefined, undefined)).toBe(false); + }); + it('should diff primitives of different types', () => { + expect(deepDiffer(1, '1')).toBe(true); + expect(deepDiffer(true, 'true')).toBe(true); + expect(deepDiffer(true, 1)).toBe(true); + expect(deepDiffer(false, 0)).toBe(true); + expect(deepDiffer(null, undefined)).toBe(true); + expect(deepDiffer(null, 0)).toBe(true); + expect(deepDiffer(null, false)).toBe(true); + expect(deepDiffer(null, '')).toBe(true); + expect(deepDiffer(undefined, 0)).toBe(true); + expect(deepDiffer(undefined, false)).toBe(true); + expect(deepDiffer(undefined, '')).toBe(true); + }); + it('should diff Objects', () => { + expect(deepDiffer({}, {})).toBe(false); + expect(deepDiffer({}, null)).toBe(true); + expect(deepDiffer(null, {})).toBe(true); + expect(deepDiffer({a: 1}, {a: 1})).toBe(false); + expect(deepDiffer({a: 1}, {a: 2})).toBe(true); + expect(deepDiffer({a: 1}, {a: 1, b: null})).toBe(true); + expect(deepDiffer({a: 1}, {a: 1, b: 1})).toBe(true); + expect(deepDiffer({a: 1, b: 1}, {a: 1})).toBe(true); + expect(deepDiffer({a: {A: 1}, b: 1}, {a: {A: 1}, b: 1})).toBe(false); + expect(deepDiffer({a: {A: 1}, b: 1}, {a: {A: 2}, b: 1})).toBe(true); + expect(deepDiffer( + {a: {A: {aA: 1, bB: 1}}, b: 1}, + {a: {A: {aA: 1, bB: 1}}, b: 1} + )).toBe(false); + expect(deepDiffer( + {a: {A: {aA: 1, bB: 1}}, b: 1}, + {a: {A: {aA: 1, cC: 1}}, b: 1} + )).toBe(true); + }); + it('should diff Arrays', () => { + expect(deepDiffer([], [])).toBe(false); + expect(deepDiffer([], null)).toBe(true); + expect(deepDiffer(null, [])).toBe(true); + expect(deepDiffer([42], [42])).toBe(false); + expect(deepDiffer([1], [2])).toBe(true); + expect(deepDiffer([1, 2, 3], [1, 2, 3])).toBe(false); + expect(deepDiffer([1, 2, 3], [1, 2, 4])).toBe(true); + expect(deepDiffer([1, 2, 3], [1, 4, 3])).toBe(true); + expect(deepDiffer([1, 2, 3, 4], [1, 2, 3])).toBe(true); + expect(deepDiffer([1, 2, 3], [1, 2, 3, 4])).toBe(true); + expect(deepDiffer([0, null, false, ''], [0, null, false, ''])).toBe(false); + expect(deepDiffer([0, null, false, ''], ['', false, null, 0])).toBe(true); + }); + it('should diff mixed types', () => { + expect(deepDiffer({}, [])).toBe(true); + expect(deepDiffer([], {})).toBe(true); + expect(deepDiffer( + {a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]}, + {a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]} + )).toBe(false); + expect(deepDiffer( + {a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]}, + {a: [{A: {aA: 1, bB: 2}}, 'bar'], c: [1, [false]]} + )).toBe(true); + expect(deepDiffer( + {a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]}, + {a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false], null]} + )).toBe(true); + expect(deepDiffer( + {a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]}, + {a: [{A: {aA: 1, bB: 1}}, ['bar']], c: [1, [false]]} + )).toBe(true); + }); + it('should distinguish between proper Array and Object', () => { + expect(deepDiffer(['a', 'b'], {0: 'a', 1: 'b', length: 2})).toBe(true); + expect(deepDiffer(['a', 'b'], {length: 2, 0: 'a', 1: 'b'})).toBe(true); + }); + it('should diff same object', () => { + var obj = [1,[2,3]]; + expect(deepDiffer(obj, obj)).toBe(false); + }); +}); diff --git a/Libraries/Utilities/differ/deepDiffer.js b/Libraries/Utilities/differ/deepDiffer.js index 66dc7fa95..4dec6bd7b 100644 --- a/Libraries/Utilities/differ/deepDiffer.js +++ b/Libraries/Utilities/differ/deepDiffer.js @@ -35,16 +35,29 @@ var deepDiffer = function(one: any, two: any): bool { if (one.constructor !== two.constructor) { return true; } - for (var key in one) { - if (deepDiffer(one[key], two[key])) { + if (Array.isArray(one)) { + // We know two is also an array because the constructors are equal + var len = one.length; + if (two.length !== len) { return true; } - } - for (var twoKey in two) { - // The only case we haven't checked yet is keys that are in two but aren't - // in one, which means they are different. - if (one[twoKey] === undefined && two[twoKey] !== undefined) { - return true; + for (var ii = 0; ii < len; ii++) { + if (deepDiffer(one[ii], two[ii])) { + return true; + } + } + } else { + for (var key in one) { + if (deepDiffer(one[key], two[key])) { + return true; + } + } + for (var twoKey in two) { + // The only case we haven't checked yet is keys that are in two but aren't + // in one, which means they are different. + if (one[twoKey] === undefined && two[twoKey] !== undefined) { + return true; + } } } return false;