]> git.r.bdr.sh - rbdr/dotfiles/blob
f1de1e301d42387f5c5d1e8f507d46b429f191ee
[rbdr/dotfiles] /
1 'use strict';
2
3 var isCallable = require('../object/is-callable')
4 , value = require('../object/valid-value')
5
6 , call = Function.prototype.call;
7
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) {
15 var t, r;
16 if (escape) return escape;
17 t = token;
18 while (t && !(r = fmap[t])) t = t.slice(0, -1);
19 if (!r) return match;
20 if (isCallable(r)) r = call.call(r, context);
21 return r + token.slice(t.length);
22 });
23 };
24 };