-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core Data: Add util for minimally modifying items. (#16823)
- Loading branch information
Showing
3 changed files
with
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEqual } from 'lodash'; | ||
|
||
/** | ||
* Given the current and next item entity, returns the minimally "modified" | ||
* result of the next item, preferring value references from the original item | ||
* if equal. If all values match, the original item is returned. | ||
* | ||
* @param {Object} item Original item. | ||
* @param {Object} nextItem Next item. | ||
* | ||
* @return {Object} Minimally modified merged item. | ||
*/ | ||
export default function conservativeMapItem( item, nextItem ) { | ||
// Return next item in its entirety if there is no original item. | ||
if ( ! item ) { | ||
return nextItem; | ||
} | ||
|
||
let hasChanges = false; | ||
const result = {}; | ||
for ( const key in nextItem ) { | ||
if ( isEqual( item[ key ], nextItem[ key ] ) ) { | ||
result[ key ] = item[ key ]; | ||
} else { | ||
hasChanges = true; | ||
result[ key ] = nextItem[ key ]; | ||
} | ||
} | ||
|
||
if ( ! hasChanges ) { | ||
return item; | ||
} | ||
return result; | ||
} |
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
33 changes: 33 additions & 0 deletions
33
packages/core-data/src/utils/test/conservative-map-item.js
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,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import conservativeMapItem from '../conservative-map-item'; | ||
|
||
describe( 'conservativeMapItem', () => { | ||
it( 'Returns the next item if there is no current item to compare with.', () => { | ||
const item = undefined; | ||
const nextItem = {}; | ||
const result = conservativeMapItem( item, nextItem ); | ||
|
||
expect( result ).toBe( nextItem ); | ||
} ); | ||
|
||
it( 'Returns the original item if all property values are the same, deeply.', () => { | ||
const item = { a: [ {} ] }; | ||
const nextItem = { a: [ {} ] }; | ||
const result = conservativeMapItem( item, nextItem ); | ||
|
||
expect( result ).toBe( item ); | ||
} ); | ||
|
||
it( 'Preserves original references of property values when unchanged, deeply.', () => { | ||
const item = { a: [ {} ], b: [ 1 ] }; | ||
const nextItem = { a: [ {} ], b: [ 2 ] }; | ||
const result = conservativeMapItem( item, nextItem ); | ||
|
||
expect( result ).not.toBe( item ); | ||
expect( result.a ).toBe( item.a ); | ||
expect( result.b ).toBe( nextItem.b ); | ||
expect( result ).toEqual( { a: [ {} ], b: [ 2 ] } ); | ||
} ); | ||
} ); |