/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/global-styles-engine/src/utils/object.ts
64 строки
2 KB
Manzoor Wani
ESLint: Ban `@ts-ignore` in favour of `@ts-expect-error` (#81148)
05 авг 2026, 07:36
Не верифицирован
05 авг 2026, 07:36
c1d8367
Код
Авторство
О чём код?
/** * Immutably sets a value inside an object. Like `lodash#set`, but returning a * new object. Treats nullish initial values as empty objects. Clones any * nested objects. Supports arrays, too. * * @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: Object, path: string | number | ( string | number )[], value: any ) { // Normalize path path = Array.isArray( path ) ? [ ...path ] : [ path ]; // Shallowly clone the base of the object object = Array.isArray( object ) ? [ ...object ] : { ...object }; const leaf = path.pop(); // Traverse object from root to leaf, shallowly cloning at each level let prev = object; for ( const key of path ) { // @ts-expect-error `prev` is typed as `Object`, which has no index signature. const lvl = prev[ key ]; // @ts-expect-error `prev` is typed as `Object`, which has no index signature. prev = prev[ key ] = Array.isArray( lvl ) ? [ ...lvl ] : { ...lvl }; } // @ts-expect-error `leaf` is possibly `undefined`, which cannot be an index type. 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 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: Object, path: string | string[], defaultValue?: any ) => { const arrayPath = Array.isArray( path ) ? path : path.split( '.' ); let value = object; arrayPath.forEach( ( fieldName ) => { // @ts-expect-error `value` is typed as `Object`, which has no index signature. value = value?.[ fieldName ]; } ); return value ?? defaultValue; };