package.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 component-base Show documentation
Show all versions of component-base Show documentation
Vaadin component base mixins
/**
* @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;
}