All files functions.js

100% Statements 76/76
100% Branches 15/15
100% Functions 5/5
100% Lines 76/76

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 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 67 68 69 70 71 72 73 74 75 76 771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 2x 2x 23x 23x 23x 23x 23x 23x 23x 23x 23x 3x 3x 3x 3x 23x 23x 23x 23x 23x 23x 23x 23x 23x 4x 4x 72x 5x 5x 72x 4x 4x 23x 23x 23x 23x 23x 23x 23x 3x 3x 3x 3x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x  
"use strict"
import dateformat from 'dateformat';
 
/**
 * Pre-defined functions which can be used as template functions.
 * When these functions are applied to the current page, it uses page + templates + params as its context.
 *
 * @param {String} page: current page which these functions will be applied to
 * @param {Object} templates: compiled jazz templates
 * @param {Object} params: template parameters (including user-defined parameters and custom functions)
 */
function funcs(page, templates, params) {
 
  /**
   * Display the current date in specified format.
   * Default to ISO date when format is not specified.
   *
   * @param {String} format: date format (felixge/node-dateformat)
   * @param {Function} cb: jazz cb(data) callback
   */
  function date(format, cb) {
    cb(dateformat(new Date(), format || 'isoDateTime'));
  }
 
  /**
   * Include a partial template in the current page.
   * An error message will be included when partial does not exist.
   *
   * @param {String} partial: partial template file to include
   * @param {Function} cb: jazz cb(data) callback
   */
  function include(partial, cb) {
    cb((params.partials && params.partials[partial]) ?
      params.partials[partial] :
      '[error] partial ' + partial + ' does not exist');
  }
 
  /**
   * Prefix path with one ../ for each parent directory of the page.
   * This is used to construct static links from various directory level in the website.
   *
   * @param {String} path: a URL path in the site navigation
   * @param {Function} cb: jazz cb(data) callback
   */
  function relative(path, cb) {
    let value = path;
    for (let i = 0, ln = page.length; i < ln; i += 1) {
      if (page.charAt(i) === '/') {
        value = '../' + value;
      }
    }
    cb(value);
  }
 
  /**
   * Display current page's sitemap title.
   *
   * @param {Function} cb: jazz cb(data) callback
   */
  function title(cb) {
    cb((params.sitemap && params.sitemap[page]) ?
      params.sitemap[page].title :
      '[error] page ' + page + ' does not have any sitemap title');
  }
 
  return {
    date: date,
    include: include,
    relative: relative,
    title: title
  };    
}
 
export {
  funcs as default
};