-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts-parser.example.babel.js
53 lines (41 loc) · 1.37 KB
/
ts-parser.example.babel.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
/* eslint-disable no-template-curly-in-string */
class TSParser {
constructor(ts) {
this.stat = [];
this.interp = [];
this.ts = ts;
this.fragments = this.ts.split(/\$\{(.*?)\}/);
this.fragments.forEach((fragment, i) => {
if ((i % 2) === 0) {
this.stat.push(fragment);
} else {
this.interp.push(fragment);
}
});
}
render(input) {
const output = [];
this.fragments.forEach((fragment, i) => {
if ((i % 2) === 0) {
output.push(fragment);
} else {
output.push(input[fragment] || '[[ERROR]]');
}
});
return output.join('');
}
}
const buildingDescFragment = 'a 5-story, walk-up, multi-family building';
const totalUnitsFragment = '18 residential units';
const input = { buildingDescFragment, totalUnitsFragment };
const templateStringDefault =
'The subject consists of ${buildingDescFragment} totaling ${totalUnitsFragment}.';
const templateString1 =
'The subject consists of ${buildingDescFragment}. It has ${totalUnitsFragment}.';
const templateString2 =
'The subject is ${buildingDescFragment} with ${totalUnitsFragment}.';
const testTS1 = new TSParser(templateStringDefault);
const testTS2 = new TSParser(templateString1);
const testTS3 = new TSParser(templateString2);
const tests = [testTS1, testTS2, testTS3];
tests.forEach(test => console.log(test.render(input)));