@rulecad/variables 0.3.1 → 0.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/variables",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
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.d.mts CHANGED
@@ -31,6 +31,28 @@ export function resolve(object: DesignObject, { sets, set, display, load }?: {
31
31
  entries: Entry[];
32
32
  deps: Record<string, string[]>;
33
33
  };
34
+ /**
35
+ * @param {RuleSet & { part?: string, interface?: Record<string, string> }} set
36
+ * @param {{ units: string[], prefixes: string[], fields: Record<string, { unit?: string, choice?: string[] }>, parts: Record<string, string[]> }} vocabulary
37
+ * @returns {{ level: 'fail', code: string, text: string, rule?: string }[]} what's wrong with it: nothing, if it passes
38
+ */
39
+ export function checkRuleSet(set: RuleSet & {
40
+ part?: string;
41
+ interface?: Record<string, string>;
42
+ }, vocabulary: {
43
+ units: string[];
44
+ prefixes: string[];
45
+ fields: Record<string, {
46
+ unit?: string;
47
+ choice?: string[];
48
+ }>;
49
+ parts: Record<string, string[]>;
50
+ }): {
51
+ level: "fail";
52
+ code: string;
53
+ text: string;
54
+ rule?: string;
55
+ }[];
34
56
  /**
35
57
  * Number literals in a design's code other than 0, 1 and 2 (outside comments and strings).
36
58
  * @param {string} source
@@ -43,7 +65,7 @@ export function lint(source: string): {
43
65
  export const ANCHORS: string[];
44
66
  export function isName(d: any): boolean;
45
67
  export function options(d: any): any;
46
- export function fmt(n: number | string): string;
68
+ export function fmt(n: any): string;
47
69
  export type Variable = {
48
70
  value: number | string;
49
71
  unit?: string;
package/variables.mjs CHANGED
@@ -196,6 +196,7 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
196
196
  // deps: each name's own references (what its expression reads), for explaining a value; stack: whose expression is being read
197
197
  const done = new Map(), busy = new Set(), deps = new Map(), stack = [];
198
198
  const pathOf = (name) => (name.includes('.') ? `rules.${name}.value` : `variables.${name}.value`);
199
+ const ruleOf = (name) => { let [t, r] = name.split('.'); if (!own(ruleSets, t)) t = t.replace(/_/g, '-'); return ruleSets[t]?.rules[r]; };
199
200
  const lookup = (name) => {
200
201
  if (stack.length) deps.get(stack[stack.length - 1]).add(name);
201
202
  if (done.has(name)) return done.get(name);
@@ -221,6 +222,8 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
221
222
  deps.set(name, new Set()); stack.push(name);
222
223
  try { v = evaluate(src, (n) => lookup(where(n))); } catch (e) {
223
224
  busy.delete(name); stack.pop();
225
+ // A rule with words for a value and no unit (a bearing's "radial", a pinout) is text, read as it is
226
+ if (name.includes('.') && typeof src === 'string' && !ruleOf(name)?.unit) { done.set(name, src); return src; }
224
227
  const out = e.message.startsWith('"') || e.message.startsWith('no ') ? e : fail(e.code || 'var.expression', `"${name}" = ${JSON.stringify(src)}: ${e.message}`);
225
228
  if (!out.path) out.path = pathOf(name); // the first expression that reads the bad name: where to fix it
226
229
  throw out;
@@ -260,6 +263,49 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
260
263
 
261
264
  // Round for display: at most 2 decimals, no trailing zeros.
262
265
  /** A number to two decimals at most; a name as it is. @param {number | string} n @returns {string} */
266
+ // A published rule set, held to what makes it traceable: every value says what it is, how far to trust it, where it
267
+ // comes from and when it was checked. A set with "part" (a motor) uses the shared names in vocabulary (the rule sets'
268
+ // vocabulary.json), every required one, each in its unit; an older set says which of its rules is which in "interface".
269
+ const KINDS = ['measured', 'spec', 'community', 'default', 'derived', 'estimate'];
270
+ /**
271
+ * @param {RuleSet & { part?: string, interface?: Record<string, string> }} set
272
+ * @param {{ units: string[], prefixes: string[], fields: Record<string, { unit?: string, choice?: string[] }>, parts: Record<string, string[]> }} vocabulary
273
+ * @returns {{ level: 'fail', code: string, text: string, rule?: string }[]} what's wrong with it: nothing, if it passes
274
+ */
275
+ export function checkRuleSet(set, vocabulary) {
276
+ const out = [], f = (code, text, rule) => out.push({ level: 'fail', code, text, ...(rule ? { rule } : {}) }), { units, prefixes, fields, parts } = vocabulary, rules = set?.rules;
277
+ if (!rules || typeof rules !== 'object' || !Object.keys(rules).length) { f('rules.shape', 'it has no rules'); return out; }
278
+ for (const [name, r] of Object.entries(rules)) {
279
+ if (!(typeof r.value === 'number' ? Number.isFinite(r.value) : typeof r.value === 'string' && r.value)) f('rules.value', `${name} has no value`, name);
280
+ if (!(typeof r.rule === 'string' && r.rule.length > 5)) f('rules.says', `${name} doesn't say what it is ("rule")`, name);
281
+ if (!KINDS.includes(r.kind)) f('rules.kind', `${name}: kind ${JSON.stringify(r.kind)} is not one of ${KINDS.join(', ')}`, name);
282
+ if (!(typeof r.source === 'string' && r.source)) f('rules.source', `${name} has no source`, name);
283
+ 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);
284
+ if (set.part) {
285
+ 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);
286
+ if (typeof r.value === 'number' && r.unit == null) f('rules.unit', `${name} is a number with no unit (give "unit", "" if it has none)`, name);
287
+ const field = fields[name];
288
+ if (prefixes.some((p) => name.startsWith(p)) && !field) f('rules.name', `${name} is not in the vocabulary (a name starting ${prefixes.join(', ')} says how a part mounts, so everything reads it the same way)`, name);
289
+ if (field?.unit != null && r.unit !== field.unit) f('rules.unit', `${name} is in ${JSON.stringify(field.unit)}, not ${JSON.stringify(r.unit ?? null)}`, name);
290
+ if (field?.choice && !field.choice.includes(r.value)) f('rules.choice', `${name} is one of ${field.choice.join(', ')} (got ${JSON.stringify(r.value)})`, name);
291
+ }
292
+ }
293
+ if (set.part) {
294
+ if (!parts[set.part]) f('rules.part', `part ${JSON.stringify(set.part)} is not one of ${Object.keys(parts).join(', ')}`);
295
+ else for (const name of parts[set.part]) if (!rules[name]) f('rules.missing', `a ${set.part} needs ${name}`, name);
296
+ }
297
+ for (const [shared, own] of Object.entries(set.interface || {})) {
298
+ if (!fields[shared]) f('rules.name', `interface: ${shared} is not in the vocabulary`, own);
299
+ if (!rules[own]) f('rules.interface', `interface: ${shared} names ${own}, which isn't one of its rules`, own);
300
+ }
301
+ for (const [name, v] of Object.entries(set.variants || {})) {
302
+ if (!KINDS.includes(v.kind)) f('rules.kind', `variant ${name}: kind ${JSON.stringify(v.kind)} is not one of ${KINDS.join(', ')}`);
303
+ if (!(typeof v.source === 'string' && v.source)) f('rules.source', `variant ${name} has no source`);
304
+ for (const rule of Object.keys(v.values || {})) if (!rules[rule]) f('rules.variant', `variant ${name} changes ${rule}, which isn't one of its rules`, rule);
305
+ }
306
+ return out;
307
+ }
308
+
263
309
  export const fmt = (n) => (typeof n === 'number' ? String(Number(n.toFixed(2))) : String(n)); // choices are names
264
310
 
265
311
  // lint(source) -> [{ line, text }]: number literals in a design's code that should be variables. Every measurement