@stripe/extensibility-sdk 0.24.1 → 0.25.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/README.md +355 -0
- package/dist/config-values/generate.d.ts +2 -0
- package/dist/config-values/generate.d.ts.map +1 -1
- package/dist/extensibility-sdk-alpha.d.ts +140 -38
- package/dist/extensibility-sdk-beta.d.ts +140 -38
- package/dist/extensibility-sdk-extensions-alpha.d.ts +155 -25
- package/dist/extensibility-sdk-extensions-beta.d.ts +155 -25
- package/dist/extensibility-sdk-extensions-internal.d.ts +159 -25
- package/dist/extensibility-sdk-extensions-public.d.ts +155 -25
- package/dist/extensibility-sdk-internal-alpha.d.ts +5 -0
- package/dist/extensibility-sdk-internal-beta.d.ts +5 -0
- package/dist/extensibility-sdk-internal-internal.d.ts +5 -0
- package/dist/extensibility-sdk-internal-public.d.ts +5 -0
- package/dist/extensibility-sdk-internal.d.ts +139 -40
- package/dist/extensibility-sdk-public.d.ts +140 -38
- package/dist/extensibility-sdk-stdlib-alpha.d.ts +145 -38
- package/dist/extensibility-sdk-stdlib-beta.d.ts +145 -38
- package/dist/extensibility-sdk-stdlib-internal.d.ts +144 -40
- package/dist/extensibility-sdk-stdlib-public.d.ts +145 -38
- package/dist/extensions/billing/bill/discount_calculation.d.ts +5 -3
- package/dist/extensions/billing/customer_balance_application.d.ts +3 -1
- package/dist/extensions/billing/invoice_collection_setting.d.ts +15 -11
- package/dist/extensions/billing/prorations.d.ts +30 -21
- package/dist/extensions/billing/recurring_billing_item_handling.d.ts +41 -23
- package/dist/extensions/billing/types.d.ts +4 -4
- package/dist/extensions/core/workflows/custom_action.d.ts +6 -2
- package/dist/extensions/extend/workflows/custom_action.d.ts +6 -2
- package/dist/index.cjs +385 -134
- package/dist/index.js +383 -133
- package/dist/internal.d.ts +4 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/stdlib/brand.d.ts +16 -10
- package/dist/stdlib/brand.d.ts.map +1 -1
- package/dist/stdlib/decimal.d.ts +49 -21
- package/dist/stdlib/decimal.d.ts.map +1 -1
- package/dist/stdlib/index.cjs +385 -134
- package/dist/stdlib/index.d.ts +10 -4
- package/dist/stdlib/index.d.ts.map +1 -1
- package/dist/stdlib/index.js +383 -133
- package/dist/stdlib/refs.d.ts +21 -7
- package/dist/stdlib/scalars.d.ts +61 -28
- package/dist/stdlib/scalars.d.ts.map +1 -1
- package/dist/stdlib/to-const.d.ts +35 -0
- package/dist/stdlib/to-const.d.ts.map +1 -0
- package/dist/stdlib/type-utils.d.ts +3 -1
- package/dist/stdlib/types.d.ts +6 -6
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +11 -11
- package/dist/api-surface.d.ts.map +0 -1
package/dist/stdlib/index.cjs
CHANGED
|
@@ -52,123 +52,13 @@ __export(stdlib_exports, {
|
|
|
52
52
|
_translateMap: () => _translateMap,
|
|
53
53
|
_translateShape: () => _translateShape,
|
|
54
54
|
_translateUnion: () => _translateUnion,
|
|
55
|
-
isDecimal: () => isDecimal
|
|
55
|
+
isDecimal: () => isDecimal,
|
|
56
|
+
toConst: () => toConst
|
|
56
57
|
});
|
|
57
58
|
module.exports = __toCommonJS(stdlib_exports);
|
|
58
59
|
|
|
59
60
|
// src/stdlib/scalars.ts
|
|
60
61
|
var import_core = require("@formspec/core");
|
|
61
|
-
function roundToInteger(value, direction) {
|
|
62
|
-
switch (direction) {
|
|
63
|
-
case "ceil":
|
|
64
|
-
return Math.ceil(value);
|
|
65
|
-
case "floor":
|
|
66
|
-
return Math.floor(value);
|
|
67
|
-
case "round-down":
|
|
68
|
-
return Math.trunc(value);
|
|
69
|
-
case "round-up":
|
|
70
|
-
return value >= 0 ? Math.ceil(value) : Math.floor(value);
|
|
71
|
-
case "half-up":
|
|
72
|
-
return value >= 0 ? Math.floor(value + 0.5) : Math.ceil(value - 0.5);
|
|
73
|
-
default: {
|
|
74
|
-
const _exhaustive = direction;
|
|
75
|
-
throw new Error(`Unknown rounding direction: ${String(_exhaustive)}`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
var Integer = {
|
|
80
|
-
/**
|
|
81
|
-
* Type guard that narrows a `number` to {@link (Integer:type)}.
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
* ```ts
|
|
85
|
-
* const n: number = getCount();
|
|
86
|
-
* if (Integer.is(n)) {
|
|
87
|
-
* // n is Integer here
|
|
88
|
-
* config.retryCount = n;
|
|
89
|
-
* }
|
|
90
|
-
* ```
|
|
91
|
-
* @public
|
|
92
|
-
*/
|
|
93
|
-
is: (value) => Number.isInteger(value),
|
|
94
|
-
/**
|
|
95
|
-
* Coerces a number to an {@link (Integer:type)} by rounding.
|
|
96
|
-
* Throws if the value is not finite.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* const price = 9.99;
|
|
101
|
-
* const rounded = Integer.from(price, 'floor'); // 9
|
|
102
|
-
* const ceiled = Integer.from(price, 'ceil'); // 10
|
|
103
|
-
* ```
|
|
104
|
-
* @public
|
|
105
|
-
*/
|
|
106
|
-
from: (value, rounding) => {
|
|
107
|
-
if (!Number.isFinite(value)) {
|
|
108
|
-
throw new Error(`Cannot round non-finite value ${String(value)} to an integer`);
|
|
109
|
-
}
|
|
110
|
-
return roundToInteger(value, rounding);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
var PositiveInteger = {
|
|
114
|
-
/**
|
|
115
|
-
* Type guard that narrows a `number` to {@link (PositiveInteger:type)}.
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* ```ts
|
|
119
|
-
* const n: number = getRetryCount();
|
|
120
|
-
* if (PositiveInteger.is(n)) {
|
|
121
|
-
* // n is PositiveInteger here
|
|
122
|
-
* config.maxRetries = n;
|
|
123
|
-
* }
|
|
124
|
-
* ```
|
|
125
|
-
* @public
|
|
126
|
-
*/
|
|
127
|
-
is: (value) => Number.isInteger(value) && value >= 0,
|
|
128
|
-
/**
|
|
129
|
-
* Coerces a number to a {@link (PositiveInteger:type)} by rounding.
|
|
130
|
-
* Throws if the value is not finite or the rounded result is negative.
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```ts
|
|
134
|
-
* const ratio = 2.7;
|
|
135
|
-
* const count = PositiveInteger.from(ratio, 'floor'); // 2
|
|
136
|
-
* ```
|
|
137
|
-
* @public
|
|
138
|
-
*/
|
|
139
|
-
from: (value, rounding) => {
|
|
140
|
-
if (!Number.isFinite(value)) {
|
|
141
|
-
throw new Error(`Cannot round non-finite value ${String(value)} to an integer`);
|
|
142
|
-
}
|
|
143
|
-
const rounded = roundToInteger(value, rounding) || 0;
|
|
144
|
-
if (rounded < 0) {
|
|
145
|
-
throw new Error(
|
|
146
|
-
`Value ${String(value)} rounds to ${String(rounded)}, which is negative`
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
return rounded;
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
var StreetAddress = {
|
|
153
|
-
create: (address) => {
|
|
154
|
-
return address;
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
var Timestamp = {
|
|
158
|
-
create: (value) => {
|
|
159
|
-
return value;
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
// src/stdlib/refs.ts
|
|
164
|
-
var Ref = {
|
|
165
|
-
create: (step) => {
|
|
166
|
-
return {
|
|
167
|
-
type: step.object,
|
|
168
|
-
id: step.id
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
62
|
|
|
173
63
|
// src/stdlib/decimal.ts
|
|
174
64
|
var PLAIN_NOTATION_DIGIT_LIMIT = 30;
|
|
@@ -185,6 +75,9 @@ var DecimalRoundingPresets = Object.freeze({
|
|
|
185
75
|
var DEFAULT_DIV_PRECISION = 34;
|
|
186
76
|
var IMPLICIT_DECIMAL_COERCION_ERROR = "Implicit Decimal coercion is not allowed; use .add(), .sub(), .mul(), .div(), .toString(), or .toNumber() explicitly.";
|
|
187
77
|
var MAX_EXPONENT = Number.MAX_SAFE_INTEGER;
|
|
78
|
+
function normalizeZero(value) {
|
|
79
|
+
return Object.is(value, -0) ? 0 : value;
|
|
80
|
+
}
|
|
188
81
|
var DECIMAL_BRAND = /* @__PURE__ */ Symbol.for(
|
|
189
82
|
"stripe.apps-extensibility-sdk.Decimal"
|
|
190
83
|
);
|
|
@@ -298,13 +191,13 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
298
191
|
/**
|
|
299
192
|
* Return the sum of this value and `other`.
|
|
300
193
|
*
|
|
301
|
-
* @param other - The addend.
|
|
194
|
+
* @param other - The addend. Accepts any {@link DecimalLike} value.
|
|
302
195
|
* @returns A new {@link Decimal} equal to `this + other`.
|
|
303
196
|
*
|
|
304
197
|
* @public
|
|
305
198
|
*/
|
|
306
199
|
add(other) {
|
|
307
|
-
const otherImpl =
|
|
200
|
+
const otherImpl = coerceToImpl(other);
|
|
308
201
|
if (this.#exponent === otherImpl.#exponent) {
|
|
309
202
|
return toDecimal(
|
|
310
203
|
new _DecimalImpl(this.#coefficient + otherImpl.#coefficient, this.#exponent)
|
|
@@ -331,13 +224,13 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
331
224
|
/**
|
|
332
225
|
* Return the difference of this value and `other`.
|
|
333
226
|
*
|
|
334
|
-
* @param other - The subtrahend.
|
|
227
|
+
* @param other - The subtrahend. Accepts any {@link DecimalLike} value.
|
|
335
228
|
* @returns A new {@link Decimal} equal to `this - other`.
|
|
336
229
|
*
|
|
337
230
|
* @public
|
|
338
231
|
*/
|
|
339
232
|
sub(other) {
|
|
340
|
-
const otherImpl =
|
|
233
|
+
const otherImpl = coerceToImpl(other);
|
|
341
234
|
if (this.#exponent === otherImpl.#exponent) {
|
|
342
235
|
return toDecimal(
|
|
343
236
|
new _DecimalImpl(this.#coefficient - otherImpl.#coefficient, this.#exponent)
|
|
@@ -364,13 +257,13 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
364
257
|
/**
|
|
365
258
|
* Return the product of this value and `other`.
|
|
366
259
|
*
|
|
367
|
-
* @param other - The multiplicand.
|
|
260
|
+
* @param other - The multiplicand. Accepts any {@link DecimalLike} value.
|
|
368
261
|
* @returns A new {@link Decimal} equal to `this × other`.
|
|
369
262
|
*
|
|
370
263
|
* @public
|
|
371
264
|
*/
|
|
372
265
|
mul(other) {
|
|
373
|
-
const otherImpl =
|
|
266
|
+
const otherImpl = coerceToImpl(other);
|
|
374
267
|
return toDecimal(
|
|
375
268
|
new _DecimalImpl(
|
|
376
269
|
this.#coefficient * otherImpl.#coefficient,
|
|
@@ -397,7 +290,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
397
290
|
* Decimal.from('5').div(Decimal.from('2'), 0, 'half-even'); // "2"
|
|
398
291
|
* ```
|
|
399
292
|
*
|
|
400
|
-
* @param other - The divisor. Must not be zero.
|
|
293
|
+
* @param other - The divisor. Must not be zero. Accepts any {@link DecimalLike} value.
|
|
401
294
|
* @param precision - Maximum number of decimal digits in the result.
|
|
402
295
|
* @param direction - How to round when the exact quotient cannot
|
|
403
296
|
* be represented at the requested precision.
|
|
@@ -412,7 +305,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
412
305
|
if (precision < 0 || !Number.isInteger(precision)) {
|
|
413
306
|
throw new Error("precision must be a non-negative integer");
|
|
414
307
|
}
|
|
415
|
-
const otherImpl =
|
|
308
|
+
const otherImpl = coerceToImpl(other);
|
|
416
309
|
if (otherImpl.#coefficient === 0n) {
|
|
417
310
|
throw new Error("Division by zero");
|
|
418
311
|
}
|
|
@@ -454,13 +347,13 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
454
347
|
* a.cmp(a); // 0
|
|
455
348
|
* ```
|
|
456
349
|
*
|
|
457
|
-
* @param other - The value to compare against.
|
|
350
|
+
* @param other - The value to compare against. Accepts any {@link DecimalLike} value.
|
|
458
351
|
* @returns `-1` if `this < other`, `0` if equal, `1` if `this > other`.
|
|
459
352
|
*
|
|
460
353
|
* @public
|
|
461
354
|
*/
|
|
462
355
|
cmp(other) {
|
|
463
|
-
const otherImpl =
|
|
356
|
+
const otherImpl = coerceToImpl(other);
|
|
464
357
|
if (this.#exponent === otherImpl.#exponent) {
|
|
465
358
|
if (this.#coefficient < otherImpl.#coefficient) return -1;
|
|
466
359
|
if (this.#coefficient > otherImpl.#coefficient) return 1;
|
|
@@ -483,7 +376,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
483
376
|
/**
|
|
484
377
|
* Return `true` if this value is numerically equal to `other`.
|
|
485
378
|
*
|
|
486
|
-
* @param other - The value to compare against.
|
|
379
|
+
* @param other - The value to compare against. Accepts any {@link DecimalLike} value.
|
|
487
380
|
* @returns `true` if `this === other` in value, `false` otherwise.
|
|
488
381
|
*
|
|
489
382
|
* @public
|
|
@@ -494,7 +387,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
494
387
|
/**
|
|
495
388
|
* Return `true` if this value is strictly less than `other`.
|
|
496
389
|
*
|
|
497
|
-
* @param other - The value to compare against.
|
|
390
|
+
* @param other - The value to compare against. Accepts any {@link DecimalLike} value.
|
|
498
391
|
* @returns `true` if `this < other`, `false` otherwise.
|
|
499
392
|
*
|
|
500
393
|
* @public
|
|
@@ -505,7 +398,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
505
398
|
/**
|
|
506
399
|
* Return `true` if this value is less than or equal to `other`.
|
|
507
400
|
*
|
|
508
|
-
* @param other - The value to compare against.
|
|
401
|
+
* @param other - The value to compare against. Accepts any {@link DecimalLike} value.
|
|
509
402
|
* @returns `true` if `this ≤ other`, `false` otherwise.
|
|
510
403
|
*
|
|
511
404
|
* @public
|
|
@@ -516,7 +409,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
516
409
|
/**
|
|
517
410
|
* Return `true` if this value is strictly greater than `other`.
|
|
518
411
|
*
|
|
519
|
-
* @param other - The value to compare against.
|
|
412
|
+
* @param other - The value to compare against. Accepts any {@link DecimalLike} value.
|
|
520
413
|
* @returns `true` if `this > other`, `false` otherwise.
|
|
521
414
|
*
|
|
522
415
|
* @public
|
|
@@ -527,7 +420,7 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
527
420
|
/**
|
|
528
421
|
* Return `true` if this value is greater than or equal to `other`.
|
|
529
422
|
*
|
|
530
|
-
* @param other - The value to compare against.
|
|
423
|
+
* @param other - The value to compare against. Accepts any {@link DecimalLike} value.
|
|
531
424
|
* @returns `true` if `this ≥ other`, `false` otherwise.
|
|
532
425
|
*
|
|
533
426
|
* @public
|
|
@@ -826,6 +719,38 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
826
719
|
return formatFixed(scaled);
|
|
827
720
|
}
|
|
828
721
|
}
|
|
722
|
+
/**
|
|
723
|
+
* Convert this value to an {@link (Integer:type)} by rounding.
|
|
724
|
+
*
|
|
725
|
+
* @remarks
|
|
726
|
+
* The rounding direction is always required — no invisible defaults
|
|
727
|
+
* in financial code.
|
|
728
|
+
*
|
|
729
|
+
* @example
|
|
730
|
+
* ```ts
|
|
731
|
+
* Decimal.from('2.7').toInteger('floor'); // 2
|
|
732
|
+
* Decimal.from('2.5').toInteger('half-up'); // 3
|
|
733
|
+
* Decimal.from('2.5').toInteger('half-even'); // 2
|
|
734
|
+
* ```
|
|
735
|
+
*
|
|
736
|
+
* @param direction - How to round when the value is not a whole number.
|
|
737
|
+
* @returns A branded {@link (Integer:type)} value.
|
|
738
|
+
* @throws Error if the rounded value is too large to represent as
|
|
739
|
+
* a JavaScript `number`.
|
|
740
|
+
*
|
|
741
|
+
* @public
|
|
742
|
+
*/
|
|
743
|
+
toInteger(direction) {
|
|
744
|
+
const fixed = this.toFixed(0, direction);
|
|
745
|
+
const num = Number(fixed);
|
|
746
|
+
if (!Number.isFinite(num) || !Number.isSafeInteger(num)) {
|
|
747
|
+
throw new Error(
|
|
748
|
+
`Decimal value ${fixed} cannot be exactly represented as a JavaScript integer`
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
const normalized = normalizeZero(num);
|
|
752
|
+
return normalized;
|
|
753
|
+
}
|
|
829
754
|
/**
|
|
830
755
|
* Reject implicit arithmetic-style coercion while preserving explicit
|
|
831
756
|
* stringification.
|
|
@@ -866,23 +791,59 @@ var DecimalImpl = class _DecimalImpl {
|
|
|
866
791
|
function toImpl(d) {
|
|
867
792
|
return d;
|
|
868
793
|
}
|
|
794
|
+
function coerceToImpl(value) {
|
|
795
|
+
if (isDecimal(value)) {
|
|
796
|
+
return toImpl(value);
|
|
797
|
+
}
|
|
798
|
+
return toImpl(Decimal.from(value));
|
|
799
|
+
}
|
|
869
800
|
function toDecimal(impl) {
|
|
870
801
|
return impl;
|
|
871
802
|
}
|
|
872
803
|
function isDecimal(value) {
|
|
873
|
-
return typeof value === "object" && value !== null && DECIMAL_BRAND in value
|
|
804
|
+
return typeof value === "object" && value !== null && DECIMAL_BRAND in value && // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- symbol key access requires cast
|
|
805
|
+
value[DECIMAL_BRAND] === true;
|
|
806
|
+
}
|
|
807
|
+
function assertIsDecimal(value) {
|
|
808
|
+
if (!isDecimal(value)) {
|
|
809
|
+
throw new Error(`Expected a Decimal, got ${typeof value}`);
|
|
810
|
+
}
|
|
874
811
|
}
|
|
875
812
|
var Decimal = {
|
|
876
813
|
/**
|
|
877
|
-
*
|
|
814
|
+
* Type guard that narrows an unknown value to {@link (Decimal:type)}.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
* ```ts
|
|
818
|
+
* if (Decimal.is(value)) {
|
|
819
|
+
* value.add(1); // value is Decimal
|
|
820
|
+
* }
|
|
821
|
+
* ```
|
|
822
|
+
* @public
|
|
823
|
+
*/
|
|
824
|
+
is: isDecimal,
|
|
825
|
+
/**
|
|
826
|
+
* Assertion guard that throws if the value is not a {@link (Decimal:type)}.
|
|
827
|
+
*
|
|
828
|
+
* @example
|
|
829
|
+
* ```ts
|
|
830
|
+
* Decimal.assert(value);
|
|
831
|
+
* value.add(1); // value is Decimal
|
|
832
|
+
* ```
|
|
833
|
+
* @public
|
|
834
|
+
*/
|
|
835
|
+
assert: assertIsDecimal,
|
|
836
|
+
/**
|
|
837
|
+
* Create a `Decimal` from a string, number, bigint, Integer, or Decimal.
|
|
878
838
|
*
|
|
879
839
|
* @remarks
|
|
840
|
+
* - **Decimal**: returned as-is (immutable passthrough).
|
|
880
841
|
* - **string**: Parsed as a decimal literal. Accepts an optional sign,
|
|
881
842
|
* integer digits, an optional fractional part, and an optional `e`/`E`
|
|
882
843
|
* exponent. Leading/trailing whitespace is trimmed.
|
|
883
|
-
* - **number
|
|
884
|
-
* then parsed, so `Decimal.from(0.1)`
|
|
885
|
-
* 53-bit binary approximation).
|
|
844
|
+
* - **number** (including Integer): Must be finite. Converted via
|
|
845
|
+
* `Number.prototype.toString()` then parsed, so `Decimal.from(0.1)`
|
|
846
|
+
* produces `"0.1"` (not the 53-bit binary approximation).
|
|
886
847
|
* - **bigint**: Treated as an integer with exponent 0.
|
|
887
848
|
*
|
|
888
849
|
* @example
|
|
@@ -891,16 +852,21 @@ var Decimal = {
|
|
|
891
852
|
* Decimal.from(42); // number
|
|
892
853
|
* Decimal.from(100n); // bigint
|
|
893
854
|
* Decimal.from('1.5e3'); // scientific notation → 1500
|
|
855
|
+
* Decimal.from(d); // Decimal passthrough
|
|
894
856
|
* ```
|
|
895
857
|
*
|
|
896
858
|
* @param value - The value to convert.
|
|
897
|
-
* @returns A new frozen `Decimal` instance
|
|
859
|
+
* @returns A new frozen `Decimal` instance (or the same instance if
|
|
860
|
+
* already a Decimal).
|
|
898
861
|
* @throws Error if `value` is a non-finite number, an empty
|
|
899
862
|
* string, or a string that does not match the decimal literal grammar.
|
|
900
863
|
*
|
|
901
864
|
* @public
|
|
902
865
|
*/
|
|
903
866
|
from(value) {
|
|
867
|
+
if (isDecimal(value)) {
|
|
868
|
+
return value;
|
|
869
|
+
}
|
|
904
870
|
if (typeof value === "bigint") {
|
|
905
871
|
return toDecimal(new DecimalImpl(value, 0));
|
|
906
872
|
}
|
|
@@ -949,6 +915,273 @@ var Decimal = {
|
|
|
949
915
|
zero: toDecimal(new DecimalImpl(0n, 0))
|
|
950
916
|
};
|
|
951
917
|
|
|
918
|
+
// src/stdlib/scalars.ts
|
|
919
|
+
function roundToInteger(value, direction) {
|
|
920
|
+
switch (direction) {
|
|
921
|
+
case "ceil":
|
|
922
|
+
return Math.ceil(value);
|
|
923
|
+
case "floor":
|
|
924
|
+
return Math.floor(value);
|
|
925
|
+
case "round-down":
|
|
926
|
+
return Math.trunc(value);
|
|
927
|
+
case "round-up":
|
|
928
|
+
return value >= 0 ? Math.ceil(value) : Math.floor(value);
|
|
929
|
+
case "half-up":
|
|
930
|
+
return value >= 0 ? Math.floor(value + 0.5) : Math.ceil(value - 0.5);
|
|
931
|
+
default: {
|
|
932
|
+
const _exhaustive = direction;
|
|
933
|
+
throw new Error(`Unknown rounding direction: ${String(_exhaustive)}`);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
function normalizeZero2(value) {
|
|
938
|
+
return Object.is(value, -0) ? 0 : value;
|
|
939
|
+
}
|
|
940
|
+
function assertIsInteger(value) {
|
|
941
|
+
if (!(typeof value === "number" && Number.isInteger(value))) {
|
|
942
|
+
throw new Error(
|
|
943
|
+
`Expected an integer, got ${typeof value === "number" ? String(value) : typeof value}`
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
function assertIsPositiveInteger(value) {
|
|
948
|
+
if (!(typeof value === "number" && Number.isInteger(value) && value >= 0)) {
|
|
949
|
+
throw new Error(
|
|
950
|
+
`Expected a non-negative integer, got ${typeof value === "number" ? String(value) : typeof value}`
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
function assertIntegerIsPositive(value) {
|
|
955
|
+
if (value < 0) {
|
|
956
|
+
throw new Error(`Expected a non-negative integer, got ${String(value)}`);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
var Integer = {
|
|
960
|
+
/**
|
|
961
|
+
* The `Integer` value representing zero.
|
|
962
|
+
*
|
|
963
|
+
* @remarks
|
|
964
|
+
* Pre-allocated singleton — prefer `Integer.zero` over
|
|
965
|
+
* `Integer.from(0, 'floor')` to avoid an unnecessary call.
|
|
966
|
+
*
|
|
967
|
+
* @public
|
|
968
|
+
*/
|
|
969
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-unsafe-type-assertion -- branded type construction: 0 is trivially an integer
|
|
970
|
+
zero: 0,
|
|
971
|
+
/**
|
|
972
|
+
* Type guard that narrows an unknown value to {@link (Integer:type)}.
|
|
973
|
+
*
|
|
974
|
+
* @example
|
|
975
|
+
* ```ts
|
|
976
|
+
* const n: unknown = getCount();
|
|
977
|
+
* if (Integer.is(n)) {
|
|
978
|
+
* // n is Integer here
|
|
979
|
+
* config.retryCount = n;
|
|
980
|
+
* }
|
|
981
|
+
* ```
|
|
982
|
+
* @public
|
|
983
|
+
*/
|
|
984
|
+
is: (value) => typeof value === "number" && Number.isInteger(value),
|
|
985
|
+
/**
|
|
986
|
+
* Assertion guard that throws if the value is not an {@link (Integer:type)}.
|
|
987
|
+
*
|
|
988
|
+
* @example
|
|
989
|
+
* ```ts
|
|
990
|
+
* const n: unknown = getCount();
|
|
991
|
+
* Integer.assert(n);
|
|
992
|
+
* // n is Integer here
|
|
993
|
+
* ```
|
|
994
|
+
* @public
|
|
995
|
+
*/
|
|
996
|
+
assert: assertIsInteger,
|
|
997
|
+
/**
|
|
998
|
+
* Coerces a value to an {@link (Integer:type)} by rounding.
|
|
999
|
+
*
|
|
1000
|
+
* @remarks
|
|
1001
|
+
* Accepts `number`, `string`, `Decimal`, or `Integer`. The rounding
|
|
1002
|
+
* direction is always required — no invisible defaults.
|
|
1003
|
+
*
|
|
1004
|
+
* - **number** (including Integer): must be finite and round to a
|
|
1005
|
+
* safe integer (`Number.isSafeInteger`). IEEE 754 negative zero is
|
|
1006
|
+
* normalized to positive zero.
|
|
1007
|
+
* - **string**: must be a valid numeric literal (not empty/whitespace).
|
|
1008
|
+
* Parsed via `Number()`, then rounded. The result must be a safe integer.
|
|
1009
|
+
* - **Decimal**: delegated to {@link Decimal.toInteger | Decimal.toInteger()}.
|
|
1010
|
+
*
|
|
1011
|
+
* @example
|
|
1012
|
+
* ```ts
|
|
1013
|
+
* Integer.from(9.99, 'floor'); // 9
|
|
1014
|
+
* Integer.from('1.5', 'ceil'); // 2
|
|
1015
|
+
* Integer.from(Decimal.from('2.7'), 'half-up'); // 3
|
|
1016
|
+
* ```
|
|
1017
|
+
*
|
|
1018
|
+
* @throws Error if the value is non-finite, an empty string, an
|
|
1019
|
+
* unparseable string, or rounds to a value outside the safe integer range.
|
|
1020
|
+
*
|
|
1021
|
+
* @public
|
|
1022
|
+
*/
|
|
1023
|
+
from(value, rounding) {
|
|
1024
|
+
if (typeof value === "number") {
|
|
1025
|
+
if (!Number.isFinite(value)) {
|
|
1026
|
+
throw new Error(`Cannot round non-finite value ${String(value)} to an integer`);
|
|
1027
|
+
}
|
|
1028
|
+
const rounded = normalizeZero2(roundToInteger(value, rounding));
|
|
1029
|
+
if (!Number.isSafeInteger(rounded)) {
|
|
1030
|
+
throw new Error(
|
|
1031
|
+
`Value ${String(value)} rounds to ${String(rounded)}, which is outside the safe integer range`
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
return rounded;
|
|
1035
|
+
}
|
|
1036
|
+
if (typeof value === "string") {
|
|
1037
|
+
if (value.trim() === "") {
|
|
1038
|
+
throw new Error("Cannot parse empty string as an integer");
|
|
1039
|
+
}
|
|
1040
|
+
const num = Number(value);
|
|
1041
|
+
if (!Number.isFinite(num)) {
|
|
1042
|
+
throw new Error(
|
|
1043
|
+
`Cannot parse "${value}" as a finite number for integer conversion`
|
|
1044
|
+
);
|
|
1045
|
+
}
|
|
1046
|
+
const rounded = normalizeZero2(roundToInteger(num, rounding));
|
|
1047
|
+
if (!Number.isSafeInteger(rounded)) {
|
|
1048
|
+
throw new Error(
|
|
1049
|
+
`Value "${value}" rounds to ${String(rounded)}, which is outside the safe integer range`
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
return rounded;
|
|
1053
|
+
}
|
|
1054
|
+
if (isDecimal(value)) {
|
|
1055
|
+
return value.toInteger(rounding);
|
|
1056
|
+
}
|
|
1057
|
+
throw new Error(
|
|
1058
|
+
`Cannot convert ${typeof value} to Integer; expected string, number, or Decimal`
|
|
1059
|
+
);
|
|
1060
|
+
},
|
|
1061
|
+
/**
|
|
1062
|
+
* Convert an {@link (Integer:type)} to a {@link (Decimal:type)}.
|
|
1063
|
+
*
|
|
1064
|
+
* @remarks
|
|
1065
|
+
* This conversion is lossless — every JavaScript integer is exactly
|
|
1066
|
+
* representable as a Decimal.
|
|
1067
|
+
*
|
|
1068
|
+
* @example
|
|
1069
|
+
* ```ts
|
|
1070
|
+
* const dec = Integer.toDecimal(Integer.from(42, 'floor'));
|
|
1071
|
+
* dec.add(Decimal.from('0.5')); // 42.5
|
|
1072
|
+
* ```
|
|
1073
|
+
* @public
|
|
1074
|
+
*/
|
|
1075
|
+
toDecimal(value) {
|
|
1076
|
+
return Decimal.from(value);
|
|
1077
|
+
},
|
|
1078
|
+
/**
|
|
1079
|
+
* Type guard that narrows an {@link (Integer:type)} to {@link (PositiveInteger:type)}.
|
|
1080
|
+
*
|
|
1081
|
+
* @example
|
|
1082
|
+
* ```ts
|
|
1083
|
+
* const n = Integer.from(count, 'floor');
|
|
1084
|
+
* if (Integer.isPositive(n)) {
|
|
1085
|
+
* // n is PositiveInteger here
|
|
1086
|
+
* }
|
|
1087
|
+
* ```
|
|
1088
|
+
* @public
|
|
1089
|
+
*/
|
|
1090
|
+
isPositive: (value) => value >= 0,
|
|
1091
|
+
/**
|
|
1092
|
+
* Assertion guard that throws if an {@link (Integer:type)} is not a {@link (PositiveInteger:type)}.
|
|
1093
|
+
*
|
|
1094
|
+
* @example
|
|
1095
|
+
* ```ts
|
|
1096
|
+
* const n = Integer.from(count, 'floor');
|
|
1097
|
+
* Integer.assertIsPositive(n);
|
|
1098
|
+
* // n is PositiveInteger here
|
|
1099
|
+
* ```
|
|
1100
|
+
* @public
|
|
1101
|
+
*/
|
|
1102
|
+
assertIsPositive: assertIntegerIsPositive
|
|
1103
|
+
};
|
|
1104
|
+
var PositiveInteger = {
|
|
1105
|
+
/**
|
|
1106
|
+
* Type guard that narrows an unknown value to {@link (PositiveInteger:type)}.
|
|
1107
|
+
*
|
|
1108
|
+
* @example
|
|
1109
|
+
* ```ts
|
|
1110
|
+
* const n: unknown = getRetryCount();
|
|
1111
|
+
* if (PositiveInteger.is(n)) {
|
|
1112
|
+
* // n is PositiveInteger here
|
|
1113
|
+
* config.maxRetries = n;
|
|
1114
|
+
* }
|
|
1115
|
+
* ```
|
|
1116
|
+
* @public
|
|
1117
|
+
*/
|
|
1118
|
+
is: (value) => typeof value === "number" && Number.isInteger(value) && value >= 0,
|
|
1119
|
+
/**
|
|
1120
|
+
* Assertion guard that throws if the value is not a {@link (PositiveInteger:type)}.
|
|
1121
|
+
*
|
|
1122
|
+
* @example
|
|
1123
|
+
* ```ts
|
|
1124
|
+
* const n: unknown = getRetryCount();
|
|
1125
|
+
* PositiveInteger.assert(n);
|
|
1126
|
+
* // n is PositiveInteger here
|
|
1127
|
+
* ```
|
|
1128
|
+
* @public
|
|
1129
|
+
*/
|
|
1130
|
+
assert: assertIsPositiveInteger,
|
|
1131
|
+
/**
|
|
1132
|
+
* Coerces a value to a {@link (PositiveInteger:type)} by rounding.
|
|
1133
|
+
*
|
|
1134
|
+
* @remarks
|
|
1135
|
+
* Delegates to {@link (Integer:variable).from | Integer.from()} for
|
|
1136
|
+
* rounding, then checks the result is non-negative. All error
|
|
1137
|
+
* conditions from `Integer.from()` apply (non-finite, empty string,
|
|
1138
|
+
* unsafe integer range), plus an additional check that the rounded
|
|
1139
|
+
* result is ≥ 0. IEEE 754 negative zero is normalized to positive zero.
|
|
1140
|
+
*
|
|
1141
|
+
* @example
|
|
1142
|
+
* ```ts
|
|
1143
|
+
* PositiveInteger.from(2.7, 'floor'); // 2
|
|
1144
|
+
* PositiveInteger.from('1.5', 'ceil'); // 2
|
|
1145
|
+
* PositiveInteger.from(Decimal.from('3.2'), 'half-up'); // 3
|
|
1146
|
+
* ```
|
|
1147
|
+
*
|
|
1148
|
+
* @throws Error if the value is non-finite, unparseable, outside the
|
|
1149
|
+
* safe integer range, or rounds to a negative number.
|
|
1150
|
+
*
|
|
1151
|
+
* @public
|
|
1152
|
+
*/
|
|
1153
|
+
from(value, rounding) {
|
|
1154
|
+
const rounded = Integer.from(value, rounding);
|
|
1155
|
+
const normalized = normalizeZero2(rounded);
|
|
1156
|
+
if (normalized < 0) {
|
|
1157
|
+
throw new Error(
|
|
1158
|
+
`Value ${String(value)} rounds to ${String(normalized)}, which is negative`
|
|
1159
|
+
);
|
|
1160
|
+
}
|
|
1161
|
+
return normalized;
|
|
1162
|
+
}
|
|
1163
|
+
};
|
|
1164
|
+
var StreetAddress = {
|
|
1165
|
+
create: (address) => {
|
|
1166
|
+
return address;
|
|
1167
|
+
}
|
|
1168
|
+
};
|
|
1169
|
+
var Timestamp = {
|
|
1170
|
+
create: (value) => {
|
|
1171
|
+
return value;
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
|
|
1175
|
+
// src/stdlib/refs.ts
|
|
1176
|
+
var Ref = {
|
|
1177
|
+
create: (step) => {
|
|
1178
|
+
return {
|
|
1179
|
+
type: step.object,
|
|
1180
|
+
id: step.id
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
|
|
952
1185
|
// src/stdlib/types.ts
|
|
953
1186
|
var WireReadError = class extends Error {
|
|
954
1187
|
/**
|
|
@@ -1481,6 +1714,23 @@ function _applyConfig(descriptor, inputObject, appCtx) {
|
|
|
1481
1714
|
const strategy = appCtx?.clockTime !== void 0 ? createJsonWireToType(appCtx) : _JsonWireToType;
|
|
1482
1715
|
return _apply(descriptor, strategy, inputObject);
|
|
1483
1716
|
}
|
|
1717
|
+
|
|
1718
|
+
// src/stdlib/to-const.ts
|
|
1719
|
+
function toConst(value) {
|
|
1720
|
+
if (value === null || typeof value !== "object") {
|
|
1721
|
+
return value;
|
|
1722
|
+
}
|
|
1723
|
+
if (Array.isArray(value)) {
|
|
1724
|
+
for (const item of value) {
|
|
1725
|
+
toConst(item);
|
|
1726
|
+
}
|
|
1727
|
+
return Object.freeze(value);
|
|
1728
|
+
}
|
|
1729
|
+
for (const key of Object.keys(value)) {
|
|
1730
|
+
toConst(value[key]);
|
|
1731
|
+
}
|
|
1732
|
+
return Object.freeze(value);
|
|
1733
|
+
}
|
|
1484
1734
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1485
1735
|
0 && (module.exports = {
|
|
1486
1736
|
DEFAULT_DIV_PRECISION,
|
|
@@ -1515,5 +1765,6 @@ function _applyConfig(descriptor, inputObject, appCtx) {
|
|
|
1515
1765
|
_translateMap,
|
|
1516
1766
|
_translateShape,
|
|
1517
1767
|
_translateUnion,
|
|
1518
|
-
isDecimal
|
|
1768
|
+
isDecimal,
|
|
1769
|
+
toConst
|
|
1519
1770
|
});
|