vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
48 lines (47 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPropVal = getPropVal;
exports.setPropVal = setPropVal;
exports.getPropKeys = getPropKeys;
const utils_js_1 = require("../utils.js");
// Get a nested property from an object using a dot-separated path such as 'user.id'
function getPropVal(obj, prop) {
const keys = getPropKeys(prop);
let value = obj;
for (const key of keys) {
if ((0, utils_js_1.isObject)(value) && key in value) {
value = value[key];
}
else {
return null; // Property or intermediate property doesn't exist
}
}
return { value };
}
// Set a nested property in an object using a dot-separated path such as 'user.id'
function setPropVal(obj, prop, val) {
const keys = getPropKeys(prop);
let currentObj = obj;
// Creating intermediate objects if necessary
for (let i = 0; i <= keys.length - 2; i++) {
const key = keys[i];
if (!(key in currentObj)) {
// Create intermediate object
currentObj[key] = {};
}
if (!(0, utils_js_1.isObject)(currentObj[key])) {
// Skip value upon data structure conflict
return;
}
currentObj = currentObj[key];
}
// Set the final key to the value
const finalKey = keys[keys.length - 1];
currentObj[finalKey] = val;
}
function getPropKeys(prop) {
// Like `prop.split('.')` but with added support for `\` escaping, see getPageContextClientSerialized.spec.ts
return prop
.split(/(?<!\\)\./) // Split on unescaped dots
.map((key) => key.replace(/\\\./g, '.')); // Replace escaped dots with literal dots
}