@rulecad/variables 0.2.3 → 0.3.0
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/package.json +5 -1
- package/variables.mjs +38 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/variables",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Design values you can trace: every number says where it comes from, and expressions are parsed, never run.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -30,6 +30,10 @@
|
|
|
30
30
|
"url": "git+https://github.com/JoniBach/rulecad.git",
|
|
31
31
|
"directory": "packages/variables"
|
|
32
32
|
},
|
|
33
|
+
"homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/variables#readme",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/JoniBach/rulecad/issues"
|
|
36
|
+
},
|
|
33
37
|
"scripts": {
|
|
34
38
|
"test": "node --test"
|
|
35
39
|
},
|
package/variables.mjs
CHANGED
|
@@ -94,7 +94,7 @@ function parseUnit(u) {
|
|
|
94
94
|
}
|
|
95
95
|
const showUnit = (x) => (x == null ? 'a number' : Object.keys(x).length ? Object.entries(x).map(([k, n]) => (n === 1 ? k : n === 2 ? `${k}²` : n === 3 ? `${k}³` : `${k}^${n}`)).join('·') : 'a plain number');
|
|
96
96
|
const sameUnit = (a, b) => a == null || b == null || (Object.keys(a).length === Object.keys(b).length && Object.entries(a).every(([k, n]) => b[k] === n));
|
|
97
|
-
const combine = (a, b, sign) => { if (a == null) return sign > 0 ? b : null; if (b == null) return a; const o = { ...a }; for (const [k, n] of Object.entries(b)) { o[k] = (o[k] || 0) + sign * n; if (!o[k]) delete o[k]; } return o; };
|
|
97
|
+
const combine = (a, b, sign) => { if (a == null) return sign > 0 ? b : b == null ? null : Object.fromEntries(Object.entries(b).map(([k, n]) => [k, -n])); /* (a plain number over a unit: 2 / w is per mm) */ if (b == null) return a; const o = { ...a }; for (const [k, n] of Object.entries(b)) { o[k] = (o[k] || 0) + sign * n; if (!o[k]) delete o[k]; } return o; };
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* The unit an expression comes to, and any mixing of units on the way (mm added to degrees, a sine of millimetres).
|
|
@@ -251,11 +251,42 @@ export const fmt = (n) => (typeof n === 'number' ? String(Number(n.toFixed(2)))
|
|
|
251
251
|
* @returns {{ line: number, text: string }[]}
|
|
252
252
|
*/
|
|
253
253
|
export function lint(source) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const found = []
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
254
|
+
// A small tokeniser, so a // inside a string doesn't hide the rest of the line, code inside a template's ${…} is read
|
|
255
|
+
// as code, and every way of writing a number is seen (1e3, 0x1F, 1_000, .5)
|
|
256
|
+
const found = [], n = source.length, NUM = /^(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?)n?/;
|
|
257
|
+
let line = 1, i = 0;
|
|
258
|
+
const depth = []; // open template literals: the brace depth their ${ started at
|
|
259
|
+
let braces = 0;
|
|
260
|
+
const skipString = (q) => { i++; while (i < n && source[i] !== q) { if (source[i] === '\\') i++; else if (source[i] === '\n') line++; i++; } i++; };
|
|
261
|
+
const template = () => { // from just after a backtick (or a closing } of ${…}) to the next ${ or the closing backtick
|
|
262
|
+
while (i < n) {
|
|
263
|
+
const c = source[i];
|
|
264
|
+
if (c === '\\') { i += 2; continue; }
|
|
265
|
+
if (c === '\n') line++;
|
|
266
|
+
if (c === '`') { i++; return; }
|
|
267
|
+
if (c === '$' && source[i + 1] === '{') { i += 2; depth.push(braces); braces++; return; }
|
|
268
|
+
i++;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
while (i < n) {
|
|
272
|
+
const c = source[i];
|
|
273
|
+
if (c === '\n') { line++; i++; continue; }
|
|
274
|
+
if (c === '/' && source[i + 1] === '/') { while (i < n && source[i] !== '\n') i++; continue; }
|
|
275
|
+
if (c === '/' && source[i + 1] === '*') { const end = source.indexOf('*/', i + 2), stop = end < 0 ? n : end + 2; for (let k = i; k < stop; k++) if (source[k] === '\n') line++; i = stop; continue; }
|
|
276
|
+
if (c === '"' || c === "'") { skipString(c); continue; }
|
|
277
|
+
if (c === '`') { i++; template(); continue; }
|
|
278
|
+
if (c === '{') { braces++; i++; continue; }
|
|
279
|
+
if (c === '}') { braces--; i++; if (depth.length && braces === depth.at(-1)) { depth.pop(); template(); } continue; }
|
|
280
|
+
if (/[\w$]/.test(c) && !/\d/.test(c)) { while (i < n && /[\w$]/.test(source[i])) i++; continue; } // a name (x1, v2: not numbers)
|
|
281
|
+
if (/\d/.test(c) || (c === '.' && /\d/.test(source[i + 1] || '') && !/[\w$)\]]/.test(source[i - 1] || ''))) {
|
|
282
|
+
if (c === '.' && /[\w$)\].]/.test(source[i - 1] || '')) { i++; continue; } // (a property after a dot)
|
|
283
|
+
const m = source.slice(i).match(NUM), text = m[0];
|
|
284
|
+
const value = Number(text.replace(/_/g, '').replace(/n$/, ''));
|
|
285
|
+
if (!(value === 0 || value === 1 || value === 2)) found.push({ line, text });
|
|
286
|
+
i += text.length;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
i++;
|
|
290
|
+
}
|
|
260
291
|
return found;
|
|
261
292
|
}
|