Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Migrate block-editor js to ts
  • Loading branch information
wwahammy committed Mar 29, 2026
commit c748e2f4159dbb305b4d6d4c163d59a92ef39bf8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found copies of the first two functions in the global-styles-engine so I figured I'd just switch this to typescript like that file and migrate the contents over.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
* new object. Treats nullish initial values as empty objects. Clones any
* nested objects. Supports arrays, too.
*
* @param {Object} object Object to set a value in.
* @param {number|string|Array} path Path in the object to modify.
* @param {*} value New value to set.
* @return {Object} Cloned object with the new value set.
* @param object Object to set a value in.
* @param path Path in the object to modify.
* @param value New value to set.
* @return Cloned object with the new value set.
*/
export function setImmutably( object, path, value ) {
export function setImmutably(
object: Object,
path: string | number | ( string | number )[],
value: any
) {
// Normalize path
path = Array.isArray( path ) ? [ ...path ] : [ path ];

Expand All @@ -20,31 +24,41 @@ export function setImmutably( object, path, value ) {
// Traverse object from root to leaf, shallowly cloning at each level
let prev = object;
for ( const key of path ) {
// @ts-expect-error
const lvl = prev[ key ];
// @ts-expect-error
prev = prev[ key ] = Array.isArray( lvl ) ? [ ...lvl ] : { ...lvl };
}

// @ts-expect-error
prev[ leaf ] = value;

return object;
}

/**
* 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 nullish.
* @return {*} Value of the object property at the specified path.
* @param object Input object.
* @param path Path to the object property.
* @param defaultValue Default value if the value at the specified path is nullish.
* @return Value of the object property at the specified path.
*/
export const getValueFromObjectPath = ( object, path, defaultValue ) => {
export const getValueFromObjectPath = (
object: Object,
path: string | string[],
defaultValue?: any
) => {
const arrayPath = Array.isArray( path ) ? path : path.split( '.' );
let value = object;
arrayPath.forEach( ( fieldName ) => {
// @ts-expect-error
value = value?.[ fieldName ];
} );
return value ?? defaultValue;
Expand All @@ -58,9 +72,10 @@ export const getValueFromObjectPath = ( object, path, defaultValue ) => {
*
* @return {Object[]} Array of objects with unique values for the specified property.
*/
export function uniqByProperty( array, property ) {
export function uniqByProperty( array: Object[], property: string ): Object[] {
const seen = new Set();
return array.filter( ( item ) => {
// @ts-expect-error
const value = item[ property ];
return seen.has( value ) ? false : seen.add( value );
} );
Expand Down