1
0

format-method.js 643 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var isCallable = require('../object/is-callable')
  3. , value = require('../object/valid-value')
  4. , call = Function.prototype.call;
  5. module.exports = function (fmap) {
  6. fmap = Object(value(fmap));
  7. return function (pattern) {
  8. var context = value(this);
  9. pattern = String(pattern);
  10. return pattern.replace(/%([a-zA-Z]+)|\\([\u0000-\uffff])/g,
  11. function (match, token, escape) {
  12. var t, r;
  13. if (escape) return escape;
  14. t = token;
  15. while (t && !(r = fmap[t])) t = t.slice(0, -1);
  16. if (!r) return match;
  17. if (isCallable(r)) r = call.call(r, context);
  18. return r + token.slice(t.length);
  19. });
  20. };
  21. };