2017-05-01 14:57:53 +00:00
|
|
|
// This file contains code that makes it easy to search handlebar templates for variables.
|
|
|
|
// Why? To get a clue if you are missing some.
|
|
|
|
|
2022-07-24 13:30:19 +00:00
|
|
|
import * as plugins from './smarthbs.plugins.js';
|
2017-05-01 14:57:53 +00:00
|
|
|
|
|
|
|
// the curly regex objects
|
2018-08-27 21:04:15 +00:00
|
|
|
let tripleCurlyRegex = /{{{\s*[\w\.]+\s*}}}/g;
|
|
|
|
let doubleCurlyRegex = /{{\s*[\w\.]+\s*}}/g;
|
|
|
|
let nameInCurlsRegex = /[\w\.]+/;
|
2017-05-01 14:57:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* finds all variables in a handlebars template
|
|
|
|
* @param hbsStringArg
|
|
|
|
*/
|
|
|
|
export let findVarsInHbsString = async (hbsStringArg: string) => {
|
2018-08-27 21:04:15 +00:00
|
|
|
let hbsString = hbsStringArg; // make sure we have a new string object that we may destroy
|
|
|
|
let varNameArray: string[] = [];
|
|
|
|
let tripleCurlyMatches = hbsString.match(tripleCurlyRegex);
|
2017-05-01 22:25:13 +00:00
|
|
|
if (tripleCurlyMatches) {
|
2018-08-27 21:04:15 +00:00
|
|
|
hbsString = hbsString.replace(tripleCurlyRegex, '[[[replaced]]]');
|
2019-09-10 15:58:14 +00:00
|
|
|
varNameArray = varNameArray.concat(tripleCurlyMatches);
|
2017-05-01 22:19:52 +00:00
|
|
|
}
|
2018-08-27 21:04:15 +00:00
|
|
|
let doubleCurlyMatches = hbsString.match(doubleCurlyRegex);
|
2017-05-03 07:56:40 +00:00
|
|
|
if (doubleCurlyMatches) {
|
2019-09-10 15:58:14 +00:00
|
|
|
varNameArray = varNameArray.concat(doubleCurlyMatches);
|
2017-05-03 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we are clean from curly brackets
|
2022-07-24 13:30:19 +00:00
|
|
|
varNameArray = varNameArray.map((x) => {
|
2018-08-27 21:04:15 +00:00
|
|
|
return x.match(nameInCurlsRegex)[0];
|
|
|
|
});
|
2017-05-03 07:56:40 +00:00
|
|
|
|
|
|
|
// make sure are uniq
|
2023-06-24 22:29:02 +00:00
|
|
|
function uniqueArray(input: string[]): string[] {
|
|
|
|
return Array.from(new Set(input));
|
|
|
|
}
|
|
|
|
varNameArray = uniqueArray(varNameArray);
|
2018-08-27 21:04:15 +00:00
|
|
|
return varNameArray;
|
|
|
|
};
|
2017-05-01 14:57:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if supplied variables satisfy an handlebars template
|
|
|
|
* @param hbsStringArg
|
2017-05-01 15:01:58 +00:00
|
|
|
* @param varObjectArg
|
|
|
|
* @return string array with missing variable names
|
2017-05-01 14:57:53 +00:00
|
|
|
*/
|
|
|
|
export let checkVarsSatisfaction = async (hbsStringArg: string, varObjectArg: any) => {
|
|
|
|
// required vars as combined deep string with . notation
|
2018-08-27 21:04:15 +00:00
|
|
|
let requiredVarStrings = await findVarsInHbsString(hbsStringArg);
|
2017-05-01 14:57:53 +00:00
|
|
|
|
|
|
|
// comparison objects
|
2018-08-27 21:04:15 +00:00
|
|
|
let suppliedVarsObject = varObjectArg;
|
|
|
|
let missingVarsObject: string[] = [];
|
2017-05-01 14:57:53 +00:00
|
|
|
|
2018-08-27 21:04:15 +00:00
|
|
|
// building the
|
2017-05-01 14:57:53 +00:00
|
|
|
for (let stringVar of requiredVarStrings) {
|
2018-08-27 21:04:15 +00:00
|
|
|
let splittedVars = stringVar.split('.');
|
|
|
|
let requiredPointer = suppliedVarsObject;
|
2017-05-01 14:57:53 +00:00
|
|
|
for (let i = 0; i < splittedVars.length; i++) {
|
2018-08-27 21:04:15 +00:00
|
|
|
let splitVar = splittedVars[i];
|
|
|
|
let varAvailable = requiredPointer[splitVar] !== undefined;
|
|
|
|
if (varAvailable && splittedVars.length === i + 1) {
|
2017-05-01 14:57:53 +00:00
|
|
|
// ok
|
|
|
|
} else if (varAvailable) {
|
2018-08-27 21:04:15 +00:00
|
|
|
requiredPointer = requiredPointer[splitVar];
|
2017-05-01 14:57:53 +00:00
|
|
|
} else {
|
2018-08-27 21:04:15 +00:00
|
|
|
missingVarsObject.push(stringVar);
|
|
|
|
i = splittedVars.length;
|
2017-05-01 14:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-27 21:04:15 +00:00
|
|
|
return missingVarsObject;
|
|
|
|
};
|