Code coverage report for ./lib/format.js

Statements: 83.33% (15 / 18)      Branches: 72.22% (13 / 18)      Functions: 100% (2 / 2)      Lines: 83.33% (15 / 18)      Ignored: none     

All files » ./lib/ » format.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                    1             1             1                   1 7   7 7         7       7 11   11       11 11 4     7        
/**
 *  String format template
 *  - Inspired:  
 *    https://github.com/Matt-Esch/string-template/index.js
 */
 
/**
 * Import(s)
 */
 
var slice = Array.prototype.slice
 
 
/**
 * Constant(s)
 */
 
var RE_NARGS = /\{([0-9a-zA-Z]+)\}/g
 
 
/**
 * Export(s)
 */
 
module.exports = template 
 
 
/**
 * template
 *  
 * @param {String} string
 * @return {String}
 */
 
function template (string) {
  var args
 
  Eif (arguments.length === 2 && typeof arguments[1] === 'object') {
    args = arguments[1]
  } else {
    args = slice.call(arguments, 1)
  }
 
  Iif (!args || !args.hasOwnProperty) {
    args = {}
  }
 
  return string.replace(RE_NARGS, function (match, i, index) {
    var result
 
    Iif (string[index - 1] === '{' &&
      string[index + match.length] === '}') {
      return i
    } else {
      result = args.hasOwnProperty(i) ? args[i] : null
      if (result === null || result === undefined) {
        return ''
      }
 
      return result
    }
  })
}