add extended variable checking
This commit is contained in:
@ -6,3 +6,4 @@ export * from './smarthbs.compile'
|
||||
import './smarthbs.helpers'
|
||||
export * from './smarthbs.partials'
|
||||
export * from './smarthbs.template'
|
||||
export * from './smarthbs.variables'
|
||||
|
@ -5,3 +5,4 @@ plugins.handlebars.registerHelper('__compile', (evaluationString, evaluationCont
|
||||
return template(evaluationContext)
|
||||
})
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@ import * as plugins from './smarthbs.plugins'
|
||||
/**
|
||||
* registers a directory of partials to make them available within handlebars compilation
|
||||
*/
|
||||
export let registerPartialDir = (dirPathArg: string) => {
|
||||
export let registerPartialDir = (dirPathArg: string): Promise<any> => {
|
||||
let done = plugins.smartq.defer()
|
||||
plugins.smartfile.fs.listFileTree(dirPathArg, '**/*.hbs').then(hbsFileArrayArg => {
|
||||
for (let hbsFilePath of hbsFileArrayArg) {
|
||||
|
@ -1,12 +1,14 @@
|
||||
import 'typings-global'
|
||||
import * as handlebars from 'handlebars'
|
||||
import * as lodash from 'lodash'
|
||||
import * as path from 'path'
|
||||
import * as smartfile from 'smartfile'
|
||||
import * as smartq from 'smartq'
|
||||
import * as path from 'path'
|
||||
|
||||
export {
|
||||
handlebars,
|
||||
lodash,
|
||||
path,
|
||||
smartfile,
|
||||
smartq,
|
||||
path
|
||||
smartq
|
||||
}
|
||||
|
61
ts/smarthbs.variables.ts
Normal file
61
ts/smarthbs.variables.ts
Normal file
@ -0,0 +1,61 @@
|
||||
// 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)
|
||||
hbsString = hbsString.replace(tripleCurlyRegex, '[[[replaced]]]')
|
||||
let doubleCurlyMatches = hbsString.match(doubleCurlyRegex)
|
||||
hbsString = hbsString.replace(doubleCurlyRegex, '[[[replaced]]]')
|
||||
varNameArray = plugins.lodash.concat(varNameArray, tripleCurlyMatches, doubleCurlyMatches)
|
||||
.map((x) => {
|
||||
return x.match(nameInCurlsRegex)[ 0 ]
|
||||
})
|
||||
varNameArray = plugins.lodash.uniq(varNameArray)
|
||||
return varNameArray
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if supplied variables satisfy an handlebars template
|
||||
* @param hbsStringArg
|
||||
* @param varObjectArg
|
||||
*/
|
||||
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++) {
|
||||
let splitVar = splittedVars[i]
|
||||
let varAvailable = (requiredPointer[splitVar] !== undefined)
|
||||
if (varAvailable && splittedVars.length === (i + 1)) {
|
||||
// ok
|
||||
} else if (varAvailable) {
|
||||
requiredPointer = requiredPointer[splitVar]
|
||||
} else {
|
||||
missingVarsObject.push(stringVar)
|
||||
i = splittedVars.length
|
||||
}
|
||||
}
|
||||
}
|
||||
return missingVarsObject
|
||||
}
|
Reference in New Issue
Block a user