From 5a06fcdd3923a34845f318592b51bfb175cb5ccc Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 12:47:47 -0700 Subject: [PATCH 01/17] Add `isEqual` function to: - compare values and types of arrays, objects, numbers, and strings --- packages/utilities/src/isEqual.ts | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 packages/utilities/src/isEqual.ts diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts new file mode 100644 index 00000000000000..6cb5de8c0efa79 --- /dev/null +++ b/packages/utilities/src/isEqual.ts @@ -0,0 +1,73 @@ +// tslint:disable-next-line no-any +export const isEqual = (itemA: any, itemB: any): boolean => { + if (itemA === itemB) { + return true; + } + // Get the value type + const type = Object.prototype.toString.call(itemA); + // If the two objects are not the same type, return false + if (type !== Object.prototype.toString.call(itemB)) { + return false; + } + // If items are not an object or array, return false + if (['[object Array]', '[object Object]'].indexOf(type) < 0) { + return false; + } + // Compare the length of the length of the two items + const valueLen = type === '[object Array]' ? itemA.length : Object.keys(itemA).length; + const otherLen = type === '[object Array]' ? itemB.length : Object.keys(itemB).length; + if (valueLen !== otherLen) { + return false; + } + // Compare two items + // tslint:disable-next-line no-any + const compare = (item1: any, item2: any) => { + + // Get the object type + const itemType = Object.prototype.toString.call(item1); + + // If an object or array, compare recursively + if (['[object Array]', '[object Object]'].indexOf(itemType) >= 0) { + if (!isEqual(item1, item2)) { + return false; + } + } else { + + // If the two items are not the same type, return false + if (itemType !== Object.prototype.toString.call(item2)) { + return false; + } + + // Else if it's a function, convert to a string and compare + // Otherwise, just compare + if (itemType === '[object Function]') { + if (item1.toString() !== item2.toString()) { + return false; + } + } else { + if (item1 !== item2) { + return false; + } + } + + } + }; + // Compare properties + if (type === '[object Array]') { + for (let i = 0; i < valueLen; i++) { + if (compare(itemA[i], itemB[i]) === false) { + return false; + } + } + } else { + for (const key in itemA) { + if (itemA.hasOwnProperty(key)) { + if (compare(itemA[key], itemB[key]) === false) { + return false; + } + } + } + } + // If nothing failed, return true + return true; +}; \ No newline at end of file From 2017ccd84b775cbd646cc99245cfcc44be28510c Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 12:48:02 -0700 Subject: [PATCH 02/17] Add unit tests for `isEqual` --- packages/utilities/src/isEqual.test.ts | 84 ++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 packages/utilities/src/isEqual.test.ts diff --git a/packages/utilities/src/isEqual.test.ts b/packages/utilities/src/isEqual.test.ts new file mode 100644 index 00000000000000..9782d8570b609f --- /dev/null +++ b/packages/utilities/src/isEqual.test.ts @@ -0,0 +1,84 @@ +import { isEqual } from './isEqual'; + +const arrayBase = ['1', '2', '3']; +const arrayControl = ['1', '2', '3']; +const arrayDifferentValues = ['1', '2', '4']; +const arrayDifferentTypes = ['1', '2', 3]; +const arrayDifferentLength = ['1', '2', '3', '4']; + +const objectBase = { a: '1', b: '2' }; +const objectControl = { a: '1', b: '2' }; +const objectDifferentValues = { a: '1', b: '3' }; +const objectDifferentTypes = { a: '1', b: 2 }; +const objectDifferentLength = { a: '1', b: '2', c: '3' }; + +const numberBase = 123; +const numberControl = 123; +const numberDifferentValues = 1234; +const numberDifferentTypes = '123'; + +const stringBase = 'This is a string'; +const stringControl = 'This is a string'; +const stringDifferentValues = 'This is a string that isn\'t the same'; + +describe('isEquals function helper', () => { + describe('test indexes', () => { + it('returns true with arrays with the same values', () => { + expect(isEqual(arrayBase, arrayControl)).toEqual(true); + }); + + it('returns false with arrays with different values', () => { + expect(isEqual(arrayBase, arrayDifferentValues)).toEqual(false); + }); + + it('returns false with arrays with different types', () => { + expect(isEqual(arrayBase, arrayDifferentTypes)).toEqual(false); + }); + + it('returns false with arrays with different length', () => { + expect(isEqual(arrayBase, arrayDifferentLength)).toEqual(false); + }); + }); + + describe('test objects', () => { + it('returns true with objects with the same values', () => { + expect(isEqual(objectBase, objectControl)).toEqual(true); + }); + + it('returns false with objects with different values', () => { + expect(isEqual(objectBase, objectDifferentValues)).toEqual(false); + }); + + it('returns false with objects with different types', () => { + expect(isEqual(objectBase, objectDifferentTypes)).toEqual(false); + }); + + it('returns false with objects with different length', () => { + expect(isEqual(objectBase, objectDifferentLength)).toEqual(false); + }); + }); + + describe('test numbers', () => { + it('returns true with numbers with the same values', () => { + expect(isEqual(numberBase, numberControl)).toEqual(true); + }); + + it('returns false with numbers with different values', () => { + expect(isEqual(numberBase, numberDifferentValues)).toEqual(false); + }); + + it('returns false with numbers with different types', () => { + expect(isEqual(numberBase, numberDifferentTypes)).toEqual(false); + }); + }); + + describe('test strings', () => { + it('returns true with strings with the same values', () => { + expect(isEqual(stringBase, stringControl)).toEqual(true); + }); + + it('returns false with strings with different values', () => { + expect(isEqual(stringBase, stringDifferentValues)).toEqual(false); + }); + }); +}); \ No newline at end of file From 2acc9de92960f0d7cba2af78fd16b3dd6b70de3b Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 12:59:00 -0700 Subject: [PATCH 03/17] move set up consts to describe blocks --- packages/utilities/src/isEqual.test.ts | 56 ++++++++++++++------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/packages/utilities/src/isEqual.test.ts b/packages/utilities/src/isEqual.test.ts index 9782d8570b609f..e00a45c02e01b3 100644 --- a/packages/utilities/src/isEqual.test.ts +++ b/packages/utilities/src/isEqual.test.ts @@ -1,28 +1,14 @@ import { isEqual } from './isEqual'; -const arrayBase = ['1', '2', '3']; -const arrayControl = ['1', '2', '3']; -const arrayDifferentValues = ['1', '2', '4']; -const arrayDifferentTypes = ['1', '2', 3]; -const arrayDifferentLength = ['1', '2', '3', '4']; - -const objectBase = { a: '1', b: '2' }; -const objectControl = { a: '1', b: '2' }; -const objectDifferentValues = { a: '1', b: '3' }; -const objectDifferentTypes = { a: '1', b: 2 }; -const objectDifferentLength = { a: '1', b: '2', c: '3' }; - -const numberBase = 123; -const numberControl = 123; -const numberDifferentValues = 1234; -const numberDifferentTypes = '123'; - -const stringBase = 'This is a string'; -const stringControl = 'This is a string'; -const stringDifferentValues = 'This is a string that isn\'t the same'; - describe('isEquals function helper', () => { - describe('test indexes', () => { + describe('Test indexes', () => { + // Set up arrays to compare + const arrayBase = ['1', '2', '3']; + const arrayControl = ['1', '2', '3']; + const arrayDifferentValues = ['1', '2', '4']; + const arrayDifferentTypes = ['1', '2', 3]; + const arrayDifferentLength = ['1', '2', '3', '4']; + it('returns true with arrays with the same values', () => { expect(isEqual(arrayBase, arrayControl)).toEqual(true); }); @@ -40,7 +26,14 @@ describe('isEquals function helper', () => { }); }); - describe('test objects', () => { + describe('Test objects', () => { + // Set up objects to compare + const objectBase = { a: '1', b: '2' }; + const objectControl = { a: '1', b: '2' }; + const objectDifferentValues = { a: '1', b: '3' }; + const objectDifferentTypes = { a: '1', b: 2 }; + const objectDifferentLength = { a: '1', b: '2', c: '3' }; + it('returns true with objects with the same values', () => { expect(isEqual(objectBase, objectControl)).toEqual(true); }); @@ -58,7 +51,13 @@ describe('isEquals function helper', () => { }); }); - describe('test numbers', () => { + describe('Test numbers', () => { + // Set up numbers to compare + const numberBase = 123; + const numberControl = 123; + const numberDifferentValues = 1234; + const numberDifferentTypes = '123'; + it('returns true with numbers with the same values', () => { expect(isEqual(numberBase, numberControl)).toEqual(true); }); @@ -72,7 +71,12 @@ describe('isEquals function helper', () => { }); }); - describe('test strings', () => { + describe('Test strings', () => { + // Set up strings to compare + const stringBase = 'This is a string'; + const stringControl = 'This is a string'; + const stringDifferentValues = 'This is a string that isn\'t the same'; + it('returns true with strings with the same values', () => { expect(isEqual(stringBase, stringControl)).toEqual(true); }); @@ -81,4 +85,4 @@ describe('isEquals function helper', () => { expect(isEqual(stringBase, stringDifferentValues)).toEqual(false); }); }); -}); \ No newline at end of file +}); From ed9bd32e9692f80b7bbb92efe97be9ee9d10e404 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 13:00:30 -0700 Subject: [PATCH 04/17] export isEqual --- packages/utilities/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/utilities/src/index.ts b/packages/utilities/src/index.ts index 7af871a0b12c18..b44cfa4458a846 100644 --- a/packages/utilities/src/index.ts +++ b/packages/utilities/src/index.ts @@ -41,4 +41,5 @@ export * from './scroll'; export * from './string'; export * from './styled'; export * from './warn'; -export * from './createRef'; \ No newline at end of file +export * from './createRef'; +export * from './isEqual'; \ No newline at end of file From 21915e5338b04d6060f4b71515460d093bf52ffc Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 14:01:20 -0700 Subject: [PATCH 05/17] Add isNotEqual and isNotEqual tests --- packages/utilities/src/isEqual.test.ts | 105 +++++++++++++++++-------- packages/utilities/src/isEqual.ts | 9 +++ 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/packages/utilities/src/isEqual.test.ts b/packages/utilities/src/isEqual.test.ts index e00a45c02e01b3..49868aaaed5785 100644 --- a/packages/utilities/src/isEqual.test.ts +++ b/packages/utilities/src/isEqual.test.ts @@ -1,6 +1,6 @@ -import { isEqual } from './isEqual'; +import { isEqual, isNotEqual } from './isEqual'; -describe('isEquals function helper', () => { +describe('isEquals and isNotEquals function helper', () => { describe('Test indexes', () => { // Set up arrays to compare const arrayBase = ['1', '2', '3']; @@ -9,20 +9,28 @@ describe('isEquals function helper', () => { const arrayDifferentTypes = ['1', '2', 3]; const arrayDifferentLength = ['1', '2', '3', '4']; - it('returns true with arrays with the same values', () => { - expect(isEqual(arrayBase, arrayControl)).toEqual(true); - }); + describe('isEqual', () => { + it('returns true with arrays with the same values', () => { + expect(isEqual(arrayBase, arrayControl)).toEqual(true); + }); - it('returns false with arrays with different values', () => { - expect(isEqual(arrayBase, arrayDifferentValues)).toEqual(false); - }); + it('returns false with arrays with different values', () => { + expect(isEqual(arrayBase, arrayDifferentValues)).toEqual(false); + }); + + it('returns false with arrays with different types', () => { + expect(isEqual(arrayBase, arrayDifferentTypes)).toEqual(false); + }); - it('returns false with arrays with different types', () => { - expect(isEqual(arrayBase, arrayDifferentTypes)).toEqual(false); + it('returns false with arrays with different length', () => { + expect(isEqual(arrayBase, arrayDifferentLength)).toEqual(false); + }); }); - it('returns false with arrays with different length', () => { - expect(isEqual(arrayBase, arrayDifferentLength)).toEqual(false); + describe('isNotEqual', () => { + it('returns true with arrays with the same values', () => { + expect(isNotEqual(arrayBase, arrayControl)).toEqual(false); + }); }); }); @@ -31,23 +39,36 @@ describe('isEquals function helper', () => { const objectBase = { a: '1', b: '2' }; const objectControl = { a: '1', b: '2' }; const objectDifferentValues = { a: '1', b: '3' }; - const objectDifferentTypes = { a: '1', b: 2 }; + const objectDifferentValueTypes = { a: '1', b: 2 }; + const objectDifferentKeyTypes = { 1: '1', 2: '2' }; const objectDifferentLength = { a: '1', b: '2', c: '3' }; - it('returns true with objects with the same values', () => { - expect(isEqual(objectBase, objectControl)).toEqual(true); - }); + describe('isEqual', () => { + it('returns true with objects with the same values', () => { + expect(isEqual(objectBase, objectControl)).toEqual(true); + }); - it('returns false with objects with different values', () => { - expect(isEqual(objectBase, objectDifferentValues)).toEqual(false); - }); + it('returns false with objects with different values', () => { + expect(isEqual(objectBase, objectDifferentValues)).toEqual(false); + }); - it('returns false with objects with different types', () => { - expect(isEqual(objectBase, objectDifferentTypes)).toEqual(false); + it('returns false with objects with different value types', () => { + expect(isEqual(objectBase, objectDifferentValueTypes)).toEqual(false); + }); + + it('returns false with objects with different key types', () => { + expect(isEqual(objectBase, objectDifferentKeyTypes)).toEqual(false); + }); + + it('returns false with objects with different length', () => { + expect(isEqual(objectBase, objectDifferentLength)).toEqual(false); + }); }); - it('returns false with objects with different length', () => { - expect(isEqual(objectBase, objectDifferentLength)).toEqual(false); + describe('isNotEqual', () => { + it('returns true with objects with the same values', () => { + expect(isNotEqual(objectBase, objectControl)).toEqual(false); + }); }); }); @@ -58,16 +79,24 @@ describe('isEquals function helper', () => { const numberDifferentValues = 1234; const numberDifferentTypes = '123'; - it('returns true with numbers with the same values', () => { - expect(isEqual(numberBase, numberControl)).toEqual(true); - }); + describe('isEqual', () => { + it('returns true with numbers with the same values', () => { + expect(isEqual(numberBase, numberControl)).toEqual(true); + }); - it('returns false with numbers with different values', () => { - expect(isEqual(numberBase, numberDifferentValues)).toEqual(false); + it('returns false with numbers with different values', () => { + expect(isEqual(numberBase, numberDifferentValues)).toEqual(false); + }); + + it('returns false with numbers with different types', () => { + expect(isEqual(numberBase, numberDifferentTypes)).toEqual(false); + }); }); - it('returns false with numbers with different types', () => { - expect(isEqual(numberBase, numberDifferentTypes)).toEqual(false); + describe('isNotEqual', () => { + it('returns true with numbers with the same values', () => { + expect(isNotEqual(numberBase, numberControl)).toEqual(false); + }); }); }); @@ -77,12 +106,20 @@ describe('isEquals function helper', () => { const stringControl = 'This is a string'; const stringDifferentValues = 'This is a string that isn\'t the same'; - it('returns true with strings with the same values', () => { - expect(isEqual(stringBase, stringControl)).toEqual(true); + describe('isEqual', () => { + it('returns true with strings with the same values', () => { + expect(isEqual(stringBase, stringControl)).toEqual(true); + }); + + it('returns false with strings with different values', () => { + expect(isEqual(stringBase, stringDifferentValues)).toEqual(false); + }); }); - it('returns false with strings with different values', () => { - expect(isEqual(stringBase, stringDifferentValues)).toEqual(false); + describe('isNotEqual', () => { + it('returns true with strings with the same values', () => { + expect(isNotEqual(stringBase, stringControl)).toEqual(false); + }); }); }); }); diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 6cb5de8c0efa79..fa060ef384a7f6 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -70,4 +70,13 @@ export const isEqual = (itemA: any, itemB: any): boolean => { } // If nothing failed, return true return true; +}; + +/** + * Checks if the first and second array|object are the NOT same recursively. Use for checking arrays and objects. + * @param itemA First array|object to compare to second array|object + * @param itemB Second array|object to compare to first array|object + */ +export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any + return !isEqual(itemA, itemB); }; \ No newline at end of file From b3695c8b11c88d888ebd21ae1244e1a065e8a5cd Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 14:01:55 -0700 Subject: [PATCH 06/17] Use new helper to Fix #3607 --- packages/office-ui-fabric-react/src/components/List/List.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/office-ui-fabric-react/src/components/List/List.tsx b/packages/office-ui-fabric-react/src/components/List/List.tsx index 9e8a2749e7cab9..40616d87a46d8e 100644 --- a/packages/office-ui-fabric-react/src/components/List/List.tsx +++ b/packages/office-ui-fabric-react/src/components/List/List.tsx @@ -9,7 +9,8 @@ import { divProperties, getNativeProps, IRenderFunction, - createRef + createRef, + isEqual, } from '../../Utilities'; import { IList, IListProps, IPage, IPageProps } from './List.types'; @@ -299,7 +300,7 @@ export class List extends BaseComponent implements IList return true; } - if (newProps.items === this.props.items && + if (isEqual(newProps.items, this.props.items) && oldPages!.length === newPages!.length) { for (let i = 0; i < oldPages!.length; i++) { const oldPage = oldPages![i]; From 3881c7e6fbbd520d88c5e09825befd06635026fe Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 14:10:45 -0700 Subject: [PATCH 07/17] changes --- .../feature-isEqual-function_2018-04-18-21-10.json | 11 +++++++++++ .../feature-isEqual-function_2018-04-18-21-10.json | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 common/changes/@uifabric/utilities/feature-isEqual-function_2018-04-18-21-10.json create mode 100644 common/changes/office-ui-fabric-react/feature-isEqual-function_2018-04-18-21-10.json diff --git a/common/changes/@uifabric/utilities/feature-isEqual-function_2018-04-18-21-10.json b/common/changes/@uifabric/utilities/feature-isEqual-function_2018-04-18-21-10.json new file mode 100644 index 00000000000000..a20d4289cbd7c7 --- /dev/null +++ b/common/changes/@uifabric/utilities/feature-isEqual-function_2018-04-18-21-10.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@uifabric/utilities", + "comment": "Add isEqual and isNotEqual utilities", + "type": "patch" + } + ], + "packageName": "@uifabric/utilities", + "email": "v-jojanz@microsoft.com" +} \ No newline at end of file diff --git a/common/changes/office-ui-fabric-react/feature-isEqual-function_2018-04-18-21-10.json b/common/changes/office-ui-fabric-react/feature-isEqual-function_2018-04-18-21-10.json new file mode 100644 index 00000000000000..e09bda92643719 --- /dev/null +++ b/common/changes/office-ui-fabric-react/feature-isEqual-function_2018-04-18-21-10.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "office-ui-fabric-react", + "comment": "Fix #3607", + "type": "patch" + } + ], + "packageName": "office-ui-fabric-react", + "email": "v-jojanz@microsoft.com" +} \ No newline at end of file From a189647ff3fa50e4a8031015e6c16c005501b272 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 18:26:42 -0700 Subject: [PATCH 08/17] Update jsdock --- packages/utilities/src/isEqual.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index fa060ef384a7f6..5c8cf6be2e445a 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -1,5 +1,9 @@ -// tslint:disable-next-line no-any -export const isEqual = (itemA: any, itemB: any): boolean => { +/** + * Checks if the first and second items are the NOT same recursively. Use for checking arrays and objects. + * @param itemA First item to compare to second item + * @param itemB Second item to compare to first item + */ +export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any if (itemA === itemB) { return true; } @@ -20,8 +24,7 @@ export const isEqual = (itemA: any, itemB: any): boolean => { return false; } // Compare two items - // tslint:disable-next-line no-any - const compare = (item1: any, item2: any) => { + const compare = (item1: any, item2: any) => { // tslint:disable-line no-any // Get the object type const itemType = Object.prototype.toString.call(item1); @@ -73,9 +76,9 @@ export const isEqual = (itemA: any, itemB: any): boolean => { }; /** - * Checks if the first and second array|object are the NOT same recursively. Use for checking arrays and objects. - * @param itemA First array|object to compare to second array|object - * @param itemB Second array|object to compare to first array|object + * Checks if the first and second items are the NOT same recursively. Use for checking arrays and objects. + * @param itemA First item to compare to second item + * @param itemB Second item to compare to first item */ export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any return !isEqual(itemA, itemB); From 9493ccf38cf8cf66d62992d0731baf108c561f5c Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 18:28:20 -0700 Subject: [PATCH 09/17] nit --- packages/utilities/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utilities/src/index.ts b/packages/utilities/src/index.ts index b44cfa4458a846..346fbc54346d23 100644 --- a/packages/utilities/src/index.ts +++ b/packages/utilities/src/index.ts @@ -42,4 +42,4 @@ export * from './string'; export * from './styled'; export * from './warn'; export * from './createRef'; -export * from './isEqual'; \ No newline at end of file +export * from './isEqual'; From 1193a54e5e10370e4ef611f7c55dda81806925d2 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Wed, 18 Apr 2018 18:29:07 -0700 Subject: [PATCH 10/17] nit --- packages/utilities/src/isEqual.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 5c8cf6be2e445a..d4b0fe08677a3c 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -82,4 +82,4 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- */ export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any return !isEqual(itemA, itemB); -}; \ No newline at end of file +}; From 09376d880f20d83922e657e25f4170585aa61ac0 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 19 Apr 2018 11:18:35 -0700 Subject: [PATCH 11/17] Fix itchy copy+paste trigger finger --- packages/utilities/src/isEqual.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index d4b0fe08677a3c..990e7fa39b59f1 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -1,5 +1,5 @@ /** - * Checks if the first and second items are the NOT same recursively. Use for checking arrays and objects. + * Checks if the first and second items are the same, recursively. Use for checking arrays and objects. * @param itemA First item to compare to second item * @param itemB Second item to compare to first item */ @@ -76,7 +76,7 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- }; /** - * Checks if the first and second items are the NOT same recursively. Use for checking arrays and objects. + * Checks if the first and second items are the NOT same, recursively. Use for checking arrays and objects. * @param itemA First item to compare to second item * @param itemB Second item to compare to first item */ From 598a3a0d2b2b38fb1731aacfa8ebdf5d06925f3b Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 19 Apr 2018 11:19:47 -0700 Subject: [PATCH 12/17] Add string|number check. --- packages/utilities/src/isEqual.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 990e7fa39b59f1..39a534c4b41c48 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -4,8 +4,12 @@ * @param itemB Second item to compare to first item */ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any + // First, a simple check for strings and numbers + if (typeof itemA === 'string' || typeof itemA === 'number') { if (itemA === itemB) { return true; + } + return false; } // Get the value type const type = Object.prototype.toString.call(itemA); From a55407160d57df54f34f13768917d7697c1be437 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 19 Apr 2018 11:20:17 -0700 Subject: [PATCH 13/17] Remove useless else statements --- packages/utilities/src/isEqual.ts | 48 +++++++++++++------------------ 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 39a534c4b41c48..6d3acdcc44e032 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -6,9 +6,9 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any // First, a simple check for strings and numbers if (typeof itemA === 'string' || typeof itemA === 'number') { - if (itemA === itemB) { - return true; - } + if (itemA === itemB) { + return true; + } return false; } // Get the value type @@ -29,34 +29,27 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- } // Compare two items const compare = (item1: any, item2: any) => { // tslint:disable-line no-any - // Get the object type const itemType = Object.prototype.toString.call(item1); - // If an object or array, compare recursively if (['[object Array]', '[object Object]'].indexOf(itemType) >= 0) { if (!isEqual(item1, item2)) { return false; } - } else { - - // If the two items are not the same type, return false - if (itemType !== Object.prototype.toString.call(item2)) { + } + // If the two items are not the same type, return false + if (itemType !== Object.prototype.toString.call(item2)) { + return false; + } + // Else if it's a function, convert to a string and compare + // Otherwise, just compare + if (itemType === '[object Function]') { + if (item1.toString() !== item2.toString()) { return false; } - - // Else if it's a function, convert to a string and compare - // Otherwise, just compare - if (itemType === '[object Function]') { - if (item1.toString() !== item2.toString()) { - return false; - } - } else { - if (item1 !== item2) { - return false; - } - } - + } + if (item1 !== item2) { + return false; } }; // Compare properties @@ -66,12 +59,11 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- return false; } } - } else { - for (const key in itemA) { - if (itemA.hasOwnProperty(key)) { - if (compare(itemA[key], itemB[key]) === false) { - return false; - } + } + for (const key in itemA) { + if (itemA.hasOwnProperty(key)) { + if (compare(itemA[key], itemB[key]) === false) { + return false; } } } From 747196325dc3dfbd5385fb060b5bda92cb1ad455 Mon Sep 17 00:00:00 2001 From: Cliff Koh Date: Thu, 19 Apr 2018 12:23:44 -0700 Subject: [PATCH 14/17] Fix "the NOT same" to "NOT the same" Fix minor English issue. --- packages/utilities/src/isEqual.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 6d3acdcc44e032..391545bfc2a74b 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -1,7 +1,7 @@ /** * Checks if the first and second items are the same, recursively. Use for checking arrays and objects. - * @param itemA First item to compare to second item - * @param itemB Second item to compare to first item + * @param itemA First item to compare to second item. + * @param itemB Second item to compare to first item. */ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any // First, a simple check for strings and numbers @@ -72,9 +72,9 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- }; /** - * Checks if the first and second items are the NOT same, recursively. Use for checking arrays and objects. - * @param itemA First item to compare to second item - * @param itemB Second item to compare to first item + * Checks if the first and second items are NOT the same, recursively. Use for checking arrays and objects. + * @param itemA First item to compare to second item. + * @param itemB Second item to compare to first item. */ export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any return !isEqual(itemA, itemB); From 9729655d167029579d7a5a044e3a98de2c894d90 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 19 Apr 2018 12:40:49 -0700 Subject: [PATCH 15/17] Better jsdoc --- packages/utilities/src/isEqual.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 391545bfc2a74b..b7162579cb8bbf 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -1,7 +1,9 @@ /** * Checks if the first and second items are the same, recursively. Use for checking arrays and objects. - * @param itemA First item to compare to second item. - * @param itemB Second item to compare to first item. + * + * @param itemA First item to compare to second item + * @param itemB Second item to compare to first item + * @returns {boolean} True if items are the same or false if not. */ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any // First, a simple check for strings and numbers @@ -72,9 +74,11 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- }; /** - * Checks if the first and second items are NOT the same, recursively. Use for checking arrays and objects. - * @param itemA First item to compare to second item. - * @param itemB Second item to compare to first item. + * Checks if the first and second items are the NOT same, recursively. Use for checking arrays and objects. + * + * @param itemA First item to compare to second item + * @param itemB Second item to compare to first item + * @returns {boolean} True if items are the NOT same or false if they are. */ export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any return !isEqual(itemA, itemB); From 5ab9ec5ed655eed7a5c29acdb18e29eb8fa54ac8 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 19 Apr 2018 12:58:58 -0700 Subject: [PATCH 16/17] tsk tsk tsk --- packages/utilities/src/isEqual.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index b7162579cb8bbf..45b7561e5325c2 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -74,11 +74,11 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- }; /** - * Checks if the first and second items are the NOT same, recursively. Use for checking arrays and objects. + * Checks if the first and second items are NOT the same, recursively. Use for checking arrays and objects. * * @param itemA First item to compare to second item * @param itemB Second item to compare to first item - * @returns {boolean} True if items are the NOT same or false if they are. + * @returns {boolean} True if items are NOT the same or false if they are. */ export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any return !isEqual(itemA, itemB); From cbee45700a5574c689b81370894c80b06dce0ff4 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 19 Apr 2018 13:00:27 -0700 Subject: [PATCH 17/17] more tsk --- packages/utilities/src/isEqual.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/utilities/src/isEqual.ts b/packages/utilities/src/isEqual.ts index 45b7561e5325c2..78e0a8edee3870 100644 --- a/packages/utilities/src/isEqual.ts +++ b/packages/utilities/src/isEqual.ts @@ -1,8 +1,8 @@ /** * Checks if the first and second items are the same, recursively. Use for checking arrays and objects. * - * @param itemA First item to compare to second item - * @param itemB Second item to compare to first item + * @param itemA First item to compare to second item. + * @param itemB Second item to compare to first item. * @returns {boolean} True if items are the same or false if not. */ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any @@ -76,8 +76,8 @@ export const isEqual = (itemA: any, itemB: any): boolean => { // tslint:disable- /** * Checks if the first and second items are NOT the same, recursively. Use for checking arrays and objects. * - * @param itemA First item to compare to second item - * @param itemB Second item to compare to first item + * @param itemA First item to compare to second item. + * @param itemB Second item to compare to first item. * @returns {boolean} True if items are NOT the same or false if they are. */ export const isNotEqual = (itemA: any, itemB: any): boolean => { // tslint:disable-line no-any