-
Notifications
You must be signed in to change notification settings - Fork 1
/
strif.js
245 lines (203 loc) · 6.08 KB
/
strif.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
let fs = null;
let path = null;
try {
fs = require('fs');
path = require('path');
} catch (error) {
console.warn('WARN [strif]', 'StrifFormatter.fromFile will not work on the Browser');
}
class StrifVar {
constructor(name, opts) {
if (!name) {
throw new Error('name is required');
} else if (typeof name != 'string') {
throw new Error('name is required to be a string');
} else {
this.name = name;
}
this.accessor = opts.accessor || name;
this.transformers = opts.transformers;
this.opts = opts;
}
hasTransformers() {
return this.transformers && this.transformers.length > 0;
}
getFromObject(obj) {
let str = this.accessor.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '');
for (let k of str.split('.')) {
if (k in obj) obj = obj[k] || null;
else return null;
}
if (this.opts.type && !(typeof obj === this.opts.type)) {
throw new Error(`Var {${this.name}} type does not match "${this.opts.type}"`);
}
return obj;
}
}
class StrifTemplate {
constructor(template, transformers, options = {}) {
if (!template) {
throw new Error('template is required');
} else if (typeof template != 'string') {
throw new Error('template is required to be a string');
} else {
this.template = template;
}
this._options = options;
this._transformers = transformers;
this._props = [];
if (options.props) {
for (let key in options.props) {
let prop = options.props[key];
this.prop(key, prop);
}
}
}
prop(name, opts = {}) {
this._props.push(new StrifVar(name, opts));
return this;
}
compile(data, options = {}) {
options = {
...StrifTemplate.DefaultCompileOptions,
...options
}
// TODO: Check and see if cloning is necessary here
const dataClone = { ...data };
let transformers = this._transformers;
// TODO: Look for better/more performant way of checking this
// Maybe check on reduce instead
if (options.ignoreTransformers) {
transformers = {};
Object.keys(this._transformers).forEach(key => {
transformers[key] = this._transformers[key];
if (options.ignoreTransformers.includes(key)) {
transformers[key].ignore = true;
}
});
} else {
Object.keys(this._transformers).forEach(key => {
transformers[key].ignore = false;
});
}
let map = dataClone;
for (let prop of this._props) {
let propData = prop.getFromObject(dataClone);
if (propData) {
map[prop.name] = propData;
}
if (prop.hasTransformers()) {
map[prop.name] = prop.transformers
.reduce((prev, curr) => {
const isFn = typeof curr === 'function';
const isTransformer = isFn || transformers[curr] != undefined;
if (!isTransformer) {
throw new Error('Transformer not found and is not a function: ' + curr);
}
if (isFn) return curr(prev);
if (transformers[curr].ignore) {
return prev;
}
return transformers[curr](prev);
}, map[prop.name]);
}
}
return StrifTemplate.compile(this.template, map, options);
}
static compile(template, data, options) {
// TODO: memoize/cache?
return template.replace(
/([{}])\1|[{](.*?)(?:!(.+?))?[}]/g,
(m, l, key) => {
if (key || l) {
let val = data[key || l] || '';
return val;
}
return data;
});
}
}
StrifTemplate.DefaultCompileOptions = {
ignoreTransformers: null
};
class StrifFormatter {
/**
* @param {object} opts
* @param {object} opts.transformers
* @param {string[]} opts.plugins
*/
constructor(opts = {}) {
this.opts = opts;
this.transformers = {
...StrifFormatter.DEFAULT_TRANSFORMERS,
...this.opts.transformers
};
if (opts.plugins) {
if (!path) {
return console.warn('WARN [strif]', '"Plugins" don not work on the browser... for now...');
}
this.opts.plugins.forEach(plugPath => {
let plugin = require(path.resolve(plugPath));
if (plugin.transformers) {
this.transformers = {
...this.transformers,
...plugin.transformers,
};
}
});
}
}
addTransformer(name, transformer) {
if (typeof name != 'string') {
throw new Error('name is required to be a string');
}
if (typeof transformer != 'function') {
throw new Error('transformer is required to be a function');
}
this.transformers[name] = transformer;
return this;
}
/**
* @param {string} template
* @param {object} options
*/
template(template, options) {
return new StrifTemplate(template, this.transformers, options);
}
/**
* @param {string} path
* @param {object} options
*/
fromFile(filePath, options) {
if (!fs) {
return console.warn('WARN [strif]', '"StrifFormatter.fromFile "does not work on the browser');
}
if (!filePath) {
throw new Error(
'filePath is required');
} else if (typeof filePath != 'string') {
throw new Error(
'filePath is required to be a string');
}
let template = fs.readFileSync(filePath).toString();
return new StrifTemplate(template, this.transformers, options);
}
}
StrifFormatter.DEFAULT_TRANSFORMERS = {};
const DEFAULT_FORMATTER_OPTS = {
transformers: {
date: s => new Date(s),
lds: d => d.toLocaleString(),
capitalize: s => {
return s && s[0].toUpperCase() + s.slice(1);
}
}
};
let strif = new StrifFormatter(DEFAULT_FORMATTER_OPTS);
strif.Formatter = StrifFormatter;
strif.Template = StrifTemplate;
strif.Var = StrifVar;
strif.create = (opts) => new StrifFormatter(opts);
strif.compile = StrifTemplate.compile;
global.strif = strif;
module.exports = strif;