@appscode/ui-builder
Version:
## Motivation
128 lines (115 loc) • 3.6 kB
JavaScript
import Vue from "vue";
function resolve(ob, ref) {
const targetValue = getValue(ob, ref);
if (targetValue && targetValue.$ref) {
return Object.assign({}, targetValue, resolve(ob, targetValue.$ref));
} else {
return targetValue;
}
}
function reactiveSet(ob, ref, value, force) {
function recursiveSetter(ob, path, value) {
const pathArray = path.split("/");
const prop = pathArray[0];
const newPath = pathArray.slice(1).join("/");
if (newPath === "") {
// base case
// set the value and make it reactive to vue
Vue.set(ob, prop, value);
} else {
// make recursive call to set the value
if (ob[prop] === undefined) {
// property does not exist inside the ob
// create the property
if (force) {
Vue.set(ob, prop, {});
} else {
return;
}
}
recursiveSetter(ob[prop], newPath, value);
}
}
// trim the given path
const trimmedPath = ref.trim();
// remove first and last slash(/) if given
const unSlashedPath = trimmedPath.replace(/^#?\/|\/$/g, "");
recursiveSetter(ob, unSlashedPath, value);
}
function getValue(ob, ref) {
function recursiveGetter(ob, path) {
if (ob && typeof ob === "object") {
// ob has to be object if we want to proceed
if (path === "") {
// path is empty return ob
return ob;
} else {
const pathArray = path.split("/");
const prop = pathArray[0];
const newPath = pathArray.slice(1).join("/");
if (newPath === "") {
// base case
// return value
return ob[prop];
} else {
// make recursive call to get the value
if (ob[prop] === undefined) {
// property does not exist inside the ob
// return undefined
return undefined;
}
return recursiveGetter(ob[prop], newPath);
}
}
} else {
return undefined;
}
}
// trim the given path
const trimmedPath = ref.trim();
// remove first and last slash(/) if given
const unSlashedPath = trimmedPath.replace(/^#?\/|\/$/g, "");
return recursiveGetter(ob, unSlashedPath);
}
function isPathExist(ob, ref) {
function recursivePathCheck(ob, path) {
const pathArray = path.split("/");
const prop = pathArray[0];
const newPath = pathArray.slice(1).join("/");
// base case
if (newPath === "") {
return !!ob[prop];
} else if (!ob[prop]) return false;
return recursivePathCheck(ob[prop], newPath);
}
// trim the given path
const trimmedPath = ref.trim();
// remove first and last slash(/) if given
const unSlashedPath = trimmedPath.replace(/^#?\/|\/$/g, "");
recursivePathCheck(ob, unSlashedPath);
}
function reactiveDelete(ob, ref) {
function recursiveDelete(ob, path) {
const pathArray = path.split("/");
const prop = pathArray[0];
const newPath = pathArray.slice(1).join("/");
if (newPath === "") {
// base case
// delete the value and make it reactive to vue
Vue.delete(ob, prop);
} else {
// make recursive call to delete the value
if (ob[prop] == undefined) {
// property does not exist inside the ob
return;
}
recursiveDelete(ob[prop], newPath);
}
}
// trim the given path
const trimmedPath = ref.trim();
// remove first and last slash(/) if given
const unSlashedPath = trimmedPath.replace(/^#?\/|\/$/g, "");
recursiveDelete(ob, unSlashedPath);
}
export { resolve, reactiveSet, getValue, reactiveDelete, isPathExist };