@umriss-ui/calculation 0.41.1 → 0.42.0

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.
@@ -0,0 +1,168 @@
1
+ import { Children as ie, isValidElement as b, Fragment as oe } from "react";
2
+ const d = (o) => Object.defineProperty(() => {
3
+ throw new Error(
4
+ `@umriss-ui/calculation: <${o}> stands only directly inside a <Calculation>. A tree written in a Server Component must be written inside the <Calculation> there, in one file, not passed through a client component.`
5
+ );
6
+ }, "name", { value: o }), ae = d("Given"), se = d("Sum"), re = d("Difference"), ce = d("Product"), ue = d("Quotient"), J = d("Ref"), le = d("Chain"), fe = d("Plus"), de = d("Minus"), pe = d("Times"), me = d("DividedBy"), x = d("Interim"), he = /* @__PURE__ */ new Map([
7
+ [se, "sum"],
8
+ [re, "difference"],
9
+ [ce, "product"],
10
+ [ue, "quotient"]
11
+ ]), U = {
12
+ sum: "Sum",
13
+ difference: "Difference",
14
+ product: "Product",
15
+ quotient: "Quotient"
16
+ }, Q = /* @__PURE__ */ new Map([
17
+ [fe, { name: "Plus", operator: "sum" }],
18
+ [de, { name: "Minus", operator: "sum", negated: !0 }],
19
+ [pe, { name: "Times", operator: "product" }],
20
+ [me, { name: "DividedBy", operator: "quotient" }]
21
+ ]), B = "<Given>, <Sum>, <Difference>, <Product>, <Quotient> or <Chain>", Y = "a calculation with metrics only adds and subtracts - <Sum>, <Difference>, <Plus>, <Minus>.", $e = ["unit", "format", "decimals"];
22
+ function a(o) {
23
+ throw new Error(`@umriss-ui/calculation: ${o}`);
24
+ }
25
+ function S(o) {
26
+ return ie.toArray(o).flatMap(
27
+ (n) => b(n) && n.type === oe ? S(n.props.children) : [n]
28
+ );
29
+ }
30
+ function E(o) {
31
+ if (!b(o)) return JSON.stringify(o);
32
+ const n = o.type;
33
+ return typeof n == "string" ? `<${n}>` : `<${n.displayName ?? n.name ?? "anonymous"}>`;
34
+ }
35
+ const G = (o, n, f) => `${o}/${b(n) && n.key || f}`;
36
+ function ge(o, n) {
37
+ const f = S(o);
38
+ if (f.length !== 1 && a(`<Calculation> takes exactly one child, the result; it was given ${f.length}.`), n !== void 0) {
39
+ n.length === 0 && a("<Calculation>: metrics is empty; leave it out for a calculation with one number per quantity.");
40
+ const t = /* @__PURE__ */ new Set();
41
+ for (const { id: i } of n)
42
+ t.has(i) && a(`<Calculation>: the metric id "${i}" is used twice.`), t.add(i);
43
+ }
44
+ const y = n?.map((t) => `"${t.id}"`).join(", "), r = (t, i) => {
45
+ const s = typeof t == "object" && t !== null;
46
+ if (n === void 0) {
47
+ s && a(`${i}: value is an object by metric, but the calculation has no metrics.`);
48
+ return;
49
+ }
50
+ s || a(`${i}: with metrics, value is an object with a number for each metric - ${y}.`);
51
+ for (const { id: e } of n)
52
+ e in t || a(`${i}: value has no number for the metric "${e}"; write null where it is absent. The metrics: ${y}.`);
53
+ for (const e of Object.keys(t))
54
+ n.some((c) => c.id === e) || a(`${i}: value has a number for "${e}", which is no metric. The metrics: ${y}.`);
55
+ }, m = /* @__PURE__ */ new Map(), v = /* @__PURE__ */ new Map(), P = [], R = (t, i, s) => {
56
+ const { id: e, ...c } = i;
57
+ if (n !== void 0) {
58
+ for (const w of $e)
59
+ i[w] !== void 0 && a(`${s}: ${w} belongs to the metric once the calculation has metrics, not to a quantity.`);
60
+ (i.target !== void 0 || i.limits !== void 0) && a(`${s}: target and limits are not assessed in a calculation with metrics.`);
61
+ }
62
+ e !== void 0 && (v.has(e) && a(`${s}: the id "${e}" is used twice.`), v.set(e, t));
63
+ const h = { ...c, key: t, id: e, operands: [] };
64
+ return m.set(t, h), h;
65
+ }, N = (t, i, s) => {
66
+ if (b(t) && t.type === J) {
67
+ const e = { key: "", reference: !0 };
68
+ return P.push({ operand: e, to: t.props.to, where: s }), e;
69
+ }
70
+ return { key: L(t, i, s), reference: !1 };
71
+ }, L = (t, i, s) => {
72
+ if ((!b(t) || t.type === J || Q.has(t.type) || t.type === x) && a(`${s}: ${E(t)} cannot stand here; a quantity must be ${B}.`), t.type === le) return z(t.props.children, i, s);
73
+ const e = he.get(t.type);
74
+ t.type !== ae && e === void 0 && a(
75
+ `${s}: ${E(t)} is not an element of the calculation. A calculation reads its children's props and cannot look inside a component of your own; write ${B} or <Ref> directly, or return them from .map.`
76
+ );
77
+ const { children: c, value: h, source: w, asOf: I, ages: p, ...q } = t.props, u = `${s} › ${q.label}`;
78
+ n !== void 0 && (e === "product" || e === "quotient") && a(`${u}: <${U[e]}> - ${Y}`);
79
+ const C = R(i, q, u);
80
+ if (e === void 0)
81
+ return r(h, u), C.given = { value: h, source: w, asOf: I, ages: p }, i;
82
+ C.operator = e;
83
+ const $ = S(c), l = $.length;
84
+ return (e === "quotient" ? l !== 2 : e !== "sum" && l < 2) && a(
85
+ `${u}: <${U[e]}> takes ${e === "quotient" ? "exactly two operands" : "two or more operands"}; it was given ${l}.`
86
+ ), C.operands = $.map((O, T) => N(O, G(i, O, T), u)), i;
87
+ }, z = (t, i, s) => {
88
+ const e = `${s} › <Chain>`, [c, ...h] = S(t);
89
+ (c === void 0 || b(c) && (Q.has(c.type) || c.type === x)) && a(`${e}: a chain starts with a quantity that has no operator - ${B} or <Ref>.`);
90
+ let w = N(c, G(i, c, 0), e), I = E(c), p = [], q;
91
+ return h.forEach((u, C) => {
92
+ const $ = G(i, u, C + 1);
93
+ if (b(u) && u.type === x) {
94
+ const g = u.props, D = `${e} › ${g.label}`;
95
+ p.length === 0 && a(`${D}: an <Interim> needs an operand since the named value before it, ${I}.`);
96
+ const A = R($, g, D);
97
+ A.interim = !0, A.operator = p[0].line.operator, A.operands = [w, ...p.map((ne) => ne.operand)], w = { key: $, reference: !1, previous: !0 }, I = `<Interim label="${g.label}">`, p = [], q = $;
98
+ return;
99
+ }
100
+ const l = b(u) ? Q.get(u.type) : void 0;
101
+ l === void 0 && a(`${e}: ${E(u)} needs an operator in a chain - <Plus>, <Minus>, <Times> or <DividedBy> - or is an <Interim>.`), n !== void 0 && l.operator !== "sum" && a(`${e}: <${l.name}> - ${Y}`);
102
+ const O = l.operator !== "sum", T = p.find((g) => g.line.operator !== "sum");
103
+ (O && p.length > 0 || T) && a(
104
+ `${e}: <${(T ?? { line: l }).line.name}> stands alone between two named values - directly after the first quantity or an <Interim>, and directly before an <Interim>.`
105
+ );
106
+ const { children: H, ...M } = u.props, k = S(H), K = () => a(`${e}: <${l.name}> takes either a label and a value, or exactly one quantity as its child.`);
107
+ if (k.length > 0) {
108
+ (k.length > 1 || Object.values(M).some((D) => D !== void 0)) && K();
109
+ const g = N(k[0], $, e);
110
+ g.negated = l.negated, p.push({ operand: g, line: l });
111
+ return;
112
+ }
113
+ M.label === void 0 && K();
114
+ const { value: V, source: W, asOf: X, ages: Z, ...ee } = M, F = `${e} › ${M.label}`, te = R($, { ...ee, label: M.label }, F);
115
+ r(V, F), te.given = { value: V, source: W, asOf: X, ages: Z }, p.push({ operand: { key: $, reference: !1, negated: l.negated }, line: l });
116
+ }), (p.length > 0 || q === void 0) && a(`${e}: a chain ends with an <Interim>, which names its value.`), q;
117
+ }, _ = L(f[0], "0", "<Calculation>"), j = m.get(_);
118
+ (j.emphasis !== void 0 || j.rule !== void 0) && a(`<Calculation> › ${j.label}: emphasis and rule do not stand on the Result, which is already the heaviest line.`);
119
+ for (const { operand: t, to: i, where: s } of P) {
120
+ const e = v.get(i);
121
+ if (e === void 0) {
122
+ const c = [...v.keys()].map((h) => `"${h}"`).join(", ");
123
+ a(`${s}: <Ref to="${i}"> names no quantity. ${c ? `The ids that exist: ${c}.` : "No quantity has an id."}`);
124
+ }
125
+ t.key = e;
126
+ }
127
+ return ye(m), { result: _, quantities: m, metrics: n };
128
+ }
129
+ function be(o) {
130
+ return o.metrics ? o.metrics.map((n) => {
131
+ const f = /* @__PURE__ */ new Map();
132
+ for (const [y, r] of o.quantities) {
133
+ const m = r.given && { ...r.given, value: r.given.value[n.id] };
134
+ f.set(y, { ...r, unit: n.unit, decimals: n.decimals, given: m });
135
+ }
136
+ return { result: o.result, quantities: f };
137
+ }) : [o];
138
+ }
139
+ function ye(o) {
140
+ const n = /* @__PURE__ */ new Set(), f = [], y = (r) => {
141
+ if (n.has(r)) return;
142
+ const m = f.indexOf(r);
143
+ if (m !== -1) {
144
+ const v = [...f.slice(m), r].map((P) => o.get(P).label);
145
+ a(`the calculation goes round in a circle through references: ${v.join(" → ")}.`);
146
+ }
147
+ f.push(r);
148
+ for (const v of o.get(r).operands) y(v.key);
149
+ f.pop(), n.add(r);
150
+ };
151
+ for (const r of o.keys()) y(r);
152
+ }
153
+ export {
154
+ le as C,
155
+ re as D,
156
+ ae as G,
157
+ x as I,
158
+ de as M,
159
+ fe as P,
160
+ ue as Q,
161
+ J as R,
162
+ se as S,
163
+ pe as T,
164
+ me as a,
165
+ ce as b,
166
+ be as p,
167
+ ge as r
168
+ };
package/dist/index.d.ts CHANGED
@@ -10,7 +10,7 @@ import { ReactNode } from 'react';
10
10
  operation it shows, the number cannot disagree with the lines above it.
11
11
  Every tree below the outermost statement starts folded and opens beneath
12
12
  the row that is clicked. */
13
- export declare function Calculation({ children, className, style, metrics, density, ...rest }: CalculationProps): JSX.Element;
13
+ export declare function Calculation({ children, metrics, ...rest }: CalculationProps): JSX.Element;
14
14
 
15
15
  /** `className` and `style` size and place the surface; every other attribute -
16
16
  `aria-label` first of all - lands on the list the surface holds. */
package/docs/llms-full.md CHANGED
@@ -1,4 +1,4 @@
1
- # @umriss-ui/calculation 0.41.1
1
+ # @umriss-ui/calculation 0.42.0
2
2
 
3
3
  > React calculation view for data-dense applications - a derivation written as it is shown, evaluated by the library, folded and read line by line.
4
4
 
@@ -1567,7 +1567,7 @@ Also takes every attribute of the rendered element – without `children`.
1567
1567
  - No result, formula text or operator of your own: a written-out operation the library did not perform could say something the number does not ([ADR-0027](https://github.com/romanhaendler/umriss-ui/blob/main/docs/adr/0027-a-calculation-is-written-as-it-is-shown.md)).
1568
1568
  - No input: a calculation shows how a figure came about; changing a given is your application's form.
1569
1569
 
1570
- What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.41.1/docs/adr/0032-what-umriss-is-not.md).
1570
+ What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.42.0/docs/adr/0032-what-umriss-is-not.md).
1571
1571
 
1572
1572
  ### Tree
1573
1573
 
@@ -1848,7 +1848,7 @@ type Severity = "warning" | "alarm";
1848
1848
  - Four operators and nothing else – no powers, roots or functions; compute those before and hand them in as givens.
1849
1849
  - A unit is a label and is never converted or checked; the calculation does no unit algebra.
1850
1850
 
1851
- What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.41.1/docs/adr/0032-what-umriss-is-not.md).
1851
+ What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.42.0/docs/adr/0032-what-umriss-is-not.md).
1852
1852
 
1853
1853
  ### Chain
1854
1854
 
@@ -2196,7 +2196,7 @@ type Severity = "warning" | "alarm";
2196
2196
 
2197
2197
  - A chain in view does not fold; as an operand of a tree it folds to its last interim.
2198
2198
 
2199
- What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.41.1/docs/adr/0032-what-umriss-is-not.md).
2199
+ What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.42.0/docs/adr/0032-what-umriss-is-not.md).
2200
2200
 
2201
2201
  ### Rows from data
2202
2202
 
@@ -2636,7 +2636,7 @@ From `Metric` — the full table stands on [Metrics](#metrics).
2636
2636
 
2637
2637
  - Only a sum takes any count: an empty product, a difference of one and a quotient of anything but two still fail on the first render.
2638
2638
 
2639
- What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.41.1/docs/adr/0032-what-umriss-is-not.md).
2639
+ What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.42.0/docs/adr/0032-what-umriss-is-not.md).
2640
2640
 
2641
2641
  ### Given
2642
2642
 
@@ -3322,7 +3322,7 @@ The keys of [Calculation](#calculation) apply here.
3322
3322
  - Unit, format and places belong to the metric; on a quantity they fail on the first render, and so does a missing or unknown metric key in a `value`.
3323
3323
  - No target or limits with metrics: an assessment per cell is not there yet.
3324
3324
 
3325
- What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.41.1/docs/adr/0032-what-umriss-is-not.md).
3325
+ What umriss deliberately does not build, and why: [ADR-0032](https://github.com/romanhaendler/umriss-ui/blob/calculation-v0.42.0/docs/adr/0032-what-umriss-is-not.md).
3326
3326
 
3327
3327
  ## In practice
3328
3328
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umriss-ui/calculation",
3
- "version": "0.41.1",
3
+ "version": "0.42.0",
4
4
  "description": "React calculation view for data-dense applications - a derivation written as it is shown, evaluated by the library, folded and read line by line.",
5
5
  "license": "MIT",
6
6
  "author": "Roman Händler",
@@ -47,7 +47,8 @@
47
47
  "exports": {
48
48
  ".": {
49
49
  "types": "./dist/index.d.ts",
50
- "import": "./dist/calculation.js"
50
+ "import": "./dist/calculation.js",
51
+ "default": "./dist/calculation.js"
51
52
  },
52
53
  "./styles.css": {
53
54
  "types": "./dist/styles.d.ts",
@@ -55,9 +56,9 @@
55
56
  }
56
57
  },
57
58
  "peerDependencies": {
59
+ "@umriss-ui/core": "^0.42.0",
58
60
  "react": ">=19",
59
- "react-dom": ">=19",
60
- "@umriss-ui/core": "^0.41.1"
61
+ "react-dom": ">=19"
61
62
  },
62
63
  "devDependencies": {
63
64
  "@fontsource/geist-mono": "^5.3.0",
@@ -71,7 +72,7 @@
71
72
  "typescript": "^5.6.3",
72
73
  "vite": "^6.0.0",
73
74
  "vite-plugin-dts": "^4.3.0",
74
- "@umriss-ui/core": "0.41.1",
75
+ "@umriss-ui/core": "0.42.0",
75
76
  "@umriss-ui/demo": "0.0.0"
76
77
  },
77
78
  "scripts": {
@@ -86,6 +87,6 @@
86
87
  "check:site": "pnpm run props -- --check",
87
88
  "preview:demo": "vite preview --config vite.demo.config.ts --port 4177 --strictPort",
88
89
  "test:unit": "vitest run",
89
- "check:dist": "node --disable-warning=ExperimentalWarning --experimental-strip-types ../../scripts/check-dist.ts calculation.js calculation.css"
90
+ "check:dist": "node --disable-warning=ExperimentalWarning --experimental-strip-types ../../scripts/check-dist.ts calculation.js calculation.css calculation.client.js"
90
91
  }
91
92
  }