Code coverage report for template/lib/index.js

Statements: 100% (44 / 44)      Branches: 100% (16 / 16)      Functions: 100% (8 / 8)      Lines: 100% (43 / 43)      Ignored: none     

All files » template/lib/ » index.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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123            1 1 1 1             1                     1 216   216 216   216                     1 839 635 640     204 223                     1 637   637 1     636 1     635 635 1274 1273 1273   1273 640 640 640   1 1 1     633 633     635                     1 2 1   2                     1 1    
'use strict';
 
/**
 * Module dependencies
 */
 
var path = require('path');
var slice = require('array-slice');
var debug = require('./debug');
var _ = require('lodash');
 
/**
 * Default router methods used in all Template instances
 * @api private
 */
 
exports.methods = ['onLoad', 'before', 'after'];
 
/**
 * Get the name of the layout to use for the given `template`.
 *
 * @param  {Object} `template` Template object to search for `layout`.
 * @param  {Object} `locals` Locals object search if not found on `template.`
 * @return {String} The name of the layout to use.
 * @api private
 */
 
exports.getLayout = function (template, locals) {
  debug.utils('[utils] getting layout: %j', template);
 
  var o = _.merge({}, template, locals);
  o = _.merge({}, o, o.options);
 
  return o.layout;
};
 
/**
 * Bind a `thisArg` to all the functions on the target
 *
 * @param  {Object|Array} `target` Object or Array with functions as values that will be bound.
 * @param  {Object} `thisArg` Object to bind to the functions
 * @return {Object|Array} Object or Array with bound functions.
 */
 
exports.bindAll = function (target, thisArg) {
  if (Array.isArray(target)) {
    return target.map(function (fn) {
      return _.bind(fn, thisArg);
    });
  }
  return _.transform(target, function (acc, fn, key) {
    acc[key] = _.bind(fn, thisArg);
  });
};
 
/**
 * Run a loader stack with a final callback.
 *
 * @param  {Array} `stack` Array of functions to run.
 * @param  {Function} `cb` Final callback when the stack is done.
 */
 
exports.runLoaderStack = function (stack, cb) {
  debug.utils('[utils] running loader stack: %j', stack);
 
  if (typeof cb !== 'function') {
    throw new Error('Expected cb to be a function');
  }
 
  if (!Array.isArray(stack)) {
    return cb(new Error('Expected stack to be an Array'));
  }
 
  var i = 0;
  var next = function next (err) {
    if (err) return cb(err);
    var args = arguments.length === 0 ? [] : slice(arguments, 1);
    var fn = stack[i++];
 
    if (fn) {
      args.push(next);
      try {
        return fn.apply(fn, args);
      } catch (err) {
        args.pop();
        args.unshift(err);
        return cb.apply(cb, args);
      }
    } else {
      args.unshift(err);
      return cb.apply(cb, args);
    }
  };
  return next();
};
 
/**
 * Ensure file extensions are formatted properly for lookups.
 *
 * @param {String} `ext` File extension
 * @return {String}
 * @api private
 */
 
exports.formatExt = function(ext) {
  if (ext && ext[0] !== '.') {
    ext = '.' + ext;
  }
  return ext;
};
 
/**
 * Get a file extension, without the dot.
 *
 * @param {String} `fp` File path
 * @return {String}
 * @api private
 */
 
exports.getExt = function(fp) {
  return path.extname(fp).slice(1);
};