component-basepackage.src.path-utils.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-webcomponents Show documentation
Show all versions of vaadin-webcomponents Show documentation
Mvnpm composite: Vaadin webcomponents
The newest version!
/**
* @license
* Copyright (c) 2023 - 2024 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
/**
* Convenience method for reading a value from a path.
*
* @param {string} path
* @param {object} object
*/
export function get(path, object) {
return path.split('.').reduce((obj, property) => (obj ? obj[property] : undefined), object);
}
/**
* Convenience method for setting a value to a path.
*
* @param {string} path
* @param {unknown} value
* @param {object} object
*/
export function set(path, value, object) {
const pathParts = path.split('.');
const lastPart = pathParts.pop();
const target = pathParts.reduce((target, part) => target[part], object);
target[lastPart] = value;
}