
META-INF.resources.page_editor.app.utils.updateIn.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.layout.content.page.editor.web
Show all versions of com.liferay.layout.content.page.editor.web
Liferay Layout Content Page Editor Web
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
export function updateIn | any[]>(
objectOrArray: T,
keyPathOrKey: Array,
updater: (value: unknown) => unknown,
defaultValue: unknown = undefined
): T {
const keyPath =
typeof keyPathOrKey === 'string' ? [keyPathOrKey] : keyPathOrKey;
const [nextKey] = keyPath as Array;
let nextObjectOrArray = objectOrArray;
if (keyPath.length > 1) {
nextObjectOrArray = Array.isArray(nextObjectOrArray)
? ([...nextObjectOrArray] as T)
: ({...nextObjectOrArray} as T);
nextObjectOrArray[nextKey] = updateIn(
(nextObjectOrArray[nextKey] || {}) as T[keyof T],
keyPath.slice(1),
updater,
defaultValue
);
}
else {
const nextValue =
typeof nextObjectOrArray[nextKey] === 'undefined'
? defaultValue
: nextObjectOrArray[nextKey];
const updatedNextValue = updater(nextValue) as T[keyof T];
if (updatedNextValue !== nextObjectOrArray[nextKey]) {
nextObjectOrArray = Array.isArray(nextObjectOrArray)
? ([...nextObjectOrArray] as T)
: ({...nextObjectOrArray} as T);
nextObjectOrArray[nextKey] = updatedNextValue;
}
}
return nextObjectOrArray;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy