-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·273 lines (248 loc) · 7.97 KB
/
build.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env node
const fs = require('node:fs');
const da = require('./lib/dimension.js');
const ls = require('./lib/languagestring.js');
const test = require('./lib/testrunner.js');
const validate = require('./lib/validate.js');
const loader = require('./lib/validatingloader.js');
// READ AND VALIDATE
if (!loader.load('.', 'en')) process.exit(1);
const degreesMap = loader.degreesMap;
const dimensionsMap = loader.dimensionsMap;
const prefixesMap = loader.prefixesMap;
const unitTypeMap = loader.unitTypeMap;
const functionSourceMap = loader.functionSourceMap;
const unitMap = loader.unitMap;
const includeMap = loader.includeMap;
const elementsMap = loader.elementsMap;
const solversMap = loader.solversMap;
loader.summarize();
// VALIDATE AND RUN TESTS
test.summarize(test.runAllDir('.'));
if (validate.totalErrorCount()) process.exit(1);
// WRITE MCDBMAIN.JS
function dimObj(dim) {
const obj = {};
if (dim) {
for (const key of Object.keys(dim)) {
obj[dimensionsMap[key]['legacy-id']] = dim[key];
}
}
return obj;
}
function catObj(cat) {
if (cat['include']) return {'i': cat['include']};
return {'t': cat['type'], 'u': cat['units']};
}
function incObj(inc) {
const name = ls.get(inc['name'], 'en', '*');
const categories = inc['categories'].map(catObj);
return {'n': name, 'c': categories};
}
function buildMainJS() {
const lines = [];
lines.push('/* Anything worth doing is worth overdoing. -- Mick Jagger */');
lines.push('if(typeof m!==\'object\')m={};(function(m){');
const mp = {};
for (const key of Object.keys(degreesMap)) {
const item = degreesMap[key];
const nameLS = ls.format(item['format'], ' ');
const name = ls.get(nameLS, 'en', '*').trim();
mp[key] = name;
}
lines.push('m.mp=' + JSON.stringify(mp) + ';');
const dp = {};
const bp = {};
for (const key of Object.keys(prefixesMap)) {
const item = prefixesMap[key];
const symbol = item['symbol'];
const nameLS = ls.format(item['format'], ' ');
const name = ls.get(nameLS, 'en', '*').trim();
const obj = {'s': symbol, 'n': name};
if (key.startsWith('10^')) dp[key.substring(3)] = obj;
if (key.startsWith('2^')) bp[key.substring(2)] = obj;
}
lines.push('m.dp=' + JSON.stringify(dp) + ';');
lines.push('m.bp=' + JSON.stringify(bp) + ';');
const t = {};
for (const key of Object.keys(unitTypeMap)) {
const item = unitTypeMap[key];
const name = ls.get(item['name'], 'en', '*');
const dim = dimObj(item['dimension']);
const obj = {'n': name};
if (!da.empty(dim)) obj['d'] = dim;
t[key] = obj;
}
lines.push('m.t=' + JSON.stringify(t) + ';');
for (const key of Object.keys(functionSourceMap)) {
lines.push('var ' + key + '=' + functionSourceMap[key] + ';');
}
const colorCodes = [];
for (const key of Object.keys(unitMap)) {
const item = unitMap[key];
if (item['cc-map'] !== undefined) {
const ncc = [];
for (const cc of item['cc-map']) {
const obj = {};
for (const k of Object.keys(cc)) {
const h = cc[k]['color'];
const n = ls.get(cc[k]['name'], 'en', '*');
obj[k] = {'h': h, 'n': n};
}
const s = JSON.stringify(obj);
let i = colorCodes.indexOf(s);
if (i < 0) {
i = colorCodes.length;
colorCodes.push(s);
}
ncc.push('cc' + i);
}
item['cc-map.source'] = '[' + ncc.join(',') + ']';
}
}
for (const key of Object.keys(colorCodes)) {
lines.push('var cc' + key + '=' + colorCodes[key] + ';');
}
lines.push('if(!m.r)m.r={};if(!m.u)m.u={};var uu={');
for (const key of Object.keys(unitMap)) {
const item = unitMap[key];
const obj = {};
if (item['symbol'] !== undefined) {
obj['s'] = item['symbol'];
}
if (item['name'] !== undefined) {
const n = ls.get(item['name'], 'en', 1);
const p = ls.get(item['name'], 'en', '*');
obj['n'] = n; if (n !== p) obj['p'] = p;
}
if ((item['datatype'] || 'num') !== 'num') {
obj['k'] = item['datatype'];
}
if (item['tuple-dimension'] !== undefined) {
obj['td'] = item['tuple-dimension'];
}
if (item['dep-name'] !== undefined) {
obj['dn'] = ls.get(item['dep-name'], 'en', '*');
}
if (item['dep-dimension'] !== undefined) {
obj['dd'] = dimObj(item['dep-dimension']);
}
if (item['cc-map'] !== undefined) {
obj['cc'] = '@@@@cc-map.source@@@@';
}
if (item['multiplier'] !== undefined || item['divisor'] !== undefined) {
if (item['multiplier'] !== undefined && item['multiplier'] != 1) {
obj['m'] = +item['multiplier'];
}
if (item['divisor'] !== undefined && item['divisor'] != 1) {
obj['w'] = +item['divisor'];
}
} else if (item['instructions'] !== undefined) {
obj['t'] = '@@@@instructions.forward.source@@@@';
obj['f'] = '@@@@instructions.reverse.source@@@@';
} else if (item['parser'] !== undefined || item['formatter'] !== undefined) {
obj['t'] = '@@@@parser.source@@@@';
obj['f'] = '@@@@formatter.source@@@@';
}
if (item['dimension'] !== undefined) {
obj['d'] = dimObj(item['dimension']);
}
let s = JSON.stringify(obj);
s = s.replaceAll(/("(cc|t|f)":)"@@@@([-a-z.]+)@@@@"/g, (g0, g1, g2, g3) => g1 + item[g3]);
lines.push('"' + key + '":' + s + ',');
}
lines.push('};for(var k in uu)m.u[k]=uu[k];m.r[\'main\']=true;');
lines.push('if(!m.i)m.i={};var ii={');
for (const key of Object.keys(includeMap)) {
const item = includeMap[key];
const obj = incObj(item);
lines.push('"' + key + '":' + JSON.stringify(obj) + ',');
}
lines.push('};for(var k in ii)m.i[k]=ii[k];m.r[\'defaults\']=true;');
lines.push('})(m);');
return lines;
}
fs.writeFileSync('mcdbmain.js', buildMainJS().join('\n'));
console.log('Wrote mcdbmain.js');
// WRITE MCDBMISC.JS
function buildMiscJS() {
const lines = [];
lines.push('/* Anything worth doing is worth overdoing. -- Mick Jagger */');
lines.push('if(typeof m!==\'object\')m={};(function(m){');
lines.push('if(!m.r)m.r={};if(!m.e)m.e={};var ee={');
for (const key of Object.keys(elementsMap)) {
const item = elementsMap[key];
const obj = {};
if (item['symbol'] !== undefined) {
obj['s'] = item['symbol'];
}
if (item['name'] !== undefined) {
const n = ls.get(item['name'], 'en', '*');
const l = ls.get(item['name'], 'la', '*');
obj['n'] = n; if (n !== l) obj['l'] = l;
}
if (item['properties'] !== undefined) {
obj['a'] = {};
for (const key of Object.keys(item['properties'])) {
obj['a'][key] = {};
const prop = item['properties'][key];
if (prop['value'] !== undefined) {
obj['a'][key]['v'] = prop['value'];
}
if (prop['unit'] !== undefined) {
obj['a'][key]['u'] = prop['unit'];
}
}
}
lines.push('"' + key + '":' + JSON.stringify(obj) + ',');
}
lines.push('};for(var k in ee)m.e[k]=ee[k];m.r[\'elements\']=true;');
lines.push('if(!m.s)m.s={};var ss={');
for (const key of Object.keys(solversMap)) {
const item = solversMap[key];
const obj = {};
if (item['name'] !== undefined) {
obj['n'] = ls.get(item['name'], 'en', '*');
}
if (item['variables'] !== undefined) {
obj['v'] = [];
for (const v of item['variables']) {
const vo = {};
switch (v['type']) {
case 'independent': vo['vt'] = 'iv'; break;
case 'dependent': vo['vt'] = 'dv'; break;
}
if (v['register'] !== undefined) {
vo['r'] = v['register'];
}
if (v['name'] !== undefined) {
vo['n'] = ls.get(v['name'], 'en', '*');
}
if (v['dimension'] !== undefined) {
vo['d'] = dimObj(v['dimension']);
}
if (v['unit'] !== undefined) {
vo['u'] = v['unit'];
}
obj['v'].push(vo);
}
}
if (item['solutions'] !== undefined) {
obj['ng'] = 0;
obj['g'] = {};
for (const k of Object.keys(item['solutions'])) {
const n = k.split(',').length;
if (obj['ng'] < n) obj['ng'] = n;
obj['g'][k] = '@@@@' + k + '@@@@';
}
}
let s = JSON.stringify(obj);
s = s.replaceAll(/("([0-9,]+)":)"@@@@\2@@@@"/g, (g0, g1, g2) => g1 + item['solutions.source'][g2]);
lines.push('"' + key + '":' + s + ',');
}
lines.push('};for(var k in ss)m.s[k]=ss[k];m.r[\'solvers\']=true;');
lines.push('})(m);');
return lines;
}
fs.writeFileSync('mcdbmisc.js', buildMiscJS().join('\n'));
console.log('Wrote mcdbmisc.js');