@rulecad/variables 0.1.7 → 0.2.3
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 +1 -1
- package/variables.d.mts +2 -2
- package/variables.mjs +30 -16
package/package.json
CHANGED
package/variables.d.mts
CHANGED
|
@@ -5,7 +5,7 @@ export function checkName(name: any, d: any, v: any): string | number;
|
|
|
5
5
|
* @param {(name: string) => number} lookup the value of a name
|
|
6
6
|
* @returns {number}
|
|
7
7
|
*/
|
|
8
|
-
export function evaluate(src: string | number, lookup
|
|
8
|
+
export function evaluate(src: string | number, lookup?: (name: string) => number): number;
|
|
9
9
|
/**
|
|
10
10
|
* The unit an expression comes to, and any mixing of units on the way (mm added to degrees, a sine of millimetres).
|
|
11
11
|
* @param {string | number} src @param {(name: string) => string | undefined} unitOf a name's unit
|
|
@@ -60,7 +60,7 @@ export type Variable = {
|
|
|
60
60
|
max: number;
|
|
61
61
|
};
|
|
62
62
|
choice?: string[];
|
|
63
|
-
type?: "text" | "anchor" | "layout";
|
|
63
|
+
type?: "text" | "anchor" | "layout" | "problem";
|
|
64
64
|
};
|
|
65
65
|
export type RuleSet = {
|
|
66
66
|
name?: string;
|
package/variables.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// No imports and no file access: it runs in a browser as well. Rule sets are handed in (sets) or fetched by name
|
|
8
8
|
// through load(name), such as one that reads rules/<name>.json.
|
|
9
9
|
|
|
10
|
-
/** @typedef {{ value: number | string, unit?: string, rule?: string, kind?: 'spec' | 'community' | 'default' | 'estimate' | 'measured' | 'derived', source?: string, licence?: string, checked?: string, label?: string, show?: boolean, interface?: boolean, tweak?: { min: number, max: number }, choice?: string[], type?: 'text' | 'anchor' | 'layout' }} Variable */
|
|
10
|
+
/** @typedef {{ value: number | string, unit?: string, rule?: string, kind?: 'spec' | 'community' | 'default' | 'estimate' | 'measured' | 'derived', source?: string, licence?: string, checked?: string, label?: string, show?: boolean, interface?: boolean, tweak?: { min: number, max: number }, choice?: string[], type?: 'text' | 'anchor' | 'layout' | 'problem' }} Variable */
|
|
11
11
|
/** @typedef {{ name?: string, rules: Record<string, Variable> }} RuleSet */
|
|
12
12
|
/** @typedef {{ uses?: string[], variables?: Record<string, Variable>, [field: string]: any }} DesignObject */
|
|
13
13
|
/** @typedef {{ name: string, value: number | string, used?: boolean, set?: boolean, [field: string]: any }} Entry */
|
|
@@ -17,7 +17,7 @@ const fail = (code, message, path) => Object.assign(new Error(message), { code,
|
|
|
17
17
|
|
|
18
18
|
export const ANCHORS = ['tl', 'tc', 'tr', 'ml', 'mc', 'mr', 'bl', 'bc', 'br'];
|
|
19
19
|
// A variable that holds a name or text, and the names it may hold (null: any text)
|
|
20
|
-
export const isName = (d) => !!(d.choice || ['text', 'anchor', 'layout'].includes(d.type));
|
|
20
|
+
export const isName = (d) => !!(d.choice || ['text', 'anchor', 'layout', 'problem'].includes(d.type));
|
|
21
21
|
export const options = (d) => (d.type === 'anchor' ? ANCHORS : d.type === 'text' ? null : d.choice);
|
|
22
22
|
export function checkName(name, d, v) {
|
|
23
23
|
const s = String(v), opts = options(d), match = opts?.find((o) => String(o) === s);
|
|
@@ -49,8 +49,8 @@ function tokens(src) {
|
|
|
49
49
|
* @param {(name: string) => number} lookup the value of a name
|
|
50
50
|
* @returns {number}
|
|
51
51
|
*/
|
|
52
|
-
export function evaluate(src, lookup) {
|
|
53
|
-
if (typeof src === 'number') return src;
|
|
52
|
+
export function evaluate(src, lookup = (name) => { throw new Error(`no value for "${name}"`); }) {
|
|
53
|
+
if (typeof src === 'number') { if (!Number.isFinite(src)) throw new Error('is not a finite number'); return src; }
|
|
54
54
|
const t = tokens(String(src));
|
|
55
55
|
let i = 0;
|
|
56
56
|
const peek = () => t[i], take = (op) => (t[i]?.op === op ? (i++, true) : false);
|
|
@@ -62,7 +62,7 @@ export function evaluate(src, lookup) {
|
|
|
62
62
|
if (!k) throw new Error('ends too soon');
|
|
63
63
|
if (k.n != null) return k.n;
|
|
64
64
|
if (k.op === '(') { const v = expr(); if (!take(')')) throw new Error('missing )'); return v; }
|
|
65
|
-
if (k.id && FNS
|
|
65
|
+
if (k.id && Object.hasOwn(FNS, k.id) && take('(')) {
|
|
66
66
|
const args = [expr()];
|
|
67
67
|
while (take(',')) args.push(expr());
|
|
68
68
|
if (!take(')')) throw new Error('missing )');
|
|
@@ -115,7 +115,7 @@ export function unitsOf(src, unitOf) {
|
|
|
115
115
|
if (!k) return null;
|
|
116
116
|
if (k.n != null) return null;
|
|
117
117
|
if (k.op === '(') { const v = expr(); take(')'); return v; }
|
|
118
|
-
if (k.id && FNS
|
|
118
|
+
if (k.id && Object.hasOwn(FNS, k.id) && take('(')) {
|
|
119
119
|
const args = [expr()];
|
|
120
120
|
while (take(',')) args.push(expr());
|
|
121
121
|
take(')');
|
|
@@ -145,6 +145,7 @@ export function unitsOf(src, unitOf) {
|
|
|
145
145
|
// { "type": "text", "value": "", "max": 8 } free text, e.g. a keycap's legend
|
|
146
146
|
// { "type": "anchor", "value": "mc" } one of 9 places on a face: tl tc tr, ml mc mr, bl bc br
|
|
147
147
|
// { "type": "layout", "value": "grid", "choice": [...] } a layout file in the project's layouts/ (or a built-in)
|
|
148
|
+
// { "type": "problem", "value": "lever" } an optimisation problem in the project's problems/
|
|
148
149
|
/**
|
|
149
150
|
* Every variable's value, and an entry for each variable and rule for checks and rule cards.
|
|
150
151
|
* @param {DesignObject} object
|
|
@@ -152,14 +153,20 @@ export function unitsOf(src, unitOf) {
|
|
|
152
153
|
* @returns {{ values: Record<string, any>, entries: Entry[], deps: Record<string, string[]> }} deps: each name with the names its own expression reads
|
|
153
154
|
*/
|
|
154
155
|
export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}) {
|
|
156
|
+
// Names are looked up as an object's own, never inherited: a variable called constructor is a variable, and a
|
|
157
|
+
// name nobody defined (toString) is unknown, not whatever every object happens to carry
|
|
158
|
+
const own = (o, k) => (o != null && Object.hasOwn(o, k) ? o[k] : undefined);
|
|
155
159
|
const ruleSets = Object.fromEntries((object.uses || []).map((u) => {
|
|
156
|
-
const s = sets
|
|
160
|
+
const s = own(sets, u) || (load ? load(u) : null);
|
|
157
161
|
if (!s) throw fail('rules.missing', `no rule set "${u}" (hand it in as sets.${u}, or give a load function)`, 'uses');
|
|
162
|
+
if (typeof s.then === 'function') throw fail('rules.async', `the load function gave a promise for "${u}": load must return the rule set itself (fetch it first, and hand it in as sets.${u})`, 'uses');
|
|
163
|
+
if (!s.rules || typeof s.rules !== 'object') throw fail('rules.shape', `rule set "${u}" has no "rules"`, 'uses');
|
|
158
164
|
return [s.name || u, s];
|
|
159
165
|
}));
|
|
160
166
|
const vars = object.variables || {};
|
|
167
|
+
for (const name of Object.keys(ruleSets)) if (own(vars, name) || own(vars, name.replace(/-/g, '_'))) throw fail('var.clash', `"${name}" is a rule set in uses and a variable too: rename the variable, so each name means one thing`, `variables.${own(vars, name) ? name : name.replace(/-/g, '_')}`);
|
|
161
168
|
for (const [k, v] of Object.entries(set)) {
|
|
162
|
-
const d = vars
|
|
169
|
+
const d = own(vars, k);
|
|
163
170
|
if (!d) throw fail('set.unknown', `no variable "${k}" to set (variables: ${Object.keys(vars).join(', ')})`, `variables.${k}`);
|
|
164
171
|
if (isName(d)) { checkName(k, d, v); continue; }
|
|
165
172
|
if (!d.tweak) throw fail('set.fixed', `"${k}" is fixed by its rule; only variables with a tweak range can be set`, `variables.${k}.tweak`);
|
|
@@ -177,15 +184,19 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
177
184
|
busy.add(name);
|
|
178
185
|
let src, where;
|
|
179
186
|
if (name.includes('.')) {
|
|
180
|
-
let [s, r] = name.split('.');
|
|
181
|
-
if (
|
|
182
|
-
if (!ruleSets
|
|
183
|
-
if (!ruleSets
|
|
187
|
+
let [s, r, more] = name.split('.');
|
|
188
|
+
if (more !== undefined) throw fail('var.unknown-rule', `"${name}": a rule is named <set>.<rule>, with one dot`);
|
|
189
|
+
if (!own(ruleSets, s) && own(ruleSets, s.replace(/_/g, '-'))) s = s.replace(/_/g, '-'); // mx_switch.housing_top reads the set named mx-switch (a - is a minus here)
|
|
190
|
+
if (!own(ruleSets, s)) throw fail('var.unknown-set', `"${name}": rule set "${s}" isn't in uses (${Object.keys(ruleSets).join(', ') || 'none'})`);
|
|
191
|
+
if (!own(ruleSets[s].rules, r)) throw fail('var.unknown-rule', `"${name}": no rule "${r}" in ${s}`);
|
|
184
192
|
src = ruleSets[s].rules[r].value; where = (n) => (n.includes('.') ? n : `${s}.${n}`); // a rule's names are its own set's
|
|
185
193
|
} else {
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
|
|
194
|
+
const d = own(vars, name);
|
|
195
|
+
if (!d) throw fail('var.unknown', `no variable "${name}"`);
|
|
196
|
+
const shown = Object.hasOwn(display, name), given = Object.hasOwn(set, name);
|
|
197
|
+
if (isName(d)) { busy.delete(name); const c = checkName(name, d, shown ? display[name] : given ? set[name] : d.value); done.set(name, c); return c; }
|
|
198
|
+
src = shown ? display[name] : given ? Number(set[name]) : d.value; where = (n) => n; // display: the screen's settings win
|
|
199
|
+
if (src === undefined) throw fail('var.value', `"${name}" has no value`, `variables.${name}`);
|
|
189
200
|
}
|
|
190
201
|
let v;
|
|
191
202
|
deps.set(name, new Set()); stack.push(name);
|
|
@@ -202,7 +213,7 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
202
213
|
const values = {}, entries = [];
|
|
203
214
|
for (const [name, d] of Object.entries(vars)) {
|
|
204
215
|
values[name] = lookup(name);
|
|
205
|
-
entries.push({ name, ...d, value: values[name], expr: typeof d.value === 'string' ? d.value : undefined, set: name
|
|
216
|
+
entries.push({ name, ...d, value: values[name], expr: typeof d.value === 'string' ? d.value : undefined, set: Object.hasOwn(set, name) });
|
|
206
217
|
}
|
|
207
218
|
// Units: each variable's expression, its names' units mixed only where they agree, and coming to the unit it says
|
|
208
219
|
const unitOf = (n) => (n.includes('.') ? ruleSets[n.split('.')[0].replace(/_/g, '-')]?.rules[n.split('.')[1]]?.unit ?? ruleSets[n.split('.')[0]]?.rules[n.split('.')[1]]?.unit : vars[n]?.unit);
|
|
@@ -221,6 +232,9 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
221
232
|
entries.push({ name: `${s}.${r}`, ...d, value: values[s][r], expr: typeof d.value === 'string' ? d.value : undefined, used: used.has(`${s}.${r}`), ...(rs.private ? { private: true } : {}) });
|
|
222
233
|
}
|
|
223
234
|
if (rs.footprints) values[s].footprints = rs.footprints; // pad data for boards (v.esp32c6.footprints.module), as the set gives it
|
|
235
|
+
if (rs.circuit) values[s].circuit = rs.circuit;
|
|
236
|
+
const pinMaps = Object.entries(rs.rules).filter(([, d]) => d.pins); // rules' pin maps (package pin: name), as v.rp2040.pinMaps.gpio
|
|
237
|
+
if (pinMaps.length) values[s].pinMaps = Object.fromEntries(pinMaps.map(([r, d]) => [r, d.pins])); // (camel case: no rule is named so) // a whole board's circuit, from cad clone (v.sea_picro.circuit), for @rulecad/pcb's clone()
|
|
224
238
|
}
|
|
225
239
|
return { values, entries, deps: Object.fromEntries([...deps].map(([k, v]) => [k, [...v]])), units };
|
|
226
240
|
}
|