@rulecad/variables 0.3.1 → 0.3.2
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 +23 -1
- package/variables.mjs +43 -0
package/package.json
CHANGED
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:
|
|
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
|
@@ -260,6 +260,49 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
260
260
|
|
|
261
261
|
// Round for display: at most 2 decimals, no trailing zeros.
|
|
262
262
|
/** A number to two decimals at most; a name as it is. @param {number | string} n @returns {string} */
|
|
263
|
+
// A published rule set, held to what makes it traceable: every value says what it is, how far to trust it, where it
|
|
264
|
+
// comes from and when it was checked. A set with "part" (a motor) uses the shared names in vocabulary (the rule sets'
|
|
265
|
+
// vocabulary.json), every required one, each in its unit; an older set says which of its rules is which in "interface".
|
|
266
|
+
const KINDS = ['measured', 'spec', 'community', 'default', 'derived', 'estimate'];
|
|
267
|
+
/**
|
|
268
|
+
* @param {RuleSet & { part?: string, interface?: Record<string, string> }} set
|
|
269
|
+
* @param {{ units: string[], prefixes: string[], fields: Record<string, { unit?: string, choice?: string[] }>, parts: Record<string, string[]> }} vocabulary
|
|
270
|
+
* @returns {{ level: 'fail', code: string, text: string, rule?: string }[]} what's wrong with it: nothing, if it passes
|
|
271
|
+
*/
|
|
272
|
+
export function checkRuleSet(set, vocabulary) {
|
|
273
|
+
const out = [], f = (code, text, rule) => out.push({ level: 'fail', code, text, ...(rule ? { rule } : {}) }), { units, prefixes, fields, parts } = vocabulary, rules = set?.rules;
|
|
274
|
+
if (!rules || typeof rules !== 'object' || !Object.keys(rules).length) { f('rules.shape', 'it has no rules'); return out; }
|
|
275
|
+
for (const [name, r] of Object.entries(rules)) {
|
|
276
|
+
if (!(typeof r.value === 'number' ? Number.isFinite(r.value) : typeof r.value === 'string' && r.value)) f('rules.value', `${name} has no value`, name);
|
|
277
|
+
if (!(typeof r.rule === 'string' && r.rule.length > 5)) f('rules.says', `${name} doesn't say what it is ("rule")`, name);
|
|
278
|
+
if (!KINDS.includes(r.kind)) f('rules.kind', `${name}: kind ${JSON.stringify(r.kind)} is not one of ${KINDS.join(', ')}`, name);
|
|
279
|
+
if (!(typeof r.source === 'string' && r.source)) f('rules.source', `${name} has no source`, name);
|
|
280
|
+
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);
|
|
281
|
+
if (set.part) {
|
|
282
|
+
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);
|
|
283
|
+
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);
|
|
284
|
+
const field = fields[name];
|
|
285
|
+
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);
|
|
286
|
+
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);
|
|
287
|
+
if (field?.choice && !field.choice.includes(r.value)) f('rules.choice', `${name} is one of ${field.choice.join(', ')} (got ${JSON.stringify(r.value)})`, name);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (set.part) {
|
|
291
|
+
if (!parts[set.part]) f('rules.part', `part ${JSON.stringify(set.part)} is not one of ${Object.keys(parts).join(', ')}`);
|
|
292
|
+
else for (const name of parts[set.part]) if (!rules[name]) f('rules.missing', `a ${set.part} needs ${name}`, name);
|
|
293
|
+
}
|
|
294
|
+
for (const [shared, own] of Object.entries(set.interface || {})) {
|
|
295
|
+
if (!fields[shared]) f('rules.name', `interface: ${shared} is not in the vocabulary`, own);
|
|
296
|
+
if (!rules[own]) f('rules.interface', `interface: ${shared} names ${own}, which isn't one of its rules`, own);
|
|
297
|
+
}
|
|
298
|
+
for (const [name, v] of Object.entries(set.variants || {})) {
|
|
299
|
+
if (!KINDS.includes(v.kind)) f('rules.kind', `variant ${name}: kind ${JSON.stringify(v.kind)} is not one of ${KINDS.join(', ')}`);
|
|
300
|
+
if (!(typeof v.source === 'string' && v.source)) f('rules.source', `variant ${name} has no source`);
|
|
301
|
+
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);
|
|
302
|
+
}
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
|
|
263
306
|
export const fmt = (n) => (typeof n === 'number' ? String(Number(n.toFixed(2))) : String(n)); // choices are names
|
|
264
307
|
|
|
265
308
|
// lint(source) -> [{ line, text }]: number literals in a design's code that should be variables. Every measurement
|