-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Indicate nested paths on __experimentalSaveSpecifiedEntityEdits #54161
Changes from all commits
cfad1f2
f05d3c5
00cf84d
865560a
8ce14a3
656ccf0
b3ade9a
de832bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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( '.' ); | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let value = object; | ||
normalizedPath.forEach( ( fieldName ) => { | ||
value = value?.[ fieldName ]; | ||
} ); | ||
return value !== undefined ? value : defaultValue; | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,10 @@ | |
* Arrays are created for missing index properties while objects are created | ||
* for all other missing properties. | ||
* | ||
* Path is specified as either: | ||
* - a string of properties, separated by dots, for example: "x.y". | ||
* - an array of properties, for example `[ 'x', 'y' ]`. | ||
* | ||
* This function intentionally mutates the input object. | ||
* | ||
* Inspired by _.set(). | ||
|
@@ -12,24 +16,26 @@ | |
* | ||
* @todo Needs to be deduplicated with its copy in `@wordpress/edit-site`. | ||
* | ||
* @param {Object} object Object to modify | ||
* @param {Array} path Path of the property to set. | ||
* @param {*} value Value to set. | ||
* @param {Object} object Object to modify | ||
* @param {Array|string} path Path of the property to set. | ||
* @param {*} value Value to set. | ||
*/ | ||
export default function setNestedValue( object, path, value ) { | ||
if ( ! object || typeof object !== 'object' ) { | ||
return object; | ||
} | ||
|
||
path.reduce( ( acc, key, idx ) => { | ||
const normalizedPath = Array.isArray( path ) ? path : path.split( '.' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that when introducing Also, keep in mind I'm not saying that we shouldn't use the dot notation ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we work on this as a follow-up when we make this a new package? |
||
|
||
normalizedPath.reduce( ( acc, key, idx ) => { | ||
if ( acc[ key ] === undefined ) { | ||
if ( Number.isInteger( path[ idx + 1 ] ) ) { | ||
if ( Number.isInteger( normalizedPath[ idx + 1 ] ) ) { | ||
acc[ key ] = []; | ||
} else { | ||
acc[ key ] = {}; | ||
} | ||
} | ||
if ( idx === path.length - 1 ) { | ||
if ( idx === normalizedPath.length - 1 ) { | ||
acc[ key ] = value; | ||
} | ||
return acc[ key ]; | ||
|
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', () => { | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 } } }; | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); | ||
} ); | ||
} ); |
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.
What a bummer that we're introducing yet another version of that utility function. While migrating away from Lodash we had to introduce (almost duplicate) a few of those in various packages, because this was the very last function to remove and we were willing to make some temporary compromises. But ideally, we should unify them all and avoid to introduce new ones if possible.
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.
@tyxla I agree. I think we can merge this and follow up with the creation of the package after.Right now we need this util to launch the Font Library. This change is in use here: #53884