-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
160 lines (146 loc) · 5.42 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
150
151
152
153
154
155
156
157
158
159
160
const vm = require('vm');
const JOIN = Symbol('join');
let evaluate;
let parse;
const reserved = `await break case catch class const continue debugger default delete do else enum export extends false finally for
function if implements import in instanceof interface let new null package private protected public return static super switch
this throw true try typeof var void while with yield`.split(/\s+/);
const isNotReserved = item => !reserved.includes(item);
const getHandler = escapeMap => {
let handler;
if (typeof escapeMap === 'function') handler = escapeMap;
else {
const regExp = new RegExp('([' + Object.keys(escapeMap).join('') + '])', 'g');
handler = str => String(str).replace(regExp, (str, x) => escapeMap[x]);
}
return str => {
const join = str?.[JOIN];
return (typeof join === 'string') ? str.join(join) : handler(str);
};
};
const getTag = escape => {
return (strings, ...values) => {
return Array
.from({length: strings.length + values.length}, (v, i) => {
return i % 2 ? escape(values[(i - 1) / 2]) : strings[i / 2];
})
.filter(Boolean)
.join('');
};
};
const stringify = str => {
let result = JSON.stringify(str) || '';
if (!result.startsWith('"')) result = JSON.stringify(result);
return result.slice(1, -1);
};
const escapeXml = getHandler({
'&': '&',
'"': '"',
'>': '>',
'<': '<',
'\'': '''
});
const escapeHtml = getHandler({
'&': '&',
'"': '"',
'>': '>',
'<': '<',
'\'': '''
});
const escapeJson = getHandler(stringify);
const stringifyUndefined = value => JSON.stringify(value == null ? null : value);
const handlers = {
escapeHtml,
escapeXml,
escapeJson,
join: (array, string = '') => {
array = [].concat(array);
array[JOIN] = string;
return array;
},
xml: getTag(escapeXml),
html: getTag(escapeHtml),
json: getTag(getHandler(stringifyUndefined))
};
const tags = {
escapeXml: getTag(escapeXml),
escapeHtml: getTag(escapeHtml),
escapeJson: getTag(escapeJson)
};
function template(useParse, templateString, templateVariables, ut = {}, escape, maxDepth = 100) {
Object.assign(ut, handlers, {tags});
const array = Array.isArray(templateVariables);
if (!array) {
switch (typeof templateString) {
case 'string':
break;
case 'object':
if (templateString !== null) {
if (typeof maxDepth !== 'number' || --maxDepth < 0) throw new Error('max depth reached!');
Object.entries(templateString).forEach(([key, value]) => {
if (typeof value === 'string' && value) {
// recurse and reassign only if not empty string
templateString[key] = template(useParse, value, templateVariables, ut, escape, maxDepth);
} else if (typeof value === 'object' && value !== null) {
// recurse only if iterable object. No reassign needed.
template(useParse, value, templateVariables, ut, escape, maxDepth);
}
});
}
return templateString;
default:
return templateString;
}
}
const [keys, values] = Object.entries(templateVariables).reduce((prev, cur) => {
const name = cur[array ? 1 : 0].split(/^[^a-zA-Z_$]|[^\w$]/g).join('_');
if (!prev[0].includes(name) && isNotReserved(name)) { // skip duplicates
prev[0].push(name);
if (!array) prev[1].push(cur[1]);
}
return prev;
}, [['ut'], [ut]]);
let functionBody;
switch (escape) {
case 'xml':
functionBody = `return ut.tags.escapeXml\`${stringify(templateString)}\`;`;
break;
case 'html':
functionBody = `return ut.tags.escapeHtml\`${stringify(templateString)}\`;`;
break;
case 'json':
functionBody = `return ut.tags.escapeJson\`${stringify(templateString)}\`;`;
break;
case 'stringify':
functionBody = `return ut.json\`${stringify(templateString)}\`;`;
break;
default:
functionBody = `return \`${stringify(templateString)}\`;`;
break;
}
let templateFunction;
try {
if (vm.compileFunction && !useParse) {
templateFunction = vm.compileFunction(functionBody, keys);
} else {
evaluate = evaluate || require('./eval');
parse = parse || require('acorn').parse;
const parsed = parse(functionBody.substring(6), {ecmaVersion: 2022}).body[0].expression;
templateFunction = function() {
const params = keys.reduce((memo, key, idx) => {
memo[key] = arguments[idx];
return memo;
}, {});
const ut = arguments[0];
return evaluate(parsed, {ut, ...params});
};
}
} catch (e) {
e.templateString = templateString;
throw e;
}
return array ? (...params) => templateFunction(ut, ...params) : templateFunction.apply(templateVariables, values);
};
const exp = template.bind(null, false);
exp.evaluate = template.bind(null, true);
module.exports = exp;