exprify 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -1
- package/dist/{exprify.cjs.js → exprify.cjs.cjs} +1 -1
- package/dist/exprify.cjs.cjs.map +1 -0
- package/dist/exprify.js +2 -2
- package/dist/exprify.min.js +1 -1
- package/package.json +15 -6
- package/src/core/Exprify.js +369 -0
- package/src/core/context.js +30 -0
- package/src/function/executor.js +64 -0
- package/src/function/internal.js +612 -0
- package/src/function/registry.js +68 -0
- package/src/index.js +2 -0
- package/src/math/operations.js +38 -0
- package/src/parser/astBuild.js +531 -0
- package/src/parser/evaluator.js +460 -0
- package/src/parser/tokenizer.js +399 -0
- package/src/utils/globalUnits.js +217 -0
- package/src/utils/matrix.js +53 -0
- package/src/utils/store.js +178 -0
- package/src/variables/store.js +75 -0
- package/dist/exprify.cjs.js.map +0 -1
- package/docs/README.md +0 -34
- package/docs/assets/css/style.scss +0 -4
- package/docs/tokenType.txt +0 -21
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
export function tokenize(expr, context = {}) {
|
|
2
|
+
const tokens = [];
|
|
3
|
+
let current = "";
|
|
4
|
+
let quote = "";
|
|
5
|
+
|
|
6
|
+
const operators = ["+", "-", "*", "/", "%", "^", "=", ">", "<", "!", "&", "|"];
|
|
7
|
+
const multiOps = [
|
|
8
|
+
"==", ">=", "<=", "&&", "||",
|
|
9
|
+
"+=", "-=", "*=", "/=", "%=",
|
|
10
|
+
"?.", "??", "|>"
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const parentheses = "()";
|
|
14
|
+
const comma = ",";
|
|
15
|
+
const semicolon = ";";
|
|
16
|
+
const keywords = ["to", "in"];
|
|
17
|
+
// const functions = context.functions?.getAllFunctionsName?.() || [];
|
|
18
|
+
const units = context.units?.getAllUnitsFlat?.() || [];
|
|
19
|
+
|
|
20
|
+
const isIdentifier = (s) => /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(s);
|
|
21
|
+
|
|
22
|
+
function getContext(str, charIndex) {
|
|
23
|
+
// 1. Extract all alphanumeric words into an array
|
|
24
|
+
const words = str.match(/[a-z0-9]+/gi) || [];
|
|
25
|
+
|
|
26
|
+
// 2. Identify the current character and the one immediately before it
|
|
27
|
+
const currentChar = str[charIndex] || null;
|
|
28
|
+
const prevChar = charIndex > 0 ? str[charIndex - 1] : null;
|
|
29
|
+
|
|
30
|
+
// 3. Find the word that contains the current charIndex
|
|
31
|
+
let start = charIndex;
|
|
32
|
+
// Move pointer back to the start of the current word
|
|
33
|
+
while (start > 0 && /[a-z0-9]/i.test(str[start - 1])) start--;
|
|
34
|
+
|
|
35
|
+
let end = charIndex;
|
|
36
|
+
// Move pointer forward to the end of the current word
|
|
37
|
+
while (end < str.length && /[a-z0-9]/i.test(str[end])) end++;
|
|
38
|
+
|
|
39
|
+
const currentWord = str.substring(start, end);
|
|
40
|
+
|
|
41
|
+
// 4. Find the word that appears before the currentWord in the sequence
|
|
42
|
+
const currentWordIdx = words.indexOf(currentWord);
|
|
43
|
+
const prevWord = currentWordIdx > 0 ? words[currentWordIdx - 1] : null;
|
|
44
|
+
|
|
45
|
+
// 5. Find the word that appears after the currentWord
|
|
46
|
+
const nextWord = (currentWordIdx !== -1 && currentWordIdx < words.length - 1)
|
|
47
|
+
? words[currentWordIdx + 1]
|
|
48
|
+
: null;
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
prevWord: prevWord,
|
|
52
|
+
prevChar: prevChar,
|
|
53
|
+
currentWord: currentWord,
|
|
54
|
+
currentChar: currentChar,
|
|
55
|
+
nextWord: nextWord
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const isUnaryContext = (prev) =>
|
|
60
|
+
!prev ||
|
|
61
|
+
prev.type === "Operator" ||
|
|
62
|
+
prev.type === "UnaryOperator" ||
|
|
63
|
+
(prev.type === "Parenthesis" && prev.value !== ")") ||
|
|
64
|
+
prev.type === "ArrayStart" ||
|
|
65
|
+
prev.type === "Semicolon" ||
|
|
66
|
+
prev.type === "Comma" ||
|
|
67
|
+
prev.type === "Ternary";
|
|
68
|
+
|
|
69
|
+
const flushCurrent = (nextChar, index) => {
|
|
70
|
+
if (!current) return;
|
|
71
|
+
|
|
72
|
+
// BOOLEAN
|
|
73
|
+
if (/^(true|false)$/i.test(current)) {
|
|
74
|
+
tokens.push({ type: "Boolean", value: current.toLowerCase() === "true" });
|
|
75
|
+
current = "";
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// KEYWORD
|
|
80
|
+
if (keywords.includes(current)) {
|
|
81
|
+
tokens.push({ type: "Keyword", value: current, pos: index });
|
|
82
|
+
current = "";
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// BIGINT
|
|
87
|
+
if (/^\d+n$/.test(current)) {
|
|
88
|
+
tokens.push({ type: "BigInt", value: BigInt(current.slice(0, -1)), pos: index });
|
|
89
|
+
current = "";
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// HEX
|
|
94
|
+
if (/^0x[0-9a-fA-F]+$/.test(current)) {
|
|
95
|
+
tokens.push({ type: "Number", value: parseInt(current, 16), pos: index });
|
|
96
|
+
current = "";
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// BINARY
|
|
101
|
+
if (/^0b[01]+$/.test(current)) {
|
|
102
|
+
tokens.push({ type: "Number", value: parseInt(current, 2), pos: index });
|
|
103
|
+
current = "";
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// NUMBER (including scientific)
|
|
108
|
+
if (/^[+-]?(\d+(\.\d+)?|\.\d+)(e[+-]?\d+)?$/i.test(current)) {
|
|
109
|
+
tokens.push({ type: "Number", value: parseFloat(current), pos: index });
|
|
110
|
+
current = "";
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// IMAGINARY NUMBER
|
|
115
|
+
if (/^[+-]?(\d+(\.\d+)?|\.\d+)(e[+-]?\d+)?i$/i.test(current)) {
|
|
116
|
+
tokens.push({
|
|
117
|
+
type: "ImaginaryLiteral",
|
|
118
|
+
value: parseFloat(current.slice(0, -1)),
|
|
119
|
+
pos: index
|
|
120
|
+
});
|
|
121
|
+
current = "";
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// IMAGINARY UNIT
|
|
126
|
+
if (/^[+-]?i$/i.test(current)) {
|
|
127
|
+
const sign = current[0] === "-" ? -1 : 1;
|
|
128
|
+
tokens.push({
|
|
129
|
+
type: "ImaginaryLiteral",
|
|
130
|
+
value: sign,
|
|
131
|
+
pos: index
|
|
132
|
+
});
|
|
133
|
+
current = "";
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// NUMBER + UNIT
|
|
138
|
+
const numUnit = current.match(/^([+-]?\d+(\.\d+)?)([a-zA-Z]+)$/);
|
|
139
|
+
if (numUnit) {
|
|
140
|
+
const value = parseFloat(numUnit[1]);
|
|
141
|
+
const unit = numUnit[3];
|
|
142
|
+
|
|
143
|
+
tokens.push({
|
|
144
|
+
type: units.includes(unit) ? "NumberWithUnit" : "UnknownUnit",
|
|
145
|
+
value,
|
|
146
|
+
unit,
|
|
147
|
+
pos: index
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
current = "";
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// UNIT
|
|
155
|
+
if (units.includes(current)) {
|
|
156
|
+
const {prevWord} = getContext(expr, index);
|
|
157
|
+
if (nextChar !== "(") {
|
|
158
|
+
if (prevWord){
|
|
159
|
+
if (!isNaN(parseFloat(prevWord)) || prevWord === "to" || prevWord === "in") {
|
|
160
|
+
// console.log("Context for unit detection:", {current, prevWord, nextChar});
|
|
161
|
+
|
|
162
|
+
tokens.push({ type: "Unit", value: current, pos: index });
|
|
163
|
+
current = "";
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// IDENTIFIER
|
|
171
|
+
if (isIdentifier(current)) {
|
|
172
|
+
if (nextChar === "(") {
|
|
173
|
+
tokens.push({
|
|
174
|
+
type: "Function",
|
|
175
|
+
name: current,
|
|
176
|
+
pos: index
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
tokens.push({
|
|
180
|
+
type: "Identifier",
|
|
181
|
+
name: current,
|
|
182
|
+
pos: index
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
current = "";
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
throw new Error(`Invalid token "${current}" at index ${index}`);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
for (let i = 0; i < expr.length; i++) {
|
|
195
|
+
let char = expr[i];
|
|
196
|
+
let next = expr[i + 1];
|
|
197
|
+
|
|
198
|
+
// comments
|
|
199
|
+
if (char === "/" && next === "/") {
|
|
200
|
+
while (i < expr.length && expr[i] !== "\n") i++;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (char === "/" && next === "*") {
|
|
205
|
+
i += 2;
|
|
206
|
+
while (i < expr.length && !(expr[i] === "*" && expr[i + 1] === "/")) i++;
|
|
207
|
+
i++;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// string
|
|
212
|
+
if (`"'`.includes(char)) {
|
|
213
|
+
if (!quote) {
|
|
214
|
+
quote = char;
|
|
215
|
+
current += char;
|
|
216
|
+
} else if (quote === char) {
|
|
217
|
+
current += char;
|
|
218
|
+
tokens.push({
|
|
219
|
+
type: "String",
|
|
220
|
+
value: current.slice(1, -1),
|
|
221
|
+
pos: i
|
|
222
|
+
});
|
|
223
|
+
current = "";
|
|
224
|
+
quote = "";
|
|
225
|
+
} else {
|
|
226
|
+
current += char;
|
|
227
|
+
}
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (quote) {
|
|
232
|
+
if (char === "\\") {
|
|
233
|
+
current += char + expr[++i];
|
|
234
|
+
} else {
|
|
235
|
+
current += char;
|
|
236
|
+
}
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// multi operators
|
|
241
|
+
const twoChar = char + next;
|
|
242
|
+
if (multiOps.includes(twoChar)) {
|
|
243
|
+
flushCurrent(char, i);
|
|
244
|
+
tokens.push({ type: "Operator", value: twoChar, pos: i });
|
|
245
|
+
i++;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (char === "?") {
|
|
250
|
+
tokens.push({ type: "Ternary", value: "?" });
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// only treat ':' as ternary IF previous token was '?'
|
|
255
|
+
if (char === ":") {
|
|
256
|
+
flushCurrent(char, i);
|
|
257
|
+
const prev = tokens[tokens.length - 1];
|
|
258
|
+
|
|
259
|
+
if (prev && prev.type === "Ternary") {
|
|
260
|
+
tokens.push({ type: "Ternary", value: ":" });
|
|
261
|
+
} else {
|
|
262
|
+
tokens.push({ type: "Colon" });
|
|
263
|
+
}
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// dot
|
|
268
|
+
if (char === "." && /\d/.test(current) && /\d/.test(next)) {
|
|
269
|
+
current += char;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (char === ".") {
|
|
274
|
+
flushCurrent(char, i);
|
|
275
|
+
tokens.push({ type: "Dot", pos: i });
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// operators
|
|
280
|
+
if (operators.includes(char)) {
|
|
281
|
+
flushCurrent(char, i);
|
|
282
|
+
|
|
283
|
+
const prev = tokens[tokens.length - 1];
|
|
284
|
+
if ((char === "-" || char === "!") && isUnaryContext(prev)) {
|
|
285
|
+
tokens.push({ type: "UnaryOperator", value: char, pos: i });
|
|
286
|
+
} else {
|
|
287
|
+
tokens.push({ type: "Operator", value: char, pos: i });
|
|
288
|
+
}
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// parenthesis
|
|
293
|
+
if (parentheses.includes(char)) {
|
|
294
|
+
flushCurrent(char, i);
|
|
295
|
+
tokens.push({ type: "Parenthesis", value: char, pos: i });
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// array
|
|
300
|
+
if (char === "[") {
|
|
301
|
+
flushCurrent(char, i);
|
|
302
|
+
tokens.push({ type: "ArrayStart", pos: i });
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (char === "]") {
|
|
307
|
+
flushCurrent(char, i);
|
|
308
|
+
tokens.push({ type: "ArrayEnd", pos: i });
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// OBJECT START
|
|
313
|
+
if (char === "{") {
|
|
314
|
+
flushCurrent(char, i);
|
|
315
|
+
tokens.push({ type: "BlockStart", pos: i });
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// OBJECT END
|
|
320
|
+
if (char === "}") {
|
|
321
|
+
flushCurrent(char, i);
|
|
322
|
+
tokens.push({ type: "BlockEnd", pos: i });
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// comma
|
|
327
|
+
if (char === comma) {
|
|
328
|
+
flushCurrent(char, i);
|
|
329
|
+
tokens.push({ type: "Comma", pos: i });
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// semicolon
|
|
334
|
+
if (char === semicolon) {
|
|
335
|
+
flushCurrent(char, i);
|
|
336
|
+
tokens.push({ type: "Semicolon", pos: i });
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// space
|
|
341
|
+
if (char === " ") {
|
|
342
|
+
flushCurrent(next, i);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// build token
|
|
347
|
+
current += char;
|
|
348
|
+
|
|
349
|
+
if (i === expr.length - 1) {
|
|
350
|
+
flushCurrent(null, i);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (quote) throw new Error("Unclosed string literal");
|
|
355
|
+
|
|
356
|
+
// merge number + unit
|
|
357
|
+
const merged = [];
|
|
358
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
359
|
+
const t = tokens[i];
|
|
360
|
+
const next = tokens[i + 1];
|
|
361
|
+
|
|
362
|
+
if (t?.type === "Number" && next?.type === "Unit") {
|
|
363
|
+
merged.push({
|
|
364
|
+
type: "NumberWithUnit",
|
|
365
|
+
value: t.value,
|
|
366
|
+
unit: next.value,
|
|
367
|
+
pos: t.pos
|
|
368
|
+
});
|
|
369
|
+
i++;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
merged.push(t);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// implicit multiplication
|
|
377
|
+
const final = [];
|
|
378
|
+
for (let i = 0; i < merged.length; i++) {
|
|
379
|
+
const a = merged[i];
|
|
380
|
+
const b = merged[i + 1];
|
|
381
|
+
|
|
382
|
+
final.push(a);
|
|
383
|
+
|
|
384
|
+
if (
|
|
385
|
+
a && b &&
|
|
386
|
+
(
|
|
387
|
+
(["Number", "Identifier"].includes(a.type) ||
|
|
388
|
+
(a.type === "Parenthesis" && a.value === ")") ||
|
|
389
|
+
a.type === "ArrayEnd") &&
|
|
390
|
+
(["Identifier", "Function"].includes(b.type) ||
|
|
391
|
+
(b.type === "Parenthesis" && b.value === "("))
|
|
392
|
+
)
|
|
393
|
+
) {
|
|
394
|
+
final.push({ type: "Operator", value: "*", implicit: true });
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return final;
|
|
399
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
export const globalUnits = {
|
|
2
|
+
// Length
|
|
3
|
+
length: {
|
|
4
|
+
m: { value: 1, unit: 'meter', symbol: 'm' },
|
|
5
|
+
cm: { value: 0.01, unit: 'centimeter', symbol: 'cm' },
|
|
6
|
+
mm: { value: 0.001, unit: 'millimeter', symbol: 'mm' },
|
|
7
|
+
km: { value: 1000, unit: 'kilometer', symbol: 'km' },
|
|
8
|
+
um: { value: 0.000001, unit: 'micrometer', symbol: 'um', note: 'also called micron' },
|
|
9
|
+
nm: { value: 0.000000001, unit: 'nanometer', symbol: 'nm' },
|
|
10
|
+
px: { value: 0.000264583, unit: 'pixel', symbol: 'px', note: '96dpi standard' },
|
|
11
|
+
em: { value: 0.000264583 * 16, unit: 'em', symbol: 'em', note: '1em = 16px by default' },
|
|
12
|
+
rem: { value: 0.000264583 * 16, unit: 'rem', symbol: 'rem', note: 'root em = 16px by default' },
|
|
13
|
+
pt: { value: 0.000352778, unit: 'point', symbol: 'pt', note: '1pt = 1/72 inch' },
|
|
14
|
+
pc: { value: 0.00423333, unit: 'pica', symbol: 'pc', note: '1pc = 12pt' },
|
|
15
|
+
inch: { value: 0.0254, unit: 'inch', symbol: 'in' },
|
|
16
|
+
ft: { value: 0.3048, unit: 'foot', symbol: 'ft' },
|
|
17
|
+
yd: { value: 0.9144, unit: 'yard', symbol: 'yd' },
|
|
18
|
+
mi: { value: 1609.344, unit: 'mile', symbol: 'mi' },
|
|
19
|
+
thou: { value: 0.0000254, unit: 'mil', symbol: 'thou', note: 'thousandth of an inch' },
|
|
20
|
+
furlong: { value: 201.168, unit: 'furlong', symbol: 'fur', note: '220 yards' },
|
|
21
|
+
nmi: { value: 1852, unit: 'nautical mile', symbol: 'nmi' },
|
|
22
|
+
fathom: { value: 1.8288, unit: 'fathom', symbol: 'fathom' },
|
|
23
|
+
au: { value: 1.496e11, unit: 'astronomical unit', symbol: 'AU' },
|
|
24
|
+
ly: { value: 9.4607e15, unit: 'light year', symbol: 'ly' },
|
|
25
|
+
pc: { value: 3.0857e16, unit: 'parsec', symbol: 'pc' }
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
// Weight / Mass
|
|
29
|
+
weight: {
|
|
30
|
+
mg: { value: 1e-6, unit: 'milligram', symbol: 'mg' },
|
|
31
|
+
g: { value: 0.001, unit: 'gram', symbol: 'g' },
|
|
32
|
+
kg: { value: 1, unit: 'kilogram', symbol: 'kg' },
|
|
33
|
+
t: { value: 1000, unit: 'tonne', symbol: 't', note: 'metric ton' },
|
|
34
|
+
lb: { value: 0.453592, unit: 'pound', symbol: 'lb' },
|
|
35
|
+
oz: { value: 0.0283495, unit: 'ounce', symbol: 'oz' },
|
|
36
|
+
stone: { value: 6.35029, unit: 'stone', symbol: 'st', note: '1 stone = 14 lb' }
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
// Time
|
|
40
|
+
time: {
|
|
41
|
+
s: { value: 1, unit: 'second', symbol: 's' },
|
|
42
|
+
min: { value: 60, unit: 'minute', symbol: 'min' },
|
|
43
|
+
h: { value: 3600, unit: 'hour', symbol: 'h' },
|
|
44
|
+
day: { value: 86400, unit: 'day', symbol: 'd' },
|
|
45
|
+
week: { value: 604800, unit: 'week', symbol: 'wk' },
|
|
46
|
+
month: { value: 2629800, unit: 'month', symbol: 'mo', note: 'average month = 30.44 days' },
|
|
47
|
+
year: { value: 31557600, unit: 'year', symbol: 'yr', note: 'average year = 365.25 days' }
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
// Voltage
|
|
51
|
+
voltage: {
|
|
52
|
+
V: { value: 1, unit: 'volt', symbol: 'V' },
|
|
53
|
+
mV: { value: 0.001, unit: 'millivolt', symbol: 'mV' },
|
|
54
|
+
kV: { value: 1000, unit: 'kilovolt', symbol: 'kV' },
|
|
55
|
+
MV: { value: 1e6, unit: 'megavolt', symbol: 'MV' },
|
|
56
|
+
GV: { value: 1e9, unit: 'gigavolt', symbol: 'GV' },
|
|
57
|
+
statV: { value: 299.792458, unit: 'statvolt', symbol: 'statV', note: 'CGS unit' },
|
|
58
|
+
abV: { value: 1e-8, unit: 'abvolt', symbol: 'abV', note: 'CGS electromagnetic unit' }
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
// Frequency
|
|
62
|
+
frequency: {
|
|
63
|
+
Hz: { value: 1, unit: 'hertz', symbol: 'Hz', note: '1 cycle per second' },
|
|
64
|
+
kHz: { value: 1e3, unit: 'kilohertz', symbol: 'kHz' },
|
|
65
|
+
MHz: { value: 1e6, unit: 'megahertz', symbol: 'MHz' },
|
|
66
|
+
GHz: { value: 1e9, unit: 'gigahertz', symbol: 'GHz' },
|
|
67
|
+
THz: { value: 1e12, unit: 'terahertz', symbol: 'THz' }
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
// Power
|
|
71
|
+
power: {
|
|
72
|
+
W: { value: 1, unit: 'watt', symbol: 'W', note: '1 joule per second' },
|
|
73
|
+
mW: { value: 0.001, unit: 'milliwatt', symbol: 'mW' },
|
|
74
|
+
kW: { value: 1000, unit: 'kilowatt', symbol: 'kW' },
|
|
75
|
+
MW: { value: 1e6, unit: 'megawatt', symbol: 'MW' },
|
|
76
|
+
GW: { value: 1e9, unit: 'gigawatt', symbol: 'GW' },
|
|
77
|
+
HP: { value: 745.7, unit: 'horsepower', symbol: 'HP', note: 'mechanical HP = 745.7 W' },
|
|
78
|
+
kcal_per_h: { value: 1.163, unit: 'kilocalorie per hour', symbol: 'kcal/h', note: '= 1.163 W' },
|
|
79
|
+
BTU_per_h: { value: 0.29307107, unit: 'BTU per hour', symbol: 'BTU/h', note: '= 0.293 W' }
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
// Sound
|
|
83
|
+
sound: {
|
|
84
|
+
dB: { value: 1, unit: 'decibel', symbol: 'dB', note: 'logarithmic unit of sound intensity' },
|
|
85
|
+
dBA: { value: 1, unit: 'A-weighted decibel', symbol: 'dBA', note: 'Adjusted for human hearing' },
|
|
86
|
+
dBC: { value: 1, unit: 'C-weighted decibel', symbol: 'dBC', note: 'Flat weighting for high-level sounds' }
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
// Temperature
|
|
90
|
+
temperature: {
|
|
91
|
+
K: { value: 1, unit: 'kelvin', symbol: 'K' },
|
|
92
|
+
C: { value: 1, unit: 'Celsius', symbol: '°C', note: '°C → K: add 273.15' },
|
|
93
|
+
F: { value: 1, unit: 'Fahrenheit', symbol: '°F', note: '°F → K: (°F - 32) * 5/9 + 273.15' }
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
// Pressure
|
|
97
|
+
pressure: {
|
|
98
|
+
Pa: { value: 1, unit: 'pascal', symbol: 'Pa' },
|
|
99
|
+
kPa: { value: 1000, unit: 'kilopascal', symbol: 'kPa' },
|
|
100
|
+
MPa: { value: 1e6, unit: 'megapascal', symbol: 'MPa' },
|
|
101
|
+
bar: { value: 1e5, unit: 'bar', symbol: 'bar' },
|
|
102
|
+
atm: { value: 101325, unit: 'atmosphere', symbol: 'atm' },
|
|
103
|
+
psi: { value: 6894.757, unit: 'pound per square inch', symbol: 'psi' },
|
|
104
|
+
mmHg:{ value: 133.322, unit: 'millimeter of mercury', symbol: 'mmHg' }
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
// Energy
|
|
108
|
+
energy: {
|
|
109
|
+
J: { value: 1, unit: 'joule', symbol: 'J' },
|
|
110
|
+
kJ: { value: 1000, unit: 'kilojoule', symbol: 'kJ' },
|
|
111
|
+
cal: { value: 4.184, unit: 'calorie', symbol: 'cal' },
|
|
112
|
+
kcal:{ value: 4184, unit: 'kilocalorie', symbol: 'kcal' },
|
|
113
|
+
eV: { value: 1.60218e-19, unit: 'electronvolt', symbol: 'eV' },
|
|
114
|
+
BTU: { value: 1055.06, unit: 'BTU', symbol: 'BTU' }
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
// Force
|
|
118
|
+
force: {
|
|
119
|
+
N: { value: 1, unit: 'newton', symbol: 'N' },
|
|
120
|
+
kN: { value: 1000, unit: 'kilonewton', symbol: 'kN' },
|
|
121
|
+
lbf: { value: 4.44822, unit: 'pound-force', symbol: 'lbf' },
|
|
122
|
+
kgf: { value: 9.80665, unit: 'kilogram-force', symbol: 'kgf' },
|
|
123
|
+
dyne:{ value: 1e-5, unit: 'dyne', symbol: 'dyn' }
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
// Area
|
|
127
|
+
area: {
|
|
128
|
+
m2: { value: 1, unit: 'square meter', symbol: 'm²' },
|
|
129
|
+
cm2: { value: 0.0001, unit: 'square centimeter', symbol: 'cm²' },
|
|
130
|
+
km2: { value: 1e6, unit: 'square kilometer', symbol: 'km²' },
|
|
131
|
+
acre: { value: 4046.856, unit: 'acre', symbol: 'acre' },
|
|
132
|
+
hectare:{ value: 10000, unit: 'hectare', symbol: 'ha' },
|
|
133
|
+
ft2: { value: 0.092903, unit: 'square foot', symbol: 'ft²' },
|
|
134
|
+
yd2: { value: 0.836127, unit: 'square yard', symbol: 'yd²' }
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
// Volume
|
|
138
|
+
volume: {
|
|
139
|
+
m3: { value: 1, unit: 'cubic meter', symbol: 'm³' },
|
|
140
|
+
L: { value: 0.001, unit: 'liter', symbol: 'L' },
|
|
141
|
+
mL: { value: 1e-6, unit: 'milliliter', symbol: 'mL' },
|
|
142
|
+
gallon:{ value: 0.00378541, unit: 'US gallon', symbol: 'gal' },
|
|
143
|
+
pint: { value: 0.000473176, unit: 'US pint', symbol: 'pt' },
|
|
144
|
+
floz: { value: 2.9574e-5, unit: 'US fluid ounce', symbol: 'fl oz' }
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
// Electrical Current
|
|
148
|
+
current: {
|
|
149
|
+
A: { value: 1, unit: 'ampere', symbol: 'A' },
|
|
150
|
+
mA: { value: 0.001, unit: 'milliampere', symbol: 'mA' },
|
|
151
|
+
uA: { value: 0.000001, unit: 'microampere', symbol: 'uA' },
|
|
152
|
+
kA: { value: 1000, unit: 'kiloampere', symbol: 'kA' }
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
// Resistance / Conductance
|
|
156
|
+
resistance: {
|
|
157
|
+
ohm: { value: 1, unit: 'ohm' },
|
|
158
|
+
kohm: { value: 1000, unit: 'kiloohm'},
|
|
159
|
+
megaohm: { value: 1e6, unit: 'megaohm'},
|
|
160
|
+
S: { value: 1, unit: 'siemens', symbol: 'S', note: 'conductance' }
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
// Capacitance / Inductance
|
|
164
|
+
capacitance: {
|
|
165
|
+
F: { value: 1, unit: 'farad', symbol: 'F' },
|
|
166
|
+
mF: { value: 0.001, unit: 'millifarad'},
|
|
167
|
+
uF: { value: 0.000001, unit: 'microfarad' }
|
|
168
|
+
},
|
|
169
|
+
inductance: {
|
|
170
|
+
H: { value: 1, unit: 'henry', symbol: 'H' },
|
|
171
|
+
mH: { value: 0.001, unit: 'millihenry', symbol: 'mH' },
|
|
172
|
+
uH: { value: 0.000001, unit: 'microhenry', symbol: 'uH' }
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
// Luminous Intensity / Illuminance
|
|
176
|
+
light: {
|
|
177
|
+
cd: { value: 1, unit: 'candela', symbol: 'cd' },
|
|
178
|
+
lm: { value: 1, unit: 'lumen', symbol: 'lm' },
|
|
179
|
+
lx: { value: 1, unit: 'lux', symbol: 'lx' }
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
// Data / Digital Storage
|
|
183
|
+
data: {
|
|
184
|
+
bit: { value: 1, unit: 'bit', symbol: 'bit' },
|
|
185
|
+
B: { value: 8, unit: 'byte', symbol: 'B' },
|
|
186
|
+
KB: { value: 8e3, unit: 'kilobyte', symbol: 'KB' },
|
|
187
|
+
MB: { value: 8e6, unit: 'megabyte', symbol: 'MB' },
|
|
188
|
+
GB: { value: 8e9, unit: 'gigabyte', symbol: 'GB' },
|
|
189
|
+
TB: { value: 8e12, unit: 'terabyte', symbol: 'TB' }
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
// Angle
|
|
193
|
+
angle: {
|
|
194
|
+
deg: { value: 1, unit: 'degree', symbol: '°' },
|
|
195
|
+
rad: { value: 57.2958, unit: 'radian', symbol: 'rad', note: '1 rad = 57.2958°' },
|
|
196
|
+
grad:{ value: 0.9, unit: 'grad', symbol: 'grad', note: '1 grad = 0.9°' }
|
|
197
|
+
},
|
|
198
|
+
radiation: {
|
|
199
|
+
// Absorbed Dose
|
|
200
|
+
Gy: { value: 1, unit: 'gray', symbol: 'Gy', note: 'Absorbed dose: 1 Gy = 1 J/kg' },
|
|
201
|
+
mGy: { value: 0.001, unit: 'milligray', symbol: 'mGy' },
|
|
202
|
+
rad: { value: 0.01, unit: 'rad', symbol: 'rad', note: '1 rad = 0.01 Gy' },
|
|
203
|
+
|
|
204
|
+
// Dose Equivalent
|
|
205
|
+
Sv: { value: 1, unit: 'sievert', symbol: 'Sv', note: 'Biological effect dose equivalent' },
|
|
206
|
+
mSv: { value: 0.001, unit: 'millisievert', symbol: 'mSv' },
|
|
207
|
+
rem: { value: 0.01, unit: 'rem', symbol: 'rem', note: '1 rem = 0.01 Sv' },
|
|
208
|
+
|
|
209
|
+
// Radioactivity
|
|
210
|
+
Bq: { value: 1, unit: 'becquerel', symbol: 'Bq', note: '1 decay per second' },
|
|
211
|
+
kBq: { value: 1e3, unit: 'kilobecquerel', symbol: 'kBq' },
|
|
212
|
+
MBq: { value: 1e6, unit: 'megabecquerel', symbol: 'MBq' },
|
|
213
|
+
GBq: { value: 1e9, unit: 'gigabecquerel', symbol: 'GBq' },
|
|
214
|
+
Ci: { value: 3.7e10, unit: 'curie', symbol: 'Ci', note: '1 Ci = 3.7 x 10¹⁰ decays per second' },
|
|
215
|
+
mCi: { value: 3.7e7, unit: 'millicurie', symbol: 'mCi' }
|
|
216
|
+
}
|
|
217
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const isDenseMatrixWrapper = (value) =>
|
|
2
|
+
value &&
|
|
3
|
+
typeof value === "object" &&
|
|
4
|
+
value.exprify === "DenseMatrix" &&
|
|
5
|
+
"data" in value &&
|
|
6
|
+
"size" in value;
|
|
7
|
+
|
|
8
|
+
export const cloneMatrixData = (value) => {
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
return value.map(cloneMatrixData);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const getMatrixSize = (data) => {
|
|
17
|
+
if (Array.isArray(data) && data.every(Array.isArray)) {
|
|
18
|
+
return [data.length, data[0]?.length || 0];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (Array.isArray(data)) {
|
|
22
|
+
return [data.length];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
throw new Error("Matrix data must be an array");
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const wrapDenseMatrix = (data) => ({
|
|
29
|
+
exprify: "DenseMatrix",
|
|
30
|
+
data: cloneMatrixData(data),
|
|
31
|
+
size: getMatrixSize(data)
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const unwrapDenseMatrix = (value) =>
|
|
35
|
+
isDenseMatrixWrapper(value) ? cloneMatrixData(value.data) : value;
|
|
36
|
+
|
|
37
|
+
export const serializeExprifyValue = (value) => {
|
|
38
|
+
if (isDenseMatrixWrapper(value)) {
|
|
39
|
+
return JSON.stringify(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (Array.isArray(value) || (value && typeof value === "object")) {
|
|
43
|
+
return JSON.stringify(value, (_, current) => {
|
|
44
|
+
if (isDenseMatrixWrapper(current)) {
|
|
45
|
+
return current;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return current;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return value;
|
|
53
|
+
};
|