pragma-views2
Version:
74 lines (59 loc) • 1.96 kB
JavaScript
import { ExecutionCompiler } from "./actions/code-compiler.js";
import { getObjOnPath } from "./binding/observers.js";
if (window.compiler == undefined) {
window.compiler = new ExecutionCompiler();
}
const knownFunctions = new Map([["@today", today]]);
export function prefixVariables(prefix, variable) {
if (isKnowFunction(variable) == true) {
return variable;
}
return variable.split("@").join(prefix);
}
function isKnowFunction(variable) {
return knownFunctions.has(variable);
}
export function getModelOnPath(obj, path) {
const stack = path.split(".");
const fieldName = stack[stack.length - 1];
let model = obj;
for (let i = 0; i < stack.length - 1; i++) {
model = model[stack[i]];
if (model == undefined) {
return undefined;
}
}
return {
model: model,
fieldName: fieldName
};
}
export function getValueOnPath(obj, path) {
if (isKnowFunction(path)) {
return knownFunctions.get(path)();
}
const fn = window.compiler.add(path, false);
return fn(obj);
}
export function setValueOnPath(obj, path, value) {
const stack = path.split(".");
const fieldName = stack[stack.length - 1];
let model = getObjOnPath(obj, stack);
model[fieldName] = value;
}
export function deleteValueOnPath(obj, path) {
const result = getModelOnPath(obj, path);
if (result != undefined) {
return delete result.model[result.fieldName];
}
}
export function executeFunctionOnPath(obj, path, parameters) {
const result = getModelOnPath(obj, path);
if (result == undefined) {
return console.warn(`Could not execute ${path} as the path does not fully exist on object`);
}
result.model[result.fieldName].call(result.model, parameters);
}
function today() {
return new Date().toISOString().split('T')[0];
}