@sanity/import
Version:
Import documents to a Sanity dataset
48 lines (47 loc) • 1.41 kB
JavaScript
function isIndexable(value) {
return typeof value === 'object' && value !== null;
}
/**
* Get a nested value from an object by path array. Similar to lodash `get`.
*/ export function deepGet(obj, path) {
let current = obj;
for (const key of path){
if (!isIndexable(current)) {
return undefined;
}
current = current[key];
}
return current;
}
/**
* Set a nested value on an object by path array. Mutates in place.
* Creates intermediate objects/arrays as needed. Similar to lodash `set`.
*/ export function deepSet(obj, path, value) {
if (path.length === 0) return;
let current = obj;
for(let i = 0; i < path.length - 1; i++){
const key = path[i];
const next = path[i + 1];
if (!isIndexable(current[key])) {
current[key] = typeof next === 'number' ? [] : {};
}
current = current[key];
}
current[path.at(-1)] = value;
}
/**
* Delete a nested value from an object by path array. Mutates in place.
* Similar to lodash `unset`.
*/ export function deepUnset(obj, path) {
if (path.length === 0) return;
let current = obj;
for(let i = 0; i < path.length - 1; i++){
const key = path[i];
if (!isIndexable(current[key])) {
return;
}
current = current[key];
}
delete current[path.at(-1)];
}
//# sourceMappingURL=deepGet.js.map