@ -3,6 +3,7 @@ const { registerAll } = require("./helpers/index")
const processors = require ( "./processors" )
const { atob , btoa } = require ( "./utilities" )
const manifest = require ( "../manifest.json" )
const { FIND_HBS_REGEX } = require ( "./utilities" )
const hbsInstance = handlebars . create ( )
registerAll ( hbsInstance )
@ -26,7 +27,7 @@ function testObject(object) {
* @ param { object | array } object The input structure which is to be recursed , it is important to note that
* if the structure contains any cycles then this will fail .
* @ param { object } context The context that handlebars should fill data from .
* @ param { object } opts optional - specify some options for processing .
* @ param { object | null } opts optional - specify some options for processing .
* @ returns { Promise < object | array > } The structure input , as fully updated as possible .
* /
module . exports . processObject = async ( object , context , opts ) => {
@ -57,7 +58,7 @@ module.exports.processObject = async (object, context, opts) => {
* then nothing will occur .
* @ param { string } string The template string which is the filled from the context object .
* @ param { object } context An object of information which will be used to enrich the string .
* @ param { object } opts optional - specify some options for processing .
* @ param { object | null } opts optional - specify some options for processing .
* @ returns { Promise < string > } The enriched string , all templates should have been replaced if they can be .
* /
module . exports . processString = async ( string , context , opts ) => {
@ -71,7 +72,7 @@ module.exports.processString = async (string, context, opts) => {
* @ param { object | array } object The input structure which is to be recursed , it is important to note that
* if the structure contains any cycles then this will fail .
* @ param { object } context The context that handlebars should fill data from .
* @ param { object } opts optional - specify some options for processing .
* @ param { object | null } opts optional - specify some options for processing .
* @ returns { object | array } The structure input , as fully updated as possible .
* /
module . exports . processObjectSync = ( object , context , opts ) => {
@ -92,7 +93,7 @@ module.exports.processObjectSync = (object, context, opts) => {
* then nothing will occur . This is a pure sync call and therefore does not have the full functionality of the async call .
* @ param { string } string The template string which is the filled from the context object .
* @ param { object } context An object of information which will be used to enrich the string .
* @ param { object } opts optional - specify some options for processing .
* @ param { object | null } opts optional - specify some options for processing .
* @ returns { string } The enriched string , all templates should have been replaced if they can be .
* /
module . exports . processStringSync = ( string , context , opts ) => {
@ -221,3 +222,30 @@ module.exports.decodeJSBinding = handlebars => {
}
return atob ( match [ 1 ] )
}
/ * *
* This function looks in the supplied template for handlebars instances , if they contain
* JS the JS will be decoded and then the supplied string will be looked for . For example
* if the template "Hello, your name is {{ related }}" this function would return that true
* for the string "related" but not for "name" as it is not within the handlebars statement .
* @ param { string } template A template string to search for handlebars instances .
* @ param { string } string The word or sentence to search for .
* @ returns { boolean } The this return true if the string is found , false if not .
* /
module . exports . doesContainString = ( template , string ) => {
let regexp = new RegExp ( FIND_HBS_REGEX )
let matches = template . match ( regexp )
if ( matches == null ) {
return false
}
for ( let match of matches ) {
let hbs = match
if ( exports . isJSBinding ( match ) ) {
hbs = exports . decodeJSBinding ( match )
}
if ( hbs . includes ( string ) ) {
return true
}
}
return false
}