@stll/calculations 0.1.0 → 0.1.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/dist/index.d.ts +11 -11
- package/dist/index.js +6 -5
- package/package.json +9 -8
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
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { panic } from "better-result";
|
|
1
2
|
import { MoneyTotals, cents } from "@stll/money";
|
|
2
3
|
//#region src/index.ts
|
|
3
4
|
/**
|
|
@@ -81,7 +82,7 @@ const runCalculation = ({ kind, values, scopeValues }) => {
|
|
|
81
82
|
kind,
|
|
82
83
|
reason: "no-scope"
|
|
83
84
|
} : percentOfTotal(kind, values, scopeValues);
|
|
84
|
-
default: return kind;
|
|
85
|
+
default: return panic(`Unhandled kind: ${String(kind)}`);
|
|
85
86
|
}
|
|
86
87
|
};
|
|
87
88
|
const countEmpty = (values) => values.filter((value) => value.type === "empty").length;
|
|
@@ -96,7 +97,7 @@ const uniqueKey = (value) => {
|
|
|
96
97
|
case "number": return `n:${value.value}`;
|
|
97
98
|
case "money": return `m:${value.currency}:${value.amountCents}`;
|
|
98
99
|
case "text": return `t:${value.value}`;
|
|
99
|
-
default: return value;
|
|
100
|
+
default: return panic(`Unhandled value: ${String(value)}`);
|
|
100
101
|
}
|
|
101
102
|
};
|
|
102
103
|
const countUnique = (values) => {
|
|
@@ -131,7 +132,7 @@ const classify = (values) => {
|
|
|
131
132
|
else buckets.set(value.currency, [value.amountCents]);
|
|
132
133
|
break;
|
|
133
134
|
}
|
|
134
|
-
default: return value;
|
|
135
|
+
default: return panic(`Unhandled value: ${String(value)}`);
|
|
135
136
|
}
|
|
136
137
|
if (numbers.length > 0 && buckets.size > 0) return {
|
|
137
138
|
type: "unsupported",
|
|
@@ -192,7 +193,7 @@ const reduceNumeric = (kind, values) => {
|
|
|
192
193
|
totals: totals.entries()
|
|
193
194
|
};
|
|
194
195
|
}
|
|
195
|
-
default: return classified;
|
|
196
|
+
default: return panic(`Unhandled classified: ${String(classified)}`);
|
|
196
197
|
}
|
|
197
198
|
};
|
|
198
199
|
/**
|
|
@@ -256,7 +257,7 @@ const singleTotal = (values) => {
|
|
|
256
257
|
unit: currency
|
|
257
258
|
};
|
|
258
259
|
}
|
|
259
|
-
default: return classified;
|
|
260
|
+
default: return panic(`Unhandled classified: ${String(classified)}`);
|
|
260
261
|
}
|
|
261
262
|
};
|
|
262
263
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/calculations",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Typed, unit-safe reducers for workspace column calculations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"calculations",
|
|
@@ -39,22 +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 $(grep -
|
|
42
|
+
"test:property": "bun test --preload @stll/property-testing/preload $(grep -rlE 'fc\\.assert|assertProperty' src)",
|
|
43
43
|
"typecheck": "bun ../../packages/scripts/src/tsc-native.ts --noEmit",
|
|
44
|
-
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --
|
|
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
|
-
"format": "oxfmt .",
|
|
46
|
+
"format": "bun ../../scripts/run-oxfmt.ts .",
|
|
47
47
|
"prepack": "bun run build"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@stll/money": "^0.
|
|
50
|
+
"@stll/money": "^0.3.0",
|
|
51
|
+
"better-result": "3.0.1"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
54
|
"@stll/property-testing": "0.0.0",
|
|
54
55
|
"@stll/typescript-config": "0.0.0",
|
|
55
|
-
"
|
|
56
|
-
"fast-check": "^4.
|
|
57
|
-
"tsdown": "0.
|
|
56
|
+
"bun-types": "1.4.2",
|
|
57
|
+
"fast-check": "^4.10.1",
|
|
58
|
+
"tsdown": "0.23.0"
|
|
58
59
|
},
|
|
59
60
|
"main": "./dist/index.js",
|
|
60
61
|
"types": "./dist/index.d.ts"
|