-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Indicate nested paths on __experimentalSaveSpecifiedEntityEdits (#54161)
* be able to indicate nested paths on __experimentalSaveSpecifiedEntityEdits * fix test decription * fix test description text Co-authored-by: Jeff Ong <[email protected]> * adding type checking Co-authored-by: Jeff Ong <[email protected]> * Format if statement * Tweak comment wording * update description Co-authored-by: Marin Atanasov <[email protected]> * correct function doc --------- Co-authored-by: Jeff Ong <[email protected]> Co-authored-by: Sarah Norris <[email protected]> Co-authored-by: Marin Atanasov <[email protected]>
- Loading branch information
1 parent
0cf7898
commit 5179b9c
Showing
6 changed files
with
113 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Helper util to return a value from a certain path of the object. | ||
* Path is specified as either: | ||
* - a string of properties, separated by dots, for example: "x.y". | ||
* - an array of properties, for example `[ 'x', 'y' ]`. | ||
* You can also specify a default value in case the result is nullish. | ||
* | ||
* @param {Object} object Input object. | ||
* @param {string|Array} path Path to the object property. | ||
* @param {*} defaultValue Default value if the value at the specified path is undefined. | ||
* @return {*} Value of the object property at the specified path. | ||
*/ | ||
export default function getNestedValue( object, path, defaultValue ) { | ||
if ( | ||
! object || | ||
typeof object !== 'object' || | ||
( typeof path !== 'string' && ! Array.isArray( path ) ) | ||
) { | ||
return object; | ||
} | ||
const normalizedPath = Array.isArray( path ) ? path : path.split( '.' ); | ||
let value = object; | ||
normalizedPath.forEach( ( fieldName ) => { | ||
value = value?.[ fieldName ]; | ||
} ); | ||
return value !== undefined ? value : defaultValue; | ||
} |
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 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import getNestedValue from '../get-nested-value'; | ||
|
||
describe( 'getNestedValue', () => { | ||
it( 'should return the same object unmodified if path is an empty array', () => { | ||
const input = { x: 'y' }; | ||
const result = getNestedValue( input, [] ); | ||
expect( result ).toEqual( input ); | ||
} ); | ||
|
||
it( 'should return the nested value', () => { | ||
const input = { x: { y: { z: 123 } } }; | ||
const result = getNestedValue( input, [ 'x', 'y', 'z' ] ); | ||
|
||
expect( result ).toEqual( 123 ); | ||
} ); | ||
|
||
it( 'should return the nested value if the path is a string', () => { | ||
const input = { x: { y: { z: 123 } } }; | ||
const result = getNestedValue( input, 'x.y.z' ); | ||
|
||
expect( result ).toEqual( 123 ); | ||
} ); | ||
|
||
it( 'should return the shallow value', () => { | ||
const input = { x: { y: { z: 123 } } }; | ||
const result = getNestedValue( input, 'x' ); | ||
|
||
expect( result ).toEqual( { y: { z: 123 } } ); | ||
} ); | ||
|
||
it( 'should return the default value if the nested value is undefined', () => { | ||
const input = { x: { y: { z: undefined } } }; | ||
const result = getNestedValue( input, [ 'x', 'y', 'z' ], 456 ); | ||
|
||
expect( result ).toEqual( 456 ); | ||
} ); | ||
|
||
it( 'should return the nested value if it is different to undefined', () => { | ||
const input = { x: { y: { z: null } } }; | ||
const result = getNestedValue( input, 'x.y.z', 456 ); | ||
|
||
expect( result ).toBeNull(); | ||
} ); | ||
|
||
it( 'should return the default value if the nested value does not exist', () => { | ||
const input = { x: { y: { z: 123 } } }; | ||
const result = getNestedValue( input, [ 'x', 'y', 'z1' ], 456 ); | ||
|
||
expect( result ).toEqual( 456 ); | ||
} ); | ||
|
||
it( 'should return undefined if the nested value does not exist', () => { | ||
const input = { x: { y: { z: 123 } } }; | ||
const result = getNestedValue( input, [ 'x', 'y', 'z1' ] ); | ||
|
||
expect( result ).toBeUndefined(); | ||
} ); | ||
} ); |
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