-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstring.js
72 lines (59 loc) · 1.58 KB
/
string.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
Convert a dotted path to a location inside an object.
@private
@example
// returns xfoo
extractValue('wow.it.works', {
wow: {
it: {
works: 'xfoo'
}
}
});
// returns undefined
extractValue('xfoo.bar', { nope: 1 });
@param {String} path dotted to indicate levels in an object.
@param {Object} view for the data.
*/
function extractValue(path, view) {
// Short circuit for direct matches.
if (view && view[path]) return view[path];
var parts = path.split('.');
while (
// view should always be truthy as all objects are.
view &&
// must have a part in the dotted path
(part = parts.shift())
) {
view = (typeof view === 'object' && part in view) ?
view[part] :
undefined;
}
return view;
}
var REGEX = new RegExp('{{([a-zA-Z.-_0-9]+)}}', 'g');
var TEMPLATE_OPEN = '{{';
/**
NOTE: I also wrote an implementation that does not use regex but it is actually slower
in real world usage and this is easier to understand.
@param {String} input template.
@param {Object} view details.
*/
function replace(input, view) {
// optimization to avoid regex calls (indexOf is strictly faster)
if (input.indexOf(TEMPLATE_OPEN) === -1) return input;
var result;
var replaced = input.replace(REGEX, function(original, path) {
var value = extractValue(path, view);
if (undefined === value || null === value) {
return original;
}
if (typeof value === 'object') {
result = value;
return;
}
return value;
});
return (undefined !== result) ? result : replaced;
}
module.exports = replace;