]> gerrit.simantics Code Review - simantics/district.git/blob - es5-ext/string/format-method.js
Hide "enabled" column for non-component type tech type tables
[simantics/district.git] / es5-ext / string / format-method.js
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 };