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.
|
|
|
|
|
|
|
|
import * as plugins from './smarthbs.plugins'
|
|
|
|
|
|
|
|
// the curly regex objects
|
|
|
|
let tripleCurlyRegex = /{{{\s*[\w\.]+\s*}}}/g
|
|
|
|
let doubleCurlyRegex = /{{\s*[\w\.]+\s*}}/g
|
|
|
|
let nameInCurlsRegex = /[\w\.]+/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* finds all variables in a handlebars template
|
|
|
|
* @param hbsStringArg
|
|
|
|
*/
|
|
|
|
export let findVarsInHbsString = async (hbsStringArg: string) => {
|
|
|
|
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) {
|
2017-05-01 22:19:52 +00:00
|
|
|
hbsString = hbsString.replace(tripleCurlyRegex, '[[[replaced]]]')
|
2017-05-03 07:56:40 +00:00
|
|
|
varNameArray = plugins.lodash.concat(varNameArray, tripleCurlyMatches)
|
2017-05-01 22:19:52 +00:00
|
|
|
}
|
2017-05-01 14:57:53 +00:00
|
|
|
let doubleCurlyMatches = hbsString.match(doubleCurlyRegex)
|
2017-05-03 07:56:40 +00:00
|
|
|
if (doubleCurlyMatches) {
|
|
|
|
varNameArray = plugins.lodash.concat(varNameArray, doubleCurlyMatches)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we are clean from curly brackets
|
|
|
|
varNameArray = varNameArray.map((x) => {
|
|
|
|
return x.match(nameInCurlsRegex)[ 0 ]
|
|
|
|
})
|
|
|
|
|
|
|
|
// make sure are uniq
|
2017-05-01 14:57:53 +00:00
|
|
|
varNameArray = plugins.lodash.uniq(varNameArray)
|
|
|
|
return varNameArray
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
let requiredVarStrings = await findVarsInHbsString(hbsStringArg)
|
|
|
|
|
|
|
|
// comparison objects
|
|
|
|
let suppliedVarsObject = varObjectArg
|
|
|
|
let missingVarsObject: string[] = []
|
|
|
|
|
|
|
|
// building the
|
|
|
|
for (let stringVar of requiredVarStrings) {
|
|
|
|
let splittedVars = stringVar.split('.')
|
|
|
|
let requiredPointer = suppliedVarsObject
|
|
|
|
for (let i = 0; i < splittedVars.length; i++) {
|
2017-05-01 22:19:52 +00:00
|
|
|
let splitVar = splittedVars[ i ]
|
|
|
|
let varAvailable = (requiredPointer[ splitVar ] !== undefined)
|
2017-05-01 14:57:53 +00:00
|
|
|
if (varAvailable && splittedVars.length === (i + 1)) {
|
|
|
|
// ok
|
|
|
|
} else if (varAvailable) {
|
2017-05-01 22:19:52 +00:00
|
|
|
requiredPointer = requiredPointer[ splitVar ]
|
2017-05-01 14:57:53 +00:00
|
|
|
} else {
|
|
|
|
missingVarsObject.push(stringVar)
|
|
|
|
i = splittedVars.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingVarsObject
|
|
|
|
}
|