syncbasestore
Version:
Lightweight reactive store with built-in filtering and async state handling
27 lines (26 loc) • 790 B
JavaScript
export function getValueAtPath(obj, path) {
return path.split('.').reduce((acc, key) => acc?.[key], obj);
}
export function setValueAtPath(obj, path, value) {
const keys = path.split('.');
const lastKey = keys.pop();
const newObj = { ...obj };
let nested = newObj;
for (const key of keys) {
nested[key] = { ...nested[key] };
nested = nested[key];
}
nested[lastKey] = value;
return newObj;
}
export function setDeepValue(obj, path, value) {
const keys = path.split('.');
const newObj = { ...obj };
let current = newObj;
for (let i = 0; i < keys.length - 1; i++) {
current[keys[i]] = { ...current[keys[i]] };
current = current[keys[i]];
}
current[keys[keys.length - 1]] = value;
return newObj;
}