summaryrefslogtreecommitdiff
path: root/node_modules/utile/lib/format.js
blob: dfa25e80121f81140ceb0604bb16818f7f82f6c4 (plain)
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
/*
 * format.js: `util.format` enhancement to allow custom formatting parameters.
 *
 * (C) 2011, Charlie Robbins & the Contributors
 * MIT LICENSE
 *
 */

var util = require('util');

exports = module.exports = function(str) {
  var formats = [].slice.call(arguments, 1, 3);

  if (!(formats[0] instanceof Array && formats[1] instanceof Array) || arguments.length > 3)
    return util.format.apply(null, arguments);

  var replacements = formats.pop(),
      formats = formats.shift();

  formats.forEach(function(format, id) {
    str = str.replace(new RegExp(format), replacements[id]);
  });

  return str;
};