@stll/calculations 0.1.1 → 0.1.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/dist/index.d.ts +11 -11
- package/dist/index.js +37 -16
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { CentsAmount, MoneyTotalsEntry } from "@stll/money";
|
|
2
2
|
//#region src/index.d.ts
|
|
3
|
-
declare const CALCULATION_KINDS: readonly ["count", "count-unique", "count-empty", "count-filled", "percent-empty", "percent-filled", "sum", "average", "median", "min", "max", "range", "percent-of-total"];
|
|
4
|
-
type CalculationKind = (typeof CALCULATION_KINDS)[number];
|
|
3
|
+
export declare const CALCULATION_KINDS: readonly ["count", "count-unique", "count-empty", "count-filled", "percent-empty", "percent-filled", "sum", "average", "median", "min", "max", "range", "percent-of-total"];
|
|
4
|
+
export type CalculationKind = (typeof CALCULATION_KINDS)[number];
|
|
5
5
|
/** Kinds that reduce the values themselves rather than counting rows. */
|
|
6
|
-
declare const NUMERIC_CALCULATION_KINDS: readonly ["sum", "average", "median", "min", "max", "range", "percent-of-total"];
|
|
7
|
-
type NumericCalculationKind = (typeof NUMERIC_CALCULATION_KINDS)[number];
|
|
8
|
-
declare const isNumericCalculationKind: (kind: CalculationKind) => kind is NumericCalculationKind;
|
|
6
|
+
export declare const NUMERIC_CALCULATION_KINDS: readonly ["sum", "average", "median", "min", "max", "range", "percent-of-total"];
|
|
7
|
+
export type NumericCalculationKind = (typeof NUMERIC_CALCULATION_KINDS)[number];
|
|
8
|
+
export declare const isNumericCalculationKind: (kind: CalculationKind) => kind is NumericCalculationKind;
|
|
9
9
|
/**
|
|
10
10
|
* One row's contribution to a calculation.
|
|
11
11
|
*
|
|
@@ -13,7 +13,7 @@ declare const isNumericCalculationKind: (kind: CalculationKind) => kind is Numer
|
|
|
13
13
|
* rather than a null value so "no value" is never confused with the number
|
|
14
14
|
* zero, which is what makes count-empty and average disagree correctly.
|
|
15
15
|
*/
|
|
16
|
-
type CalculationValue = {
|
|
16
|
+
export type CalculationValue = {
|
|
17
17
|
type: "empty";
|
|
18
18
|
} | {
|
|
19
19
|
type: "number";
|
|
@@ -26,7 +26,7 @@ type CalculationValue = {
|
|
|
26
26
|
type: "text";
|
|
27
27
|
value: string;
|
|
28
28
|
};
|
|
29
|
-
type CalculationUnsupportedReason =
|
|
29
|
+
export type CalculationUnsupportedReason =
|
|
30
30
|
/** A numeric reduction over values that are not numbers. */
|
|
31
31
|
"non-numeric" |
|
|
32
32
|
/** Numbers mixed with money, or money the reduction cannot combine. */
|
|
@@ -35,7 +35,7 @@ type CalculationUnsupportedReason =
|
|
|
35
35
|
"no-values" |
|
|
36
36
|
/** A share of the whole, asked without being told what the whole is. */
|
|
37
37
|
"no-scope";
|
|
38
|
-
type CalculationResult =
|
|
38
|
+
export type CalculationResult =
|
|
39
39
|
/** A number of rows. */
|
|
40
40
|
{
|
|
41
41
|
type: "count";
|
|
@@ -64,7 +64,7 @@ type CalculationResult =
|
|
|
64
64
|
kind: CalculationKind;
|
|
65
65
|
reason: CalculationUnsupportedReason;
|
|
66
66
|
};
|
|
67
|
-
type RunCalculationParams = {
|
|
67
|
+
export type RunCalculationParams = {
|
|
68
68
|
kind: CalculationKind;
|
|
69
69
|
/** The values in the column, in row order. */
|
|
70
70
|
values: readonly CalculationValue[];
|
|
@@ -76,6 +76,6 @@ type RunCalculationParams = {
|
|
|
76
76
|
*/
|
|
77
77
|
scopeValues?: readonly CalculationValue[] | undefined;
|
|
78
78
|
};
|
|
79
|
-
declare const runCalculation: ({ kind, values, scopeValues }: RunCalculationParams) => CalculationResult;
|
|
79
|
+
export declare const runCalculation: ({ kind, values, scopeValues }: RunCalculationParams) => CalculationResult;
|
|
80
80
|
//#endregion
|
|
81
|
-
export {
|
|
81
|
+
export type { CentsAmount, MoneyTotalsEntry };
|
package/dist/index.js
CHANGED
|
@@ -124,9 +124,17 @@ const classify = (values) => {
|
|
|
124
124
|
reason: "non-numeric"
|
|
125
125
|
};
|
|
126
126
|
case "number":
|
|
127
|
+
if (!Number.isFinite(value.value)) return {
|
|
128
|
+
type: "unsupported",
|
|
129
|
+
reason: "non-numeric"
|
|
130
|
+
};
|
|
127
131
|
numbers.push(value.value);
|
|
128
132
|
break;
|
|
129
133
|
case "money": {
|
|
134
|
+
if (!Number.isFinite(value.amountCents)) return {
|
|
135
|
+
type: "unsupported",
|
|
136
|
+
reason: "non-numeric"
|
|
137
|
+
};
|
|
130
138
|
const bucket = buckets.get(value.currency);
|
|
131
139
|
if (bucket) bucket.push(value.amountCents);
|
|
132
140
|
else buckets.set(value.currency, [value.amountCents]);
|
|
@@ -179,14 +187,18 @@ const reduceNumeric = (kind, values) => {
|
|
|
179
187
|
kind,
|
|
180
188
|
reason: classified.reason
|
|
181
189
|
};
|
|
182
|
-
case "numbers": return
|
|
183
|
-
type: "number",
|
|
184
|
-
kind,
|
|
185
|
-
value: REDUCERS[kind](classified.values)
|
|
186
|
-
};
|
|
190
|
+
case "numbers": return finiteNumberResult(kind, REDUCERS[kind](classified.values));
|
|
187
191
|
case "money": {
|
|
188
192
|
const totals = new MoneyTotals();
|
|
189
|
-
for (const [currency, amounts] of classified.buckets)
|
|
193
|
+
for (const [currency, amounts] of classified.buckets) {
|
|
194
|
+
const reduced = REDUCERS[kind](amounts);
|
|
195
|
+
if (!Number.isFinite(reduced)) return {
|
|
196
|
+
type: "unsupported",
|
|
197
|
+
kind,
|
|
198
|
+
reason: "non-numeric"
|
|
199
|
+
};
|
|
200
|
+
totals.add(currency, cents(Math.round(reduced)));
|
|
201
|
+
}
|
|
190
202
|
return {
|
|
191
203
|
type: "money",
|
|
192
204
|
kind,
|
|
@@ -196,6 +208,15 @@ const reduceNumeric = (kind, values) => {
|
|
|
196
208
|
default: return panic(`Unhandled classified: ${String(classified)}`);
|
|
197
209
|
}
|
|
198
210
|
};
|
|
211
|
+
const finiteNumberResult = (kind, value) => Number.isFinite(value) ? {
|
|
212
|
+
type: "number",
|
|
213
|
+
kind,
|
|
214
|
+
value
|
|
215
|
+
} : {
|
|
216
|
+
type: "unsupported",
|
|
217
|
+
kind,
|
|
218
|
+
reason: "non-numeric"
|
|
219
|
+
};
|
|
199
220
|
/**
|
|
200
221
|
* The column's total as a share of the view's total.
|
|
201
222
|
*
|
|
@@ -240,25 +261,25 @@ const singleTotal = (values) => {
|
|
|
240
261
|
type: "unsupported",
|
|
241
262
|
reason: classified.reason
|
|
242
263
|
};
|
|
243
|
-
case "numbers": return
|
|
244
|
-
type: "total",
|
|
245
|
-
value: REDUCERS.sum(classified.values),
|
|
246
|
-
unit: "number"
|
|
247
|
-
};
|
|
264
|
+
case "numbers": return finiteTotal(REDUCERS.sum(classified.values), "number");
|
|
248
265
|
case "money": {
|
|
249
266
|
if (classified.buckets.size > 1) return {
|
|
250
267
|
type: "unsupported",
|
|
251
268
|
reason: "mixed-units"
|
|
252
269
|
};
|
|
253
270
|
const [currency, amounts] = [...classified.buckets.entries()].at(0) ?? ["", []];
|
|
254
|
-
return
|
|
255
|
-
type: "total",
|
|
256
|
-
value: REDUCERS.sum(amounts),
|
|
257
|
-
unit: currency
|
|
258
|
-
};
|
|
271
|
+
return finiteTotal(REDUCERS.sum(amounts), currency);
|
|
259
272
|
}
|
|
260
273
|
default: return panic(`Unhandled classified: ${String(classified)}`);
|
|
261
274
|
}
|
|
262
275
|
};
|
|
276
|
+
const finiteTotal = (value, unit) => Number.isFinite(value) ? {
|
|
277
|
+
type: "total",
|
|
278
|
+
value,
|
|
279
|
+
unit
|
|
280
|
+
} : {
|
|
281
|
+
type: "unsupported",
|
|
282
|
+
reason: "non-numeric"
|
|
283
|
+
};
|
|
263
284
|
//#endregion
|
|
264
285
|
export { CALCULATION_KINDS, NUMERIC_CALCULATION_KINDS, isNumericCalculationKind, runCalculation };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/calculations",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Typed, unit-safe reducers for workspace column calculations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"calculations",
|
|
@@ -39,23 +39,23 @@
|
|
|
39
39
|
"build": "tsdown",
|
|
40
40
|
"pack:dry-run": "bun pm pack --dry-run",
|
|
41
41
|
"test": "bun test src",
|
|
42
|
-
"test:property": "bun test --preload @stll/property-testing/preload $(grep -
|
|
43
|
-
"typecheck": "bun
|
|
44
|
-
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --
|
|
42
|
+
"test:property": "bun test --preload @stll/property-testing/preload $(grep -rlE 'fc\\.assert|assertProperty' src)",
|
|
43
|
+
"typecheck": "bun check --no-pretty --all --project=tsconfig.json",
|
|
44
|
+
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --type-aware --type-check packages/calculations",
|
|
45
45
|
"lint:fix": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --type-aware --fix packages/calculations",
|
|
46
46
|
"format": "bun ../../scripts/run-oxfmt.ts .",
|
|
47
47
|
"prepack": "bun run build"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@stll/money": "^0.
|
|
51
|
-
"better-result": "3.0.
|
|
50
|
+
"@stll/money": "^0.3.1",
|
|
51
|
+
"better-result": "3.0.1"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@stll/property-testing": "0.0.0",
|
|
55
55
|
"@stll/typescript-config": "0.0.0",
|
|
56
|
-
"bun-types": "1.4.
|
|
57
|
-
"fast-check": "^4.
|
|
58
|
-
"tsdown": "0.
|
|
56
|
+
"bun-types": "1.4.3",
|
|
57
|
+
"fast-check": "^4.10.2",
|
|
58
|
+
"tsdown": "0.23.0"
|
|
59
59
|
},
|
|
60
60
|
"main": "./dist/index.js",
|
|
61
61
|
"types": "./dist/index.d.ts"
|