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