-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
149 lines (129 loc) · 4.14 KB
/
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// json-templates
// Simple templating within JSON structures.
//
// Created by Curran Kelleher and Chrostophe Serafin.
// Contributions from Paul Brewer and Javier Blanco Martinez.
const objectPath = require('object-path');
const dedupe = require('dedupe');
// An enhanced version of `typeof` that handles arrays and dates as well.
function type(value) {
let valueType = typeof value;
if (Array.isArray(value)) {
valueType = 'array';
} else if (value instanceof Date) {
valueType = 'date';
} else if (value === null) {
valueType = 'null';
}
return valueType;
}
// Constructs a parameter object from a match result.
// e.g. "['{{foo}}']" --> { key: "foo" }
// e.g. "['{{foo:bar}}']" --> { key: "foo", defaultValue: "bar" }
function Parameter(match) {
let param;
const matchValue = match.substr(2, match.length - 4).trim();
const i = matchValue.indexOf(':');
if (i !== -1) {
param = {
key: matchValue.substr(0, i),
defaultValue: matchValue.substr(i + 1)
};
} else {
param = { key: matchValue };
}
return param;
}
// Constructs a template function with deduped `parameters` property.
function Template(fn, parameters) {
// Paul Brewer Dec 2017 add deduplication call, use only key property to eliminate
Object.assign(fn, {
parameters: dedupe(parameters, item => item.key)
});
return fn;
}
// Parses the given template object.
//
// Returns a function `template(context)` that will "fill in" the template
// with the context object passed to it.
//
// The returned function has a `parameters` property,
// which is an array of parameter descriptor objects,
// each of which has a `key` property and possibly a `defaultValue` property.
function parse(value) {
switch (type(value)) {
case 'string':
return parseString(value);
case 'object':
return parseObject(value);
case 'array':
return parseArray(value);
default:
return Template(function() {
return value;
}, []);
}
}
// Parses leaf nodes of the template object that are strings.
// Also used for parsing keys that contain templates.
const parseString = (() => {
// This regular expression detects instances of the
// template parameter syntax such as {{foo}} or {{foo:someDefault}}.
const regex = /{{(\w|:|[\s-+.,@/\//()?=*_])+}}/g;
return str => {
let parameters = [];
let templateFn = () => str;
if (regex.test(str)) {
const matches = str.match(regex);
parameters = matches.map(Parameter);
templateFn = context => {
context = context || {};
return matches.reduce((str, match, i) => {
const parameter = parameters[i];
let value = objectPath.get(context, parameter.key);
if (value === undefined || value == null) {
value = parameter.defaultValue;
}
if (typeof value === 'object') {
return value;
}
if (value === undefined || value === null) {
return null;
}
return str.replace(match, value);
}, str);
};
}
return Template(templateFn, parameters);
};
})();
// Parses non-leaf-nodes in the template object that are objects.
function parseObject(object) {
const children = Object.keys(object).map(key => ({
keyTemplate: parseString(key),
valueTemplate: parse(object[key])
}));
const templateParameters = children.reduce(
(parameters, child) =>
parameters.concat(child.valueTemplate.parameters, child.keyTemplate.parameters),
[]
);
const templateFn = context => {
return children.reduce((newObject, child) => {
newObject[child.keyTemplate(context)] = child.valueTemplate(context);
return newObject;
}, {});
};
return Template(templateFn, templateParameters);
}
// Parses non-leaf-nodes in the template object that are arrays.
function parseArray(array) {
const templates = array.map(parse);
const templateParameters = templates.reduce(
(parameters, template) => parameters.concat(template.parameters),
[]
);
const templateFn = context => templates.map(template => template(context));
return Template(templateFn, templateParameters);
}
module.exports = parse;