@rulecad/variables 0.3.5 → 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,18 @@
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
+
12
+ ## 0.3.6: material ranges
13
+ ### Added
14
+
15
+ - `checkRuleSet` holds a rule's range to its value: `low` at most it, `high` at least it, both numbers (`rules.range`).
16
+
5
17
  ## 0.3.5: points in a rule set
6
18
 
7
19
  ### Added
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.5",
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)) {
@@ -276,6 +284,8 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
276
284
  // { "ascending": ["tap_drill", "major_diameter", "clearance"], "says": "…" } the names it has, each larger than the last
277
285
  // { "near": "tap_drill", "to": "major_diameter - pitch", "within": "0.2 * pitch", "says": "…" } expressions over its own rules
278
286
  // and a field marked "list": true holds a list (the lengths a screw is sold in), which must ascend.
287
+ // A number may carry the range it is found in, "low" and "high" (a printed plastic's, which varies with the printer and
288
+ // the spool), each a number in its unit, low at most the value and high at least it.
279
289
  const KINDS = ['measured', 'spec', 'community', 'default', 'derived', 'estimate'];
280
290
  const finite = (x) => typeof x === 'number' && Number.isFinite(x);
281
291
  /**
@@ -294,6 +304,11 @@ export function checkRuleSet(set, vocabulary) {
294
304
  if (!(typeof r.rule === 'string' && r.rule.length > 5)) f('rules.says', `${name} doesn't say what it is ("rule")`, name);
295
305
  if (!KINDS.includes(r.kind)) f('rules.kind', `${name}: kind ${JSON.stringify(r.kind)} is not one of ${KINDS.join(', ')}`, name);
296
306
  if (!(typeof r.source === 'string' && r.source)) f('rules.source', `${name} has no source`, name);
307
+ for (const [k, ok] of [['low', (v) => v <= r.value], ['high', (v) => v >= r.value]]) {
308
+ if (r[k] == null) continue;
309
+ if (!finite(r[k]) || !finite(r.value)) f('rules.range', `${name}: ${k} is a number in the rule's unit, beside a number value (got ${JSON.stringify(r[k])})`, name);
310
+ else if (!ok(r[k])) f('rules.range', `${name}: ${k} (${r[k]}) is ${k === 'low' ? 'above' : 'below'} its value (${r.value}): the range it is found in holds the value`, name, { value: r.value, [k]: r[k] });
311
+ }
297
312
  if (!/^\d{4}-\d{2}-\d{2}$/.test(String(r.checked))) f('rules.checked', `${name}: checked is the date it was last checked, YYYY-MM-DD (got ${JSON.stringify(r.checked)})`, name);
298
313
  if (set.part) {
299
314
  if (r.unit != null && !units.includes(r.unit)) f('rules.unit', `${name}: unit ${JSON.stringify(r.unit)} is not one of ${units.filter(Boolean).join(', ')}`, name);