Code coverage report for lib/text.js

Statements: 100% (18 / 18)      Branches: 100% (4 / 4)      Functions: 100% (7 / 7)      Lines: 100% (18 / 18)      Ignored: none     

All files » lib/ » text.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671                       1   9   9     9 18   9 9   9           9                     1   7                       1   6   6     1 1 1  
var async = require('async'),
  jazz = require('jazz');
 
/**
 * Apply parameters/functions to specified pre-compiled template.
 * Used in conjunction with text#applyPrecompiled when text needs to be compiled once
 * and then applied multiple times.
 *
 * @param {String} template: pre-compiled template
 * @param {Object} params: an object containing parameter key value mapping
 * @return {String} template merged with the parameters
 */
function applyPrecompiled(template, params) {
 
  params = params || {};
 
  var applied;
 
  // template evaluation shouldn't need a callback since it doesn't involve any IO, hence text#apply blocks
  async.whilst(
    function () { return applied === undefined; },
    function (cb) {
      template.process(params, function (result) {
        applied = result;
      });
      setTimeout(cb, 1);
    },
    function (err) {
    }
  );
 
  return applied;
}
 
/**
 * Compile text. An abstraction layer on top of jazz.compile .
 * Used in conjunction with text#applyPrecompiled when text needs to be compiled once
 * and then applied multiple times.
 *
 * @param {String} text: a text containing {param} parameter placeholders, can also contain simple {function('arg1', ... , 'argN')} function calls 
 * @return {Object} compiled template
 */
function compile(text) {
 
  return jazz.compile(text);
}
 
/**
 * Apply parameters/functions to specified text.
 * E.g. The text 'Hello {name}' with param { name: 'FooBar' } will become 'Hello FooBar'.
 * The text 'Today is {now('dd-mm-yyyy')}' will become 'Today is 09-05-2012' (whatever today's date is)
 *
 * @param text {String}: a text containing {param} parameter placeholders, can also contain simple {function('arg1', ... , 'argN')} function calls
 * @param params {Object}: an object containing parameter key value mapping
 * @return {String} template merged with the parameters
 */
function apply(text, params) {
 
  text = text || '';
 
  return this.applyPrecompiled(this.compile(text), params);
}
 
exports.apply = apply;
exports.applyPrecompiled = applyPrecompiled;
exports.compile = compile;