-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from blueflag/feature/replace-equal
add: replaceEqual and replaceEqualDeep
- Loading branch information
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,61 @@ | ||
// @flow | ||
import replaceEqual from '../replaceEqual'; | ||
import {fromJS} from 'immutable'; | ||
|
||
test(`replaceEqual() on object should work`, () => { | ||
let data = { | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
|
||
let newDataSame = { | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
|
||
let newDataDifferent = { | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: undefined | ||
} | ||
}; | ||
|
||
expect(replaceEqual(data)(newDataSame)).toBe(data); | ||
expect(replaceEqual(data)(newDataSame)).toEqual(data); | ||
expect(replaceEqual(data)(newDataDifferent)).toEqual(newDataDifferent); | ||
// ^ when data is different, dont care if it's not strictly equal, deep equality is all that is guaranteed | ||
}); | ||
|
||
test(`replaceEqual() on map should work`, () => { | ||
let data = fromJS({ | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}); | ||
|
||
let newDataSame = fromJS({ | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}); | ||
|
||
let newDataDifferent = fromJS({ | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: undefined | ||
} | ||
}); | ||
|
||
expect(replaceEqual(data)(newDataSame)).toBe(data); | ||
expect(replaceEqual(data)(newDataSame)).toEqual(data); | ||
expect(replaceEqual(data)(newDataDifferent)).toEqual(newDataDifferent); | ||
// ^ when data is different, dont care if it's not strictly equal, deep equality is all that is guaranteed | ||
}); |
This file contains 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,82 @@ | ||
// @flow | ||
import replaceEqualDeep from '../replaceEqualDeep'; | ||
import {fromJS} from 'immutable'; | ||
|
||
test(`replaceEqualDeep() on object should work when top level is deeply equal`, () => { | ||
let data = { | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
|
||
let newDataSame = { | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
|
||
let newDataDifferent = { | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: undefined | ||
} | ||
}; | ||
|
||
expect(replaceEqualDeep(data)(newDataSame)).toBe(data); | ||
expect(replaceEqualDeep(data)(newDataSame)).toEqual(data); | ||
expect(replaceEqualDeep(data)(newDataDifferent)).toEqual(newDataDifferent); | ||
// ^ when data is different, dont care if it's not strictly equal, deep equality is all that is guaranteed | ||
}); | ||
|
||
test(`replaceEqualDeep() on map should work when top level is deeply equal`, () => { | ||
let data = fromJS({ | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}); | ||
|
||
let newDataSame = fromJS({ | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}); | ||
|
||
let newDataDifferent = fromJS({ | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: undefined | ||
} | ||
}); | ||
|
||
expect(replaceEqualDeep(data)(newDataSame)).toBe(data); | ||
expect(replaceEqualDeep(data)(newDataSame)).toEqual(data); | ||
expect(replaceEqualDeep(data)(newDataDifferent)).toEqual(newDataDifferent); | ||
// ^ when data is different, dont care if it's not strictly equal, deep equality is all that is guaranteed | ||
}); | ||
|
||
test(`replaceEqualDeep() on object should work when a child is deeply equal`, () => { | ||
let data = { | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
|
||
let newDataDifferent = { | ||
a: 10, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
|
||
expect(replaceEqualDeep(data)(newDataDifferent)).toEqual(newDataDifferent); | ||
expect(replaceEqualDeep(data)(newDataDifferent)).not.toBe(newDataDifferent); | ||
expect(replaceEqualDeep(data)(newDataDifferent).b).toBe(data.b); | ||
expect(replaceEqualDeep(data)(newDataDifferent).b).toEqual(data.b); | ||
}); |
This file contains 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,10 @@ | ||
// @flow | ||
import prep from './internal/unmutable'; | ||
import equals from './equals'; | ||
|
||
export default prep({ | ||
name: 'replaceEqual', | ||
all: (other: any) => (collection: *): * => { | ||
return equals(other)(collection) ? other : collection; | ||
} | ||
}); |
This file contains 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,28 @@ | ||
// @flow | ||
import prep from './internal/unmutable'; | ||
import equals from './equals'; | ||
import get from './get'; | ||
import map from './map'; | ||
import pipeWith from './pipeWith'; | ||
import isWriteable from './isWriteable'; | ||
|
||
const replaceEqualDeep = (other: any) => (collection: *): * => { | ||
if(equals(other)(collection)) { | ||
return other; | ||
} | ||
if(isWriteable(collection)) { | ||
return pipeWith( | ||
collection, | ||
map((child, key) => { | ||
let otherChild = get(key)(other); | ||
return replaceEqualDeep(otherChild)(child); | ||
}) | ||
); | ||
} | ||
return collection; | ||
}; | ||
|
||
export default prep({ | ||
name: 'replaceEqualDeep', | ||
all: replaceEqualDeep | ||
}); |