@classytic/primitives 0.31.1 → 0.32.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.
- package/CHANGELOG.md +9 -0
- package/dist/quantity.d.mts +23 -2
- package/dist/quantity.mjs +31 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,15 @@
|
|
|
3
3
|
Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
4
4
|
adhering to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
5
5
|
|
|
6
|
+
## 0.32.0 — 2026-10-12
|
|
7
|
+
|
|
8
|
+
### Added — `./quantity`: the step and running-sum helpers every package was copying
|
|
9
|
+
|
|
10
|
+
Seven packages (order, cart, catalog, invoice, bd-tax, commerce-ui, pos-ui) each carried their own copy of the quantity grid; the pieces the grid did not yet offer are now here, so a consumer imports instead of copying.
|
|
11
|
+
- `isQuantityStep(step)` — a guard for a usable step (a positive number on the grid, at least one grid unit). True exactly when `quantityStepToGrid` accepts it, pinned by a property test. For untrusted input (a stored `quantityStep`, a request body), where a throw is the wrong answer.
|
|
12
|
+
- `isOnStepStrict(quantity, step)` — is the quantity ON the grid and a whole multiple of a valid step? STRICT and total: junk, an off-grid quantity or an invalid step is `false`, never a throw and never a snap. `isOnStep` is unchanged (it snaps first and throws on a bad step; its docs now say so and point here).
|
|
13
|
+
- `addQuantity(a, b)` / `subtractQuantity(a, b)` — arithmetic on the grid, so a running sum never drifts (`1.2 + 1.15` is `2.35`). Zero is `0`, never `-0`. A non-finite operand throws `QuantityError`, as everywhere in the grid.
|
|
14
|
+
|
|
6
15
|
## 0.31.1 — 2026-10-11
|
|
7
16
|
|
|
8
17
|
### Fixed — `convertWithSnapshot` is exact (`./currency`)
|
package/dist/quantity.d.mts
CHANGED
|
@@ -28,11 +28,32 @@ declare function snapQuantity(quantity: number, step?: number, mode?: RoundingMo
|
|
|
28
28
|
* boundary, where a 7th decimal is a caller error rather than float drift.
|
|
29
29
|
*/
|
|
30
30
|
declare function isOnQuantityGrid(value: number): boolean;
|
|
31
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Is `quantity` a whole multiple of `step`, within half a grid unit? Snaps `quantity` first and THROWS
|
|
33
|
+
* ({@link QuantityError}) on a non-finite quantity or an invalid step. Use {@link isOnStepStrict} where a
|
|
34
|
+
* malformed input must answer `false` instead.
|
|
35
|
+
*/
|
|
32
36
|
declare function isOnStep(quantity: number, step: number): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Is `step` a usable quantity step: a positive number on the grid, at least one grid unit? True exactly
|
|
39
|
+
* when {@link quantityStepToGrid} accepts it. A guard for untrusted input (a stored `quantityStep`, a
|
|
40
|
+
* request body) where a throw is the wrong answer.
|
|
41
|
+
*/
|
|
42
|
+
declare function isQuantityStep(step: unknown): step is number;
|
|
43
|
+
/**
|
|
44
|
+
* Is `quantity` ON the grid and a whole multiple of a valid `step`? STRICT and total: an off-grid or
|
|
45
|
+
* non-finite quantity, or an invalid step, is `false` — never a throw and never a snap (`2.3500004`
|
|
46
|
+
* is off the 0.001 step here, on it for {@link isOnStep}). The check for an input boundary, such as a
|
|
47
|
+
* line quantity against the line's step.
|
|
48
|
+
*/
|
|
49
|
+
declare function isOnStepStrict(quantity: number, step: number): boolean;
|
|
50
|
+
/** `a + b` on the grid, so a running sum never drifts (`1.2 + 1.15` is 2.35, not 2.3499…). */
|
|
51
|
+
declare function addQuantity(a: number, b: number): number;
|
|
52
|
+
/** `a − b` on the grid, so a running balance never drifts. */
|
|
53
|
+
declare function subtractQuantity(a: number, b: number): number;
|
|
33
54
|
/** Compare two quantities on the grid: equal when they snap to the same grid unit. */
|
|
34
55
|
declare function compareQuantity(a: number, b: number): -1 | 0 | 1;
|
|
35
56
|
/** `compareQuantity(a, b) === 0`. */
|
|
36
57
|
declare function quantitiesEqual(a: number, b: number): boolean;
|
|
37
58
|
//#endregion
|
|
38
|
-
export { QUANTITY_SCALE, QuantityError, QuantityErrorCode, type RoundingMode, compareQuantity, isOnQuantityGrid, isOnStep, quantitiesEqual, quantityStepToGrid, quantityToGrid, snapQuantity };
|
|
59
|
+
export { QUANTITY_SCALE, QuantityError, QuantityErrorCode, type RoundingMode, addQuantity, compareQuantity, isOnQuantityGrid, isOnStep, isOnStepStrict, isQuantityStep, quantitiesEqual, quantityStepToGrid, quantityToGrid, snapQuantity, subtractQuantity };
|
package/dist/quantity.mjs
CHANGED
|
@@ -70,10 +70,39 @@ function isOnQuantityGrid(value) {
|
|
|
70
70
|
const grid = Math.round(scaled);
|
|
71
71
|
return Number.isSafeInteger(grid) && Math.abs(grid - scaled) <= 1e-9 * Math.max(1, Math.abs(scaled));
|
|
72
72
|
}
|
|
73
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Is `quantity` a whole multiple of `step`, within half a grid unit? Snaps `quantity` first and THROWS
|
|
75
|
+
* ({@link QuantityError}) on a non-finite quantity or an invalid step. Use {@link isOnStepStrict} where a
|
|
76
|
+
* malformed input must answer `false` instead.
|
|
77
|
+
*/
|
|
74
78
|
function isOnStep(quantity, step) {
|
|
75
79
|
return quantityToGrid(quantity) % quantityStepToGrid(step) === 0n;
|
|
76
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Is `step` a usable quantity step: a positive number on the grid, at least one grid unit? True exactly
|
|
83
|
+
* when {@link quantityStepToGrid} accepts it. A guard for untrusted input (a stored `quantityStep`, a
|
|
84
|
+
* request body) where a throw is the wrong answer.
|
|
85
|
+
*/
|
|
86
|
+
function isQuantityStep(step) {
|
|
87
|
+
return typeof step === "number" && Number.isFinite(step) && step > 0 && isOnQuantityGrid(step) && quantityToGrid(step) > 0n;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Is `quantity` ON the grid and a whole multiple of a valid `step`? STRICT and total: an off-grid or
|
|
91
|
+
* non-finite quantity, or an invalid step, is `false` — never a throw and never a snap (`2.3500004`
|
|
92
|
+
* is off the 0.001 step here, on it for {@link isOnStep}). The check for an input boundary, such as a
|
|
93
|
+
* line quantity against the line's step.
|
|
94
|
+
*/
|
|
95
|
+
function isOnStepStrict(quantity, step) {
|
|
96
|
+
return isOnQuantityGrid(quantity) && isQuantityStep(step) && isOnStep(quantity, step);
|
|
97
|
+
}
|
|
98
|
+
/** `a + b` on the grid, so a running sum never drifts (`1.2 + 1.15` is 2.35, not 2.3499…). */
|
|
99
|
+
function addQuantity(a, b) {
|
|
100
|
+
return gridToNumber(quantityToGrid(a) + quantityToGrid(b));
|
|
101
|
+
}
|
|
102
|
+
/** `a − b` on the grid, so a running balance never drifts. */
|
|
103
|
+
function subtractQuantity(a, b) {
|
|
104
|
+
return gridToNumber(quantityToGrid(a) - quantityToGrid(b));
|
|
105
|
+
}
|
|
77
106
|
/** Compare two quantities on the grid: equal when they snap to the same grid unit. */
|
|
78
107
|
function compareQuantity(a, b) {
|
|
79
108
|
const ga = quantityToGrid(a);
|
|
@@ -85,4 +114,4 @@ function quantitiesEqual(a, b) {
|
|
|
85
114
|
return compareQuantity(a, b) === 0;
|
|
86
115
|
}
|
|
87
116
|
//#endregion
|
|
88
|
-
export { QUANTITY_SCALE, QuantityError, compareQuantity, isOnQuantityGrid, isOnStep, quantitiesEqual, quantityStepToGrid, quantityToGrid, snapQuantity };
|
|
117
|
+
export { QUANTITY_SCALE, QuantityError, addQuantity, compareQuantity, isOnQuantityGrid, isOnStep, isOnStepStrict, isQuantityStep, quantitiesEqual, quantityStepToGrid, quantityToGrid, snapQuantity, subtractQuantity };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classytic/primitives",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.0",
|
|
4
4
|
"description": "Framework-agnostic TypeScript primitives shared across Classytic packages — Money, events, pagination, tenant config, and other building blocks.",
|
|
5
5
|
"author": "Classytic",
|
|
6
6
|
"license": "MIT",
|