@rulecad/variables 0.3.6 → 0.3.7

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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  What changed in each release, for someone upgrading a workspace.
4
4
 
5
+ ## 0.3.7: range ends in expressions
6
+ ### Added
7
+
8
+ - An expression reads either end of a rule's range: `pa_cf.strength.low`, `pa_cf.strength.high`, in the rule's unit.
9
+ An end the rule does not give is refused by name (`var.range`). Names with two dots that are not `low` or `high` are
10
+ refused as before. No value read by an existing name changes.
11
+
5
12
  ## 0.3.6: material ranges
6
13
  ### Added
7
14
 
package/README.md CHANGED
@@ -31,6 +31,10 @@ entries // every variable and rule with its value, source and whet
31
31
  - **Lists:** a rule may hold a list, `"value": [5, 6, 8, 10]` with one `unit` for all of it (the lengths a screw is
32
32
  sold in). Each entry is a number or an expression. A design reads it as an array; in an expression only `min()` and
33
33
  `max()` take a list, and anything else refuses it by name.
34
+ - **Ranges:** a rule may give the range it is found in, `"low"` and `"high"` beside its value, in its unit. An
35
+ expression reads either end as `<set>.<rule>.low` or `<set>.<rule>.high` (`pa_cf.strength.low`), and the plain name
36
+ still reads the value. Reading an end a rule does not give is refused by name (`var.range`), with the rules in that set
37
+ that do give one.
34
38
  - **Rule sets** are handed in through `sets`, or fetched by name with `load: (name) => …` (from files, a URL, anywhere).
35
39
  - **Tweaks:** only variables with a `tweak` range can be set, and only within it. Errors say which variable and why.
36
40
  - **Names, not numbers:** `choice: [...]`, `type: 'text'` or `type: 'anchor'` (one of `ANCHORS`: tl tc tr / ml mc
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/variables",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
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": {
package/variables.mjs CHANGED
@@ -207,11 +207,19 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
207
207
  busy.add(name);
208
208
  let src, where;
209
209
  if (name.includes('.')) {
210
- let [s, r, more] = name.split('.');
211
- if (more !== undefined) throw fail('var.unknown-rule', `"${name}": a rule is named <set>.<rule>, with one dot`);
210
+ let [s, r, end, more] = name.split('.');
211
+ if (more !== undefined || (end !== undefined && end !== 'low' && end !== 'high')) throw fail('var.unknown-rule', `"${name}": a rule is named <set>.<rule>, with one dot, and the ends of its range <set>.<rule>.low and <set>.<rule>.high`);
212
212
  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)
213
213
  if (!own(ruleSets, s)) throw fail('var.unknown-set', `"${name}": rule set "${s}" isn't in uses (${Object.keys(ruleSets).join(', ') || 'none'})`);
214
214
  if (!own(ruleSets[s].rules, r)) throw fail('var.unknown-rule', `"${name}": no rule "${r}" in ${s}`);
215
+ // An end of the range a value is found in (pa_cf.strength.low): a number beside the value, in the same unit
216
+ if (end !== undefined) {
217
+ const d = ruleSets[s].rules[r], ranged = Object.entries(ruleSets[s].rules).filter(([, x]) => typeof x[end] === 'number').map(([k]) => k);
218
+ busy.delete(name);
219
+ if (typeof d[end] !== 'number') throw fail('var.range', `"${name}": ${s}.${r} gives no ${end} (${ranged.length ? `the rules in ${s} with a range: ${ranged.join(', ')}` : `no rule in ${s} gives a range`}), so read its value, ${s}.${r}`, `rules.${s}.${r}`);
220
+ deps.set(name, new Set([`${s}.${r}`])); done.set(name, d[end]);
221
+ return d[end];
222
+ }
215
223
  src = ruleSets[s].rules[r].value; where = (n) => (n.includes('.') ? n : `${s}.${n}`); // a rule's names are its own set's
216
224
  } else {
217
225
  const d = own(vars, name);
@@ -252,7 +260,7 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
252
260
  for (const p of r.problems) units.push({ name, path: `variables.${name}.value`, message: `"${name}" = ${d.value}: ${p}` });
253
261
  if (said && r.value && !sameUnit(said, r.value)) units.push({ name, path: `variables.${name}.unit`, message: `"${name}" comes to ${r.unit}, but its unit says ${showUnit(said)}` });
254
262
  }
255
- const used = new Set(done.keys()); // rules the variables actually refer to (directly or through other rules)
263
+ const used = new Set([...done.keys(), ...[...deps].filter(([k]) => k.split('.').length === 3).flatMap(([, v]) => [...v])]); // rules the variables actually refer to (directly or through other rules)
256
264
  for (const [s, rs] of Object.entries(ruleSets)) {
257
265
  values[s] = {};
258
266
  for (const [r, d] of Object.entries(rs.rules)) {