All files / lib/util handlebars.js

100% Statements 11/11
100% Branches 7/7
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2142x   42x   42x     40x 19x   40x 40x 39x 43x 43x         42x  
const Evaluator = require('./evaluator');
 
const evaluator = new Evaluator();
 
const dictionary = {};
 
function compile(str) {
  if (dictionary[str] === undefined) {
    dictionary[str] = str.match(/{{\s*([^}]+)\s*}}/g) || [];
  }
  const matches = dictionary[str];
  return (context = {}) => {
    return matches.reduce((p, c) => {
      const solution = evaluator.evaluate(c.substr(2, c.length - 4), context);
      return solution ? p.replace(c, solution) : p;
    }, str);
  };
}
 
module.exports = { compile };