-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (30 loc) · 901 Bytes
/
index.js
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
26
27
28
29
30
31
32
const assign = require('lodash.assign');
const replace = require('lodash.replace');
const reduce = require('lodash.reduce');
module.exports = function urlPlaceholders(_options) {
var options = _options || {};
// {defaults = null, prefix = ':', postfix=''}
if (!options.defaults) {
options.defaults = null;
}
if (!options.prefix) {
options.prefix = ':';
}
if (!options.postfix) {
options.postfix = '';
}
return function interpolate(url, _locals) {
if (arguments.length === 1 && !options.defaults) {
return interpolate.bind(null, url);
}
const locals = assign({}, options.defaults, _locals);
return reduce(locals, function(acc, val, key) {
if (!val) {
return acc;
}
const toReplace = options.prefix + key + options.postfix;
const replaceBy = val;
return replace(acc, toReplace, replaceBy);
}, url);
};
};