-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add isEqual and isNotEqual utility #4603
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
Merged
dzearing
merged 19 commits into
microsoft:master
from
jordandrako:feature/isEqual-function
Apr 26, 2018
Merged
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5a06fcd
Add `isEqual` function to:
2017ccd
Add unit tests for `isEqual`
2acc9de
move set up consts to describe blocks
ed9bd32
export isEqual
21915e5
Add isNotEqual and isNotEqual tests
b3695c8
Use new helper to Fix #3607
3881c7e
changes
a189647
Update jsdock
jordandrako 9493ccf
nit
jordandrako 1193a54
nit
jordandrako 09376d8
Fix itchy copy+paste trigger finger
598a3a0
Add string|number check.
a554071
Remove useless else statements
7471963
Fix "the NOT same" to "NOT the same"
cliffkoh 9729655
Better jsdoc
5ab9ec5
tsk tsk tsk
cbee457
more tsk
97ea2bc
Merge branch 'master' into feature/isEqual-function
e309850
Merge branch 'master' into feature/isEqual-function
cliffkoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
common/changes/@uifabric/utilities/feature-isEqual-function_2018-04-18-21-10.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@uifabric/utilities", | ||
| "comment": "Add isEqual and isNotEqual utilities", | ||
| "type": "patch" | ||
| } | ||
| ], | ||
| "packageName": "@uifabric/utilities", | ||
| "email": "v-jojanz@microsoft.com" | ||
| } |
11 changes: 11 additions & 0 deletions
11
common/changes/office-ui-fabric-react/feature-isEqual-function_2018-04-18-21-10.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { isEqual, isNotEqual } from './isEqual'; | ||
|
|
||
| describe('isEquals and isNotEquals function helper', () => { | ||
| 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']; | ||
|
|
||
| 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 types', () => { | ||
| expect(isEqual(arrayBase, arrayDifferentTypes)).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); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| 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 objectDifferentValueTypes = { a: '1', b: 2 }; | ||
| const objectDifferentKeyTypes = { 1: '1', 2: '2' }; | ||
| const objectDifferentLength = { a: '1', b: '2', c: '3' }; | ||
|
|
||
| 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 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); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isNotEqual', () => { | ||
| it('returns true with objects with the same values', () => { | ||
| expect(isNotEqual(objectBase, objectControl)).toEqual(false); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Test numbers', () => { | ||
| // Set up numbers to compare | ||
| const numberBase = 123; | ||
| const numberControl = 123; | ||
| const numberDifferentValues = 1234; | ||
| const numberDifferentTypes = '123'; | ||
|
|
||
| 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 types', () => { | ||
| expect(isEqual(numberBase, numberDifferentTypes)).toEqual(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isNotEqual', () => { | ||
| it('returns true with numbers with the same values', () => { | ||
| expect(isNotEqual(numberBase, numberControl)).toEqual(false); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| 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'; | ||
|
|
||
| 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); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isNotEqual', () => { | ||
| it('returns true with strings with the same values', () => { | ||
| expect(isNotEqual(stringBase, stringControl)).toEqual(false); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /** | ||
| * 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; | ||
| } | ||
| // 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 | ||
| 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)) { | ||
| 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; | ||
| }; | ||
|
|
||
| /** | ||
| * 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); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have copy and paste issues unfortunately :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also for
isNotEqual, you probably want to fix the doc to sayare NOT the same, recursively.instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, thanks for catching that. Fixed my itchy copy+paste trigger finger.