eval | const TEMPLATE_OBJECT = {
defaultLabel:
"${Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_senior_market_head' ? 'Senior Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_market_head' ? 'Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_sr_region_head' ? 'Senior Region Head Unassigned' : Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_regional_head' ? 'Region Head Unassigned':'Business Head Unassigned' }",
content: "${Date.now() - currentRecord?.start}",
};
const CONTEXT = {
currentRecord: {
start: 1736953200000,
lookupsByField: {
"HIERARCHY-hierarchy_regional_head": [{
name: "William Schulz",
lookupType: "HIERARCHY-hierarchy_regional_head",
id: "HROLE/AMERICAS_GSA_GVP/OWNERSHIP",
field: "HIERARCHY-hierarchy_regional_head",
}]
}
}
};
const REGEX_FOR_SINGLE_OR_MULTIPLE_EXP_IN_STRING = /\${([^}]*)}/g;
const REGEX_FOR_SINGLE_EXP_IN_STRING = /^\${.*}$/;
function evalTemplateObject(templateObject, context){
const window = undefined;
const fetch = undefined;
const document = undefined;
const setTimeout = undefined;
const setInterval = undefined;
const { currentRecord, currentUser, iteratorObject, router, parentRecord, resources, __customContext__ } = context;
if (_.isEmpty(templateObject)) {
return templateObject;
}
const recursivelyIterate = iterator => {
if (!_.isObject(iterator)) {
if (typeof iterator === 'string') {
if (REGEX_FOR_SINGLE_EXP_IN_STRING.test(iterator)) {
const parsedValue = eval(iterator.substring(2, iterator.length - 1));
return parsedValue;
}
return iterator.replace(REGEX_FOR_SINGLE_OR_MULTIPLE_EXP_IN_STRING, (match, group) => {
const parsedValue = eval(`${group}`);
return parsedValue;
});
}
return iterator;
}
if (Array.isArray(iterator)) {
return iterator.map(value => recursivelyIterate(value));
}
return _.reduce(
iterator,
(acc, value, key) => {
acc[key] = recursivelyIterate(value);
return acc;
},
{}
);
};
try {
return recursivelyIterate(templateObject);
} catch (error) {
return templateObject;
}
};
evalTemplateObject(TEMPLATE_OBJECT, CONTEXT)
| ready |
sval | const TEMPLATE_OBJECT = {
defaultLabel:
"${Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_senior_market_head' ? 'Senior Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_market_head' ? 'Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_sr_region_head' ? 'Senior Region Head Unassigned' : Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_regional_head' ? 'Region Head Unassigned':'Business Head Unassigned' }",
content: "${Date.now() - currentRecord?.start}",
}
const CONTEXT = {
currentRecord: {
start: 1736953200000,
lookupsByField: {
"HIERARCHY-hierarchy_regional_head": [{
name: "William Schulz",
lookupType: "HIERARCHY-hierarchy_regional_head",
id: "HROLE/AMERICAS_GSA_GVP/OWNERSHIP",
field: "HIERARCHY-hierarchy_regional_head",
}]
}
}
}
const REGEX_FOR_SINGLE_OR_MULTIPLE_EXP_IN_STRING = /\${([^}]*)}/g;
const REGEX_FOR_SINGLE_EXP_IN_STRING = /^\${.*}$/;
const svalInterpreter = new Sval({
ecmaVer: 11,
sourceType: "script",
sandBox: true,
});
function svalParseObject(templateObject, context) {
svalInterpreter.import({
currentRecord: context.currentRecord,
currentUser: context.currentUser,
iteratorObject: context.iteratorObject,
router: context.router,
parentRecord: context.parentRecord,
resources: context.resources,
__customContext__: context.__customContext__,
});
if (_.isEmpty(templateObject)) {
return templateObject;
}
const recursivelyIterate = (iterator) => {
if (!_.isObject(iterator)) {
if (typeof iterator === "string") {
if (REGEX_FOR_SINGLE_EXP_IN_STRING.test(iterator)) {
svalInterpreter.run(
`exports.__output__ = ${iterator.substring(2, iterator.length - 1)}`
);
return svalInterpreter.exports.__output__;
}
return iterator.replace(
REGEX_FOR_SINGLE_OR_MULTIPLE_EXP_IN_STRING,
(match, group) => {
svalInterpreter.run(`exports.__output__ = ${group}`);
return svalInterpreter.exports.__output__;
}
);
}
return iterator;
}
if (Array.isArray(iterator)) {
return iterator.map((value) => recursivelyIterate(value));
}
return _.reduce(
iterator,
(acc, value, key) => {
acc[key] = recursivelyIterate(value);
return acc;
},
{}
);
};
try {
return recursivelyIterate(templateObject);
} catch (error) {
return templateObject;
}
}
svalParseObject(TEMPLATE_OBJECT, CONTEXT)
| ready |
lodash/_template | const TEMPLATE_OBJECT = {
defaultLabel:
"${Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_senior_market_head' ? 'Senior Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_market_head' ? 'Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_sr_region_head' ? 'Senior Region Head Unassigned' : Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hierarchy_regional_head' ? 'Region Head Unassigned':'Business Head Unassigned' }",
content: "${Date.now() - currentRecord?.start}",
}
const CONTEXT = {
currentRecord: {
start: 1736953200000,
lookupsByField: {
"HIERARCHY-hierarchy_regional_head": [{
name: "William Schulz",
lookupType: "HIERARCHY-hierarchy_regional_head",
id: "HROLE/AMERICAS_GSA_GVP/OWNERSHIP",
field: "HIERARCHY-hierarchy_regional_head",
}]
}
}
}
function lodashParseObject(
templateObject,
context
){
if (_.isEmpty(templateObject)) {
return templateObject;
}
try {
return JSON.parse(_.template(JSON.stringify(templateObject))(context)) ??{};
} catch (e) {
return templateObject;
}
};
lodashParseObject(TEMPLATE_OBJECT, CONTEXT)
| ready |