Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script scr ="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js" > </script >
<script type ="text/javascript" src ="https://cdn.jsdelivr.net/npm/sval@0.6.3/dist/sval.min.js" > </script >
Setup JS const TEMPLATE_OBJECT = {
defaultLabel :
"${Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hdfc_hierarchy_senior_market_head' ? 'Senior Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hdfc_hierarchy_market_head' ? 'Market Head Unassigned': Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hdfc_hierarchy_sr_region_head' ? 'Senior Region Head Unassigned' : Object.values(currentRecord?.lookupsByField)[0][0].field==='HIERARCHY-hdfc_hierarchy_regional_head' ? 'Region Head Unassigned':'Business Head Unassigned' }" ,
content : "${Date.now() - currentRecord?.start}" ,
}
const CONTEXT = {
currentRecord : {
start : 1736953200000 ,
lookupsByField : {
"HIERARCHY-hdfc_hierarchy_regional_head" : [{
name : "William Schulz" ,
lookupType : "HIERARCHY-hdfc_hierarchy_regional_head" ,
id : "HROLE/AMERICAS_GSA_GVP/OWNERSHIP" ,
field : "HIERARCHY-hdfc_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;
}
};
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;
}
}
function lodashParseObject (
templateObject,
context
){
if (_.isEmpty (templateObject)) {
return templateObject;
}
try {
return JSON .parse (_.template (JSON .stringify (templateObject))(context)) ??{};
} catch (e) {
return templateObject;
}
};
Teardown JS