ts-data-forge 9.0.0 → 9.0.1
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/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-creation.mjs +17 -112
- package/dist/array/impl/array-utils-creation.mjs.map +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts.map +1 -1
- package/dist/array/impl/array-utils-element-access.mjs +22 -61
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/array/impl/array-utils-iterators.d.mts.map +1 -1
- package/dist/array/impl/array-utils-iterators.mjs +9 -63
- package/dist/array/impl/array-utils-iterators.mjs.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs +78 -78
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +95 -89
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-search.mjs +100 -92
- package/dist/array/impl/array-utils-search.mjs.map +1 -1
- package/dist/array/impl/array-utils-set-op.d.mts.map +1 -1
- package/dist/array/impl/array-utils-set-op.mjs +31 -141
- package/dist/array/impl/array-utils-set-op.mjs.map +1 -1
- package/dist/array/impl/array-utils-size.d.mts.map +1 -1
- package/dist/array/impl/array-utils-size.mjs +4 -8
- package/dist/array/impl/array-utils-size.mjs.map +1 -1
- package/dist/array/impl/array-utils-slice-clamped.mjs +11 -12
- package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs +44 -96
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +150 -292
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-validation.mjs +18 -169
- package/dist/array/impl/array-utils-validation.mjs.map +1 -1
- package/dist/collections/imap-mapped.d.mts.map +1 -1
- package/dist/collections/imap-mapped.mjs +243 -330
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.d.mts.map +1 -1
- package/dist/collections/imap.mjs +190 -275
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/iset-mapped.d.mts +1 -1
- package/dist/collections/iset-mapped.d.mts.map +1 -1
- package/dist/collections/iset-mapped.mjs +222 -444
- package/dist/collections/iset-mapped.mjs.map +1 -1
- package/dist/collections/iset.d.mts +1 -1
- package/dist/collections/iset.d.mts.map +1 -1
- package/dist/collections/iset.mjs +158 -324
- package/dist/collections/iset.mjs.map +1 -1
- package/dist/collections/queue.d.mts.map +1 -1
- package/dist/collections/queue.mjs +106 -178
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.d.mts.map +1 -1
- package/dist/collections/stack.mjs +95 -165
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/expect-type.d.mts +1 -1
- package/dist/expect-type.d.mts.map +1 -1
- package/dist/expect-type.mjs +1 -69
- package/dist/expect-type.mjs.map +1 -1
- package/dist/functional/match.mjs +9 -9
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-expect-to-be.mjs +13 -14
- package/dist/functional/optional/impl/optional-expect-to-be.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-filter.mjs +14 -15
- package/dist/functional/optional/impl/optional-filter.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-flat-map.mjs +10 -11
- package/dist/functional/optional/impl/optional-flat-map.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-from-nullable.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-from-nullable.mjs +1 -25
- package/dist/functional/optional/impl/optional-from-nullable.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-none.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-none.mjs +0 -19
- package/dist/functional/optional/impl/optional-is-none.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-optional.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-optional.mjs +1 -23
- package/dist/functional/optional/impl/optional-is-optional.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-some.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-some.mjs +0 -20
- package/dist/functional/optional/impl/optional-is-some.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-map.mjs +13 -14
- package/dist/functional/optional/impl/optional-map.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-none.mjs +0 -16
- package/dist/functional/optional/impl/optional-none.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-or-else.mjs +10 -11
- package/dist/functional/optional/impl/optional-or-else.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-some.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-some.mjs +2 -21
- package/dist/functional/optional/impl/optional-some.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-to-nullable.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-to-nullable.mjs +1 -27
- package/dist/functional/optional/impl/optional-to-nullable.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-or.mjs +9 -10
- package/dist/functional/optional/impl/optional-unwrap-or.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs +4 -32
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap.mjs +4 -4
- package/dist/functional/optional/impl/optional-unwrap.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-zip.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-zip.mjs +1 -35
- package/dist/functional/optional/impl/optional-zip.mjs.map +1 -1
- package/dist/functional/optional/impl/tag.mjs +2 -3
- package/dist/functional/optional/impl/tag.mjs.map +1 -1
- package/dist/functional/pipe.d.mts.map +1 -1
- package/dist/functional/pipe.mjs +9 -34
- package/dist/functional/pipe.mjs.map +1 -1
- package/dist/functional/result/impl/result-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-err.mjs +2 -27
- package/dist/functional/result/impl/result-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-expect-to-be.mjs +13 -15
- package/dist/functional/result/impl/result-expect-to-be.mjs.map +1 -1
- package/dist/functional/result/impl/result-flat-map.mjs +16 -16
- package/dist/functional/result/impl/result-flat-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-fold.mjs +16 -16
- package/dist/functional/result/impl/result-fold.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-promise.mjs +4 -31
- package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-throwable.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-throwable.mjs +8 -38
- package/dist/functional/result/impl/result-from-throwable.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-err.mjs +0 -19
- package/dist/functional/result/impl/result-is-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-ok.mjs +0 -19
- package/dist/functional/result/impl/result-is-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-result.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-result.mjs +1 -23
- package/dist/functional/result/impl/result-is-result.mjs.map +1 -1
- package/dist/functional/result/impl/result-map-err.mjs +16 -16
- package/dist/functional/result/impl/result-map-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-map.mjs +16 -16
- package/dist/functional/result/impl/result-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-ok.mjs +2 -27
- package/dist/functional/result/impl/result-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-or-else.mjs +10 -12
- package/dist/functional/result/impl/result-or-else.mjs.map +1 -1
- package/dist/functional/result/impl/result-swap.d.mts.map +1 -1
- package/dist/functional/result/impl/result-swap.mjs +4 -22
- package/dist/functional/result/impl/result-swap.mjs.map +1 -1
- package/dist/functional/result/impl/result-to-optional.d.mts.map +1 -1
- package/dist/functional/result/impl/result-to-optional.mjs +0 -27
- package/dist/functional/result/impl/result-to-optional.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-or.mjs +9 -11
- package/dist/functional/result/impl/result-unwrap-err-or.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs +13 -40
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err.mjs +4 -32
- package/dist/functional/result/impl/result-unwrap-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs +9 -11
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok.mjs +4 -4
- package/dist/functional/result/impl/result-unwrap-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.mjs +4 -35
- package/dist/functional/result/impl/result-unwrap-throw.mjs.map +1 -1
- package/dist/functional/result/impl/result-zip.d.mts.map +1 -1
- package/dist/functional/result/impl/result-zip.mjs +1 -34
- package/dist/functional/result/impl/result-zip.mjs.map +1 -1
- package/dist/functional/result/impl/tag.mjs +2 -3
- package/dist/functional/result/impl/tag.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/tag.mjs +3 -5
- package/dist/functional/ternary-result/impl/tag.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs +2 -15
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs +13 -13
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs +27 -26
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs +21 -22
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +4 -20
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +7 -25
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs +0 -17
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +0 -19
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +1 -25
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +0 -18
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs +13 -15
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs +18 -18
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs +21 -22
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs +2 -16
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +4 -22
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +12 -27
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +4 -19
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs +4 -5
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +15 -27
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs +9 -9
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +10 -24
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +4 -19
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs +3 -19
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +14 -45
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/variant-name.d.mts +1 -1
- package/dist/functional/ternary-result/impl/variant-name.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/variant-name.mjs +8 -9
- package/dist/functional/ternary-result/impl/variant-name.mjs.map +1 -1
- package/dist/guard/has-key.d.mts.map +1 -1
- package/dist/guard/has-key.mjs +0 -41
- package/dist/guard/has-key.mjs.map +1 -1
- package/dist/guard/is-length-bounded-string.d.mts.map +1 -1
- package/dist/guard/is-length-bounded-string.mjs +0 -157
- package/dist/guard/is-length-bounded-string.mjs.map +1 -1
- package/dist/guard/is-non-empty-string.d.mts +1 -1
- package/dist/guard/is-non-empty-string.d.mts.map +1 -1
- package/dist/guard/is-non-empty-string.mjs +1 -38
- package/dist/guard/is-non-empty-string.mjs.map +1 -1
- package/dist/guard/is-non-null-object.d.mts.map +1 -1
- package/dist/guard/is-non-null-object.mjs +1 -35
- package/dist/guard/is-non-null-object.mjs.map +1 -1
- package/dist/guard/is-primitive.d.mts.map +1 -1
- package/dist/guard/is-primitive.mjs +12 -43
- package/dist/guard/is-primitive.mjs.map +1 -1
- package/dist/guard/is-record.d.mts.map +1 -1
- package/dist/guard/is-record.mjs +0 -86
- package/dist/guard/is-record.mjs.map +1 -1
- package/dist/guard/is-type.d.mts.map +1 -1
- package/dist/guard/is-type.mjs +12 -399
- package/dist/guard/is-type.mjs.map +1 -1
- package/dist/guard/key-is-in.d.mts.map +1 -1
- package/dist/guard/key-is-in.mjs +0 -40
- package/dist/guard/key-is-in.mjs.map +1 -1
- package/dist/iterator/range.mjs +16 -16
- package/dist/iterator/range.mjs.map +1 -1
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +52 -340
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/branded-types/finite-number.d.mts +3 -3
- package/dist/number/branded-types/finite-number.mjs +135 -154
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +4 -4
- package/dist/number/branded-types/int.mjs +205 -245
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.d.mts +15 -15
- package/dist/number/branded-types/int16.mjs +116 -123
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +11 -11
- package/dist/number/branded-types/int32.mjs +116 -123
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +4 -4
- package/dist/number/branded-types/negative-finite-number.mjs +129 -144
- package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +5 -5
- package/dist/number/branded-types/negative-int.mjs +94 -113
- package/dist/number/branded-types/negative-int.mjs.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +5 -5
- package/dist/number/branded-types/negative-safe-int.mjs +103 -124
- package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +5 -5
- package/dist/number/branded-types/non-negative-finite-number.mjs +136 -142
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +16 -16
- package/dist/number/branded-types/non-negative-int16.mjs +109 -118
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +12 -12
- package/dist/number/branded-types/non-negative-int32.mjs +110 -119
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +3 -3
- package/dist/number/branded-types/non-positive-finite-number.mjs +120 -124
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-int.d.mts +4 -4
- package/dist/number/branded-types/non-positive-int.mjs +167 -212
- package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.d.mts +4 -4
- package/dist/number/branded-types/non-positive-safe-int.mjs +95 -106
- package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +4 -4
- package/dist/number/branded-types/non-zero-finite-number.mjs +114 -123
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.d.mts +2 -2
- package/dist/number/branded-types/non-zero-int.mjs +72 -83
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.d.mts +7 -7
- package/dist/number/branded-types/non-zero-int16.mjs +96 -107
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +5 -5
- package/dist/number/branded-types/non-zero-int32.mjs +97 -108
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +2 -2
- package/dist/number/branded-types/non-zero-safe-int.mjs +100 -111
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +8 -8
- package/dist/number/branded-types/non-zero-uint16.mjs +96 -107
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +6 -6
- package/dist/number/branded-types/non-zero-uint32.mjs +96 -107
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +8 -8
- package/dist/number/branded-types/positive-finite-number.mjs +139 -141
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +11 -11
- package/dist/number/branded-types/positive-int.mjs +228 -271
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +19 -19
- package/dist/number/branded-types/positive-int16.mjs +109 -118
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +15 -15
- package/dist/number/branded-types/positive-int32.mjs +109 -118
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +11 -11
- package/dist/number/branded-types/positive-safe-int.mjs +244 -275
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +17 -17
- package/dist/number/branded-types/positive-uint16.mjs +109 -118
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +13 -13
- package/dist/number/branded-types/positive-uint32.mjs +110 -119
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +4 -4
- package/dist/number/branded-types/safe-int.mjs +251 -292
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +8 -8
- package/dist/number/branded-types/safe-uint.mjs +111 -119
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +8 -8
- package/dist/number/branded-types/uint.mjs +198 -225
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +14 -14
- package/dist/number/branded-types/uint16.mjs +109 -119
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +10 -10
- package/dist/number/branded-types/uint32.mjs +110 -117
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.mjs +119 -145
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.mjs +107 -121
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/number/num.d.mts +2 -2
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +76 -537
- package/dist/number/num.mjs.map +1 -1
- package/dist/number/refined-number-utils.d.mts +1 -1
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs +127 -263
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +107 -222
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/cast-mutable.d.mts.map +1 -1
- package/dist/others/cast-mutable.mjs +8 -37
- package/dist/others/cast-mutable.mjs.map +1 -1
- package/dist/others/cast-readonly.d.mts.map +1 -1
- package/dist/others/cast-readonly.mjs +8 -36
- package/dist/others/cast-readonly.mjs.map +1 -1
- package/dist/others/debounce-function.d.mts.map +1 -1
- package/dist/others/debounce-function.mjs +7 -16
- package/dist/others/debounce-function.mjs.map +1 -1
- package/dist/others/fast-deep-equal.d.mts.map +1 -1
- package/dist/others/fast-deep-equal.mjs +43 -59
- package/dist/others/fast-deep-equal.mjs.map +1 -1
- package/dist/others/if-then.d.mts.map +1 -1
- package/dist/others/if-then.mjs +0 -29
- package/dist/others/if-then.mjs.map +1 -1
- package/dist/others/map-nullable.mjs +9 -9
- package/dist/others/map-nullable.mjs.map +1 -1
- package/dist/others/memoize-function.mjs +12 -12
- package/dist/others/memoize-function.mjs.map +1 -1
- package/dist/others/tuple.d.mts.map +1 -1
- package/dist/others/tuple.mjs +0 -50
- package/dist/others/tuple.mjs.map +1 -1
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs +41 -88
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/dist/promise/promise.d.mts.map +1 -1
- package/dist/promise/promise.mjs +4 -49
- package/dist/promise/promise.mjs.map +1 -1
- package/package.json +8 -7
package/dist/number/num.mjs
CHANGED
|
@@ -2,552 +2,91 @@ import { err } from '../functional/result/impl/result-err.mjs';
|
|
|
2
2
|
import { ok } from '../functional/result/impl/result-ok.mjs';
|
|
3
3
|
import '@sindresorhus/is';
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* Namespace providing utility functions for number manipulation and validation.
|
|
7
|
-
*
|
|
8
|
-
* This namespace offers a comprehensive set of type-safe number utilities
|
|
9
|
-
* including:
|
|
10
|
-
*
|
|
11
|
-
* - Type conversion and validation
|
|
12
|
-
* - Type guards for numeric constraints (non-zero, non-negative, positive)
|
|
13
|
-
* - Range checking and clamping operations
|
|
14
|
-
* - Mathematical operations with type safety
|
|
15
|
-
* - Rounding utilities
|
|
16
|
-
*
|
|
17
|
-
* Many functions in this namespace leverage TypeScript's type system to provide
|
|
18
|
-
* compile-time guarantees about numeric constraints.
|
|
19
|
-
*/
|
|
20
5
|
var Num;
|
|
21
|
-
(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
*
|
|
61
|
-
* ```ts
|
|
62
|
-
* assert.strictEqual(
|
|
63
|
-
* Result.unwrapOkOr(Num.safeParseInt('123'), Number.NaN),
|
|
64
|
-
* 123,
|
|
65
|
-
* );
|
|
66
|
-
*
|
|
67
|
-
* assert.strictEqual(
|
|
68
|
-
* Result.unwrapOkOr(Num.safeParseInt('12.9'), Number.NaN),
|
|
69
|
-
* 12,
|
|
70
|
-
* );
|
|
71
|
-
*
|
|
72
|
-
* assert.strictEqual(
|
|
73
|
-
* Result.unwrapOkOr(Num.safeParseInt('-12.9'), Number.NaN),
|
|
74
|
-
* -12,
|
|
75
|
-
* );
|
|
76
|
-
*
|
|
77
|
-
* assert.strictEqual(Number.parseInt('-12.9', 10), -12);
|
|
78
|
-
*
|
|
79
|
-
* // Native `parseInt` ignores trailing non-numeric characters
|
|
80
|
-
*
|
|
81
|
-
* assert.strictEqual(Number.parseInt('123abc', 10), 123);
|
|
82
|
-
*
|
|
83
|
-
* assert.isTrue(Number.isNaN(Number('123abc')));
|
|
84
|
-
*
|
|
85
|
-
* assert.isTrue(Result.isErr(Num.safeParseInt('123abc')));
|
|
86
|
-
*
|
|
87
|
-
* // Whitespace is not a valid integer, so we return an error instead of coercing to 0.
|
|
88
|
-
*
|
|
89
|
-
* assert.isTrue(Number.isNaN(Number.parseInt(' ', 10)));
|
|
90
|
-
*
|
|
91
|
-
* assert.strictEqual(Number(' '), 0); // Native `Number` coerces whitespace to 0
|
|
92
|
-
*
|
|
93
|
-
* assert.isTrue(Result.isErr(Num.safeParseInt('')));
|
|
94
|
-
*
|
|
95
|
-
* assert.strictEqual(Result.unwrapOk(Num.safeParseInt(' ')), undefined);
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* @param s The string to parse.
|
|
99
|
-
* @returns `Result.ok(parsedInt)` for valid input, otherwise `Result.err`
|
|
100
|
-
* wrapping an `Error` describing the invalid input.
|
|
101
|
-
*/
|
|
102
|
-
Num.safeParseInt = (s) => {
|
|
103
|
-
const viaNumber = Number(s);
|
|
104
|
-
// `Number('')` / `Number(' ')` は 0 を返すが、`parseInt` は NaN を返す。
|
|
105
|
-
// 末尾不正文字 ('12abc' 等) は `Number` 側が NaN にするので、両者が共に
|
|
106
|
-
// 有効な場合のみ採用することで空文字・空白のみ・末尾不正をまとめて弾く。
|
|
107
|
-
return Number.isNaN(viaNumber) || Number.isNaN(Number.parseInt(s, 10))
|
|
108
|
-
? err(new Error(`safeParseInt: "${s}" is not a valid base-10 integer`))
|
|
109
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
110
|
-
ok(Math.trunc(viaNumber));
|
|
111
|
-
};
|
|
112
|
-
/**
|
|
113
|
-
* Safely parses a finite floating-point number from a string, returning a
|
|
114
|
-
* {@link Result} that is `Ok<FiniteNumber>` for valid input and `Err<Error>`
|
|
115
|
-
* otherwise.
|
|
116
|
-
*
|
|
117
|
-
* This is a stricter alternative to both `parseFloat` and `Number`:
|
|
118
|
-
*
|
|
119
|
-
* - Unlike `parseFloat('12abc')` (which returns `12`), trailing non-numeric
|
|
120
|
-
* characters make the whole input invalid and yield `Err`.
|
|
121
|
-
* - Unlike `Number('')` / `Number(' ')` (which return `0`), empty or
|
|
122
|
-
* whitespace-only input yields `Err`.
|
|
123
|
-
* - Unlike `Number('Infinity')` (which returns `Infinity`), non-finite values
|
|
124
|
-
* yield `Err`.
|
|
125
|
-
*
|
|
126
|
-
* The empty-string case is rejected by delegating to `parseFloat` (which
|
|
127
|
-
* returns `NaN` there) rather than hard-coding a check, while the trailing-
|
|
128
|
-
* garbage case is rejected via `Number`. Decimal values are preserved as-is,
|
|
129
|
-
* so `'12.9'` stays `12.9`.
|
|
130
|
-
*
|
|
131
|
-
* Use `Result.unwrapOk` (optionally with a `?? Number.NaN` fallback) or
|
|
132
|
-
* `Result.unwrapOkOr` to get a plain number back.
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
*
|
|
136
|
-
* ```ts
|
|
137
|
-
* assert.strictEqual(
|
|
138
|
-
* Result.unwrapOkOr(Num.safeParseFloat('12.9'), Number.NaN),
|
|
139
|
-
* 12.9,
|
|
140
|
-
* );
|
|
141
|
-
*
|
|
142
|
-
* assert.strictEqual(
|
|
143
|
-
* Result.unwrapOkOr(Num.safeParseFloat('-3.5'), Number.NaN),
|
|
144
|
-
* -3.5,
|
|
145
|
-
* );
|
|
146
|
-
*
|
|
147
|
-
* assert.strictEqual(
|
|
148
|
-
* Result.unwrapOkOr(Num.safeParseFloat('1e3'), Number.NaN),
|
|
149
|
-
* 1000,
|
|
150
|
-
* );
|
|
151
|
-
*
|
|
152
|
-
* // Native `parseFloat` ignores trailing non-numeric characters
|
|
153
|
-
*
|
|
154
|
-
* assert.strictEqual(Number.parseFloat('12px'), 12);
|
|
155
|
-
*
|
|
156
|
-
* assert.isTrue(Result.isErr(Num.safeParseFloat('12px')));
|
|
157
|
-
*
|
|
158
|
-
* // Whitespace is not a valid number, so we return an error instead of coercing to 0.
|
|
159
|
-
*
|
|
160
|
-
* assert.isTrue(Result.isErr(Num.safeParseFloat('')));
|
|
161
|
-
*
|
|
162
|
-
* assert.isTrue(Result.isErr(Num.safeParseFloat(' ')));
|
|
163
|
-
*
|
|
164
|
-
* // Infinity and NaN are not finite, so they are rejected.
|
|
165
|
-
*
|
|
166
|
-
* assert.isTrue(Result.isErr(Num.safeParseFloat('Infinity')));
|
|
167
|
-
*
|
|
168
|
-
* assert.isTrue(Result.isErr(Num.safeParseFloat('NaN')));
|
|
169
|
-
* ```
|
|
170
|
-
*
|
|
171
|
-
* @param s The string to parse.
|
|
172
|
-
* @returns `Result.ok(parsedFloat)` for valid finite input, otherwise
|
|
173
|
-
* `Result.err` wrapping an `Error` describing the invalid input.
|
|
174
|
-
*/
|
|
175
|
-
Num.safeParseFloat = (s) => {
|
|
176
|
-
const viaNumber = Number(s);
|
|
177
|
-
// `Number('')` / `Number(' ')` は 0 を返すが、`parseFloat` は NaN を返す。
|
|
178
|
-
// 末尾不正文字 ('12abc' 等) は `Number` 側が NaN にするので、両者が共に
|
|
179
|
-
// 非 NaN かつ有限の場合のみ採用することで空文字・空白のみ・末尾不正・
|
|
180
|
-
// Infinity をまとめて弾く。
|
|
181
|
-
return Number.isNaN(viaNumber) ||
|
|
182
|
-
!Number.isFinite(viaNumber) ||
|
|
183
|
-
Number.isNaN(Number.parseFloat(s))
|
|
184
|
-
? err(new Error(`safeParseFloat: "${s}" is not a valid finite number`))
|
|
185
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
186
|
-
ok(viaNumber);
|
|
187
|
-
};
|
|
188
|
-
/**
|
|
189
|
-
* Type guard that checks if a number is non-zero.
|
|
190
|
-
*
|
|
191
|
-
* When this function returns `true`, TypeScript narrows the type to exclude
|
|
192
|
-
* zero, providing compile-time safety for division operations and other
|
|
193
|
-
* calculations that require non-zero values.
|
|
194
|
-
*
|
|
195
|
-
* @example
|
|
196
|
-
*
|
|
197
|
-
* ```ts
|
|
198
|
-
* const value: number = 5;
|
|
199
|
-
*
|
|
200
|
-
* if (Num.isNonZero(value)) {
|
|
201
|
-
* // Safe to divide now that we know value is non-zero
|
|
202
|
-
* // eslint-disable-next-line total-functions/no-partial-division
|
|
203
|
-
* const inverted = 1 / value;
|
|
204
|
-
*
|
|
205
|
-
* assert.isTrue(inverted === 0.2);
|
|
206
|
-
* }
|
|
207
|
-
*
|
|
208
|
-
* assert.isFalse(Num.isNonZero(0));
|
|
209
|
-
* ```
|
|
210
|
-
*
|
|
211
|
-
* @template N - The numeric literal type or number type to check
|
|
212
|
-
* @param num - The number to check
|
|
213
|
-
* @returns `true` if the number is not zero, `false` otherwise
|
|
214
|
-
*/
|
|
215
|
-
Num.isNonZero = (num) => num !== 0;
|
|
216
|
-
/**
|
|
217
|
-
* Type guard that checks if a number is non-negative (greater than or equal
|
|
218
|
-
* to zero).
|
|
219
|
-
*
|
|
220
|
-
* When this function returns `true`, TypeScript narrows the type to exclude
|
|
221
|
-
* negative values, which is useful for operations that require non-negative
|
|
222
|
-
* inputs like array indices or measurements.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
*
|
|
226
|
-
* ```ts
|
|
227
|
-
* const candidate = 10;
|
|
228
|
-
*
|
|
229
|
-
* if (Num.isNonNegative(candidate)) {
|
|
230
|
-
* const index: number = candidate;
|
|
231
|
-
*
|
|
232
|
-
* assert.isTrue(index === 10);
|
|
233
|
-
* }
|
|
234
|
-
*
|
|
235
|
-
* assert.isFalse(Num.isNonNegative(-1));
|
|
236
|
-
* ```
|
|
237
|
-
*
|
|
238
|
-
* @template N - The numeric literal type or number type to check
|
|
239
|
-
* @param num - The number to check
|
|
240
|
-
* @returns `true` if the number is >= 0, `false` otherwise
|
|
241
|
-
*/
|
|
242
|
-
Num.isNonNegative = (num) => num >= 0;
|
|
243
|
-
/**
|
|
244
|
-
* Type guard that checks if a number is positive (greater than zero).
|
|
245
|
-
*
|
|
246
|
-
* When this function returns `true`, TypeScript narrows the type to exclude
|
|
247
|
-
* zero and negative values. This is particularly useful for validating inputs
|
|
248
|
-
* that must be strictly positive, such as dimensions, counts, or rates.
|
|
249
|
-
*
|
|
250
|
-
* @example
|
|
251
|
-
*
|
|
252
|
-
* ```ts
|
|
253
|
-
* const amount = 42;
|
|
254
|
-
*
|
|
255
|
-
* if (Num.isPositive(amount)) {
|
|
256
|
-
* assert.isTrue(amount > 0);
|
|
257
|
-
* }
|
|
258
|
-
*
|
|
259
|
-
* assert.isFalse(Num.isPositive(0));
|
|
260
|
-
* ```
|
|
261
|
-
*
|
|
262
|
-
* @template N - The numeric literal type or number type to check
|
|
263
|
-
* @param num - The number to check
|
|
264
|
-
* @returns `true` if the number is > 0, `false` otherwise
|
|
265
|
-
*/
|
|
266
|
-
Num.isPositive = (num) => num > 0;
|
|
267
|
-
/**
|
|
268
|
-
* Creates a function that checks if a number `x` is within the range
|
|
269
|
-
* `lowerBound <= x < upperBound`.
|
|
270
|
-
*
|
|
271
|
-
* @example
|
|
272
|
-
*
|
|
273
|
-
* ```ts
|
|
274
|
-
* const isGrade = Num.isInRange(0, 100);
|
|
275
|
-
*
|
|
276
|
-
* assert.isTrue(isGrade(50));
|
|
277
|
-
*
|
|
278
|
-
* assert.isFalse(isGrade(100));
|
|
279
|
-
* ```
|
|
280
|
-
*
|
|
281
|
-
* @param lowerBound The lower bound (inclusive).
|
|
282
|
-
* @param upperBound The upper bound (exclusive).
|
|
283
|
-
* @returns A function that takes a number `x` and returns `true` if `x` is in
|
|
284
|
-
* the range, `false` otherwise.
|
|
285
|
-
*/
|
|
286
|
-
Num.isInRange = (lowerBound, upperBound) => (x) => lowerBound <= x && x < upperBound;
|
|
287
|
-
/**
|
|
288
|
-
* Creates a function that checks if a number `x` is within the range
|
|
289
|
-
* `lowerBound <= x <= upperBound`.
|
|
290
|
-
*
|
|
291
|
-
* @example
|
|
292
|
-
*
|
|
293
|
-
* ```ts
|
|
294
|
-
* const isPercentage = Num.isInRangeInclusive(0, 100);
|
|
295
|
-
*
|
|
296
|
-
* assert.isTrue(isPercentage(100));
|
|
297
|
-
*
|
|
298
|
-
* assert.isFalse(isPercentage(-1));
|
|
299
|
-
* ```
|
|
300
|
-
*
|
|
301
|
-
* @param lowerBound The lower bound (inclusive).
|
|
302
|
-
* @param upperBound The upper bound (inclusive).
|
|
303
|
-
* @returns A function that takes a number `x` and returns `true` if `x` is in
|
|
304
|
-
* the range, `false` otherwise.
|
|
305
|
-
*/
|
|
306
|
-
Num.isInRangeInclusive = (lowerBound, upperBound) => (x) => lowerBound <= x && x <= upperBound;
|
|
307
|
-
/**
|
|
308
|
-
* Creates a type guard that checks if a number is an unsigned integer within
|
|
309
|
-
* a specified range.
|
|
310
|
-
*
|
|
311
|
-
* This function returns a predicate that validates whether a number is:
|
|
312
|
-
*
|
|
313
|
-
* - A safe integer (no floating point)
|
|
314
|
-
* - Within the range [lowerBound, upperBound)
|
|
315
|
-
*
|
|
316
|
-
* The returned type guard provides precise type narrowing when the bounds are
|
|
317
|
-
* SmallUint literals, making it ideal for array index validation.
|
|
318
|
-
*
|
|
319
|
-
* @example
|
|
320
|
-
*
|
|
321
|
-
* ```ts
|
|
322
|
-
* const indexGuard = Num.isUintInRange(0, 5);
|
|
323
|
-
*
|
|
324
|
-
* assert.isTrue(indexGuard(3));
|
|
325
|
-
*
|
|
326
|
-
* assert.isFalse(indexGuard(5));
|
|
327
|
-
*
|
|
328
|
-
* assert.isFalse(indexGuard(-1));
|
|
329
|
-
* ```
|
|
330
|
-
*
|
|
331
|
-
* @template L - The lower bound as a SmallUint literal type
|
|
332
|
-
* @template U - The upper bound as a SmallUint literal type
|
|
333
|
-
* @param lowerBound - The minimum value (inclusive)
|
|
334
|
-
* @param upperBound - The maximum value (exclusive)
|
|
335
|
-
* @returns A type guard function that validates and narrows number types
|
|
336
|
-
*/
|
|
337
|
-
Num.isUintInRange = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x < upperBound;
|
|
338
|
-
/**
|
|
339
|
-
* Creates a type guard that checks if a number is an unsigned integer within
|
|
340
|
-
* a specified inclusive range.
|
|
341
|
-
*
|
|
342
|
-
* This function returns a predicate that validates whether a number is:
|
|
343
|
-
*
|
|
344
|
-
* - A safe integer (no floating point)
|
|
345
|
-
* - Within the range [lowerBound, upperBound] (both bounds inclusive)
|
|
346
|
-
*
|
|
347
|
-
* The returned type guard provides precise type narrowing when the bounds are
|
|
348
|
-
* SmallUint literals, useful for validating scores, percentages, or other
|
|
349
|
-
* bounded values.
|
|
350
|
-
*
|
|
351
|
-
* @example
|
|
352
|
-
*
|
|
353
|
-
* ```ts
|
|
354
|
-
* const inclusiveGuard = Num.isUintInRangeInclusive(0, 5);
|
|
355
|
-
*
|
|
356
|
-
* assert.isTrue(inclusiveGuard(5));
|
|
357
|
-
*
|
|
358
|
-
* assert.isFalse(inclusiveGuard(6));
|
|
359
|
-
* ```
|
|
360
|
-
*
|
|
361
|
-
* @template L - The lower bound as a SmallUint literal type
|
|
362
|
-
* @template U - The upper bound as a SmallUint literal type
|
|
363
|
-
* @param lowerBound - The minimum value (inclusive)
|
|
364
|
-
* @param upperBound - The maximum value (inclusive)
|
|
365
|
-
* @returns A type guard function that validates and narrows number types
|
|
366
|
-
*/
|
|
367
|
-
Num.isUintInRangeInclusive = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x <= upperBound;
|
|
368
|
-
function clamp(...args) {
|
|
369
|
-
switch (args.length) {
|
|
370
|
-
case 3: {
|
|
371
|
-
const [target, lowerBound, upperBound] = args;
|
|
372
|
-
return !Number.isFinite(target)
|
|
373
|
-
? lowerBound
|
|
374
|
-
: Math.max(lowerBound, Math.min(upperBound, target));
|
|
375
|
-
}
|
|
376
|
-
case 2: {
|
|
377
|
-
const [lowerBound, upperBound] = args;
|
|
378
|
-
return (target) => clamp(target, lowerBound, upperBound);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
6
|
+
((Num2) => {
|
|
7
|
+
Num2.from = Number;
|
|
8
|
+
Num2.safeParseInt = (s) => {
|
|
9
|
+
const viaNumber = Number(s);
|
|
10
|
+
return Number.isNaN(viaNumber) || Number.isNaN(Number.parseInt(s, 10)) ? err(
|
|
11
|
+
new Error(`safeParseInt: "${s}" is not a valid base-10 integer`)
|
|
12
|
+
) : (
|
|
13
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
14
|
+
ok(Math.trunc(viaNumber))
|
|
15
|
+
);
|
|
16
|
+
};
|
|
17
|
+
Num2.safeParseFloat = (s) => {
|
|
18
|
+
const viaNumber = Number(s);
|
|
19
|
+
return Number.isNaN(viaNumber) || !Number.isFinite(viaNumber) || Number.isNaN(Number.parseFloat(s)) ? err(
|
|
20
|
+
new Error(`safeParseFloat: "${s}" is not a valid finite number`)
|
|
21
|
+
) : (
|
|
22
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
23
|
+
ok(viaNumber)
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
Num2.isNonZero = (num) => num !== 0;
|
|
27
|
+
Num2.isNonNegative = (num) => num >= 0;
|
|
28
|
+
Num2.isPositive = (num) => num > 0;
|
|
29
|
+
Num2.isInRange = (lowerBound, upperBound) => (x) => lowerBound <= x && x < upperBound;
|
|
30
|
+
Num2.isInRangeInclusive = (lowerBound, upperBound) => (x) => lowerBound <= x && x <= upperBound;
|
|
31
|
+
Num2.isUintInRange = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x < upperBound;
|
|
32
|
+
Num2.isUintInRangeInclusive = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x <= upperBound;
|
|
33
|
+
function clamp(...args) {
|
|
34
|
+
switch (args.length) {
|
|
35
|
+
case 3: {
|
|
36
|
+
const [target, lowerBound, upperBound] = args;
|
|
37
|
+
return !Number.isFinite(target) ? lowerBound : Math.max(lowerBound, Math.min(upperBound, target));
|
|
38
|
+
}
|
|
39
|
+
case 2: {
|
|
40
|
+
const [lowerBound, upperBound] = args;
|
|
41
|
+
return (target) => clamp(target, lowerBound, upperBound);
|
|
42
|
+
}
|
|
381
43
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
*
|
|
386
|
-
* The sign of the result is computed exactly (e.g. a non-negative plus a
|
|
387
|
-
* non-negative is non-negative); the integer/finite level is preserved, with
|
|
388
|
-
* `SafeInt` operands widening to `Int` because the sum may exceed the safe
|
|
389
|
-
* range. No clamping is performed — for a result that stays inside a bounded
|
|
390
|
-
* type, use that type's branded `add` instead.
|
|
391
|
-
*
|
|
392
|
-
* @param a - The first addend.
|
|
393
|
-
* @param b - The second addend.
|
|
394
|
-
* @returns `a + b`, typed as the tightest branded type guaranteed to contain
|
|
395
|
-
* it.
|
|
396
|
-
*/
|
|
397
|
-
Num.add = (a, b) =>
|
|
44
|
+
}
|
|
45
|
+
Num2.clamp = clamp;
|
|
46
|
+
Num2.add = (a, b) => (
|
|
398
47
|
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
*
|
|
403
|
-
* The sign is computed exactly; the level is preserved (with `SafeInt`
|
|
404
|
-
* widening to `Int`). No clamping — for saturating subtraction that stays in a
|
|
405
|
-
* bounded type (e.g. `Uint.sub` clamping at `0`), use that type's branded
|
|
406
|
-
* `sub`.
|
|
407
|
-
*
|
|
408
|
-
* @param a - The minuend.
|
|
409
|
-
* @param b - The subtrahend.
|
|
410
|
-
* @returns `a - b`, typed as the tightest branded type guaranteed to contain
|
|
411
|
-
* it.
|
|
412
|
-
*/
|
|
413
|
-
Num.sub = (a, b) =>
|
|
48
|
+
a + b
|
|
49
|
+
);
|
|
50
|
+
Num2.sub = (a, b) => (
|
|
414
51
|
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
*
|
|
419
|
-
* The sign follows the usual product rule (e.g. negative times negative is
|
|
420
|
-
* positive); the level is preserved (with `SafeInt` widening to `Int`). No
|
|
421
|
-
* clamping.
|
|
422
|
-
*
|
|
423
|
-
* @param a - The first factor.
|
|
424
|
-
* @param b - The second factor.
|
|
425
|
-
* @returns `a * b`, typed as the tightest branded type guaranteed to contain
|
|
426
|
-
* it.
|
|
427
|
-
*/
|
|
428
|
-
Num.mul = (a, b) =>
|
|
52
|
+
a - b
|
|
53
|
+
);
|
|
54
|
+
Num2.mul = (a, b) => (
|
|
429
55
|
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
*
|
|
434
|
-
* The divisor must be typed as `NonZeroNumber` or a non-zero numeric literal.
|
|
435
|
-
* The result sign follows the quotient rule; because exact division can be
|
|
436
|
-
* fractional, the level is `finite` (or `number`).
|
|
437
|
-
*
|
|
438
|
-
* @param a - The dividend.
|
|
439
|
-
* @param b - The divisor (must be non-zero, enforced by types).
|
|
440
|
-
* @returns `a / b`, typed as the tightest branded type guaranteed to contain
|
|
441
|
-
* it.
|
|
442
|
-
*/
|
|
443
|
-
Num.div = (a, b) =>
|
|
56
|
+
a * b
|
|
57
|
+
);
|
|
58
|
+
Num2.div = (a, b) => (
|
|
444
59
|
// eslint-disable-next-line total-functions/no-partial-division, total-functions/no-unsafe-type-assertion
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
};
|
|
465
|
-
/**
|
|
466
|
-
* Rounds a number to a specified number of decimal places.
|
|
467
|
-
*
|
|
468
|
-
* Uses the standard rounding algorithm (round half up) to round the number to
|
|
469
|
-
* the given precision. The precision must be a positive safe integer.
|
|
470
|
-
*
|
|
471
|
-
* @param num - The number to round
|
|
472
|
-
* @param precision - The number of decimal places (must be positive)
|
|
473
|
-
* @returns The rounded number
|
|
474
|
-
*/
|
|
475
|
-
Num.roundAt = (num, precision) => {
|
|
476
|
-
const digit = 10 ** precision;
|
|
477
|
-
// eslint-disable-next-line total-functions/no-partial-division
|
|
478
|
-
return Math.round(num * digit) / digit;
|
|
479
|
-
};
|
|
480
|
-
/**
|
|
481
|
-
* Rounds a number to the nearest integer using bitwise operations.
|
|
482
|
-
*
|
|
483
|
-
* This function uses a bitwise OR trick for potentially faster rounding.
|
|
484
|
-
* Note: This implementation rounds half up for positive numbers but may
|
|
485
|
-
* behave differently for negative numbers compared to Math.round.
|
|
486
|
-
*
|
|
487
|
-
* @param num - The number to round
|
|
488
|
-
* @returns The rounded integer as an Int branded type
|
|
489
|
-
*/
|
|
60
|
+
a / b
|
|
61
|
+
);
|
|
62
|
+
Num2.divInt = (a, b) => {
|
|
63
|
+
const quotient = Math.floor(Math.floor(a) / Math.floor(b));
|
|
64
|
+
return quotient;
|
|
65
|
+
};
|
|
66
|
+
Num2.roundAt = (num, precision) => {
|
|
67
|
+
const digit = 10 ** precision;
|
|
68
|
+
return Math.round(num * digit) / digit;
|
|
69
|
+
};
|
|
70
|
+
Num2.roundToInt = (num) => Math.trunc(num + 0.5);
|
|
71
|
+
Num2.round = (digit) => {
|
|
72
|
+
const powAmount = 10 ** digit;
|
|
73
|
+
return (target) => (
|
|
74
|
+
// eslint-disable-next-line total-functions/no-partial-division
|
|
75
|
+
(0, Num2.roundToInt)(powAmount * target) / powAmount
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
Num2.mapNaN2Undefined = (num) => Number.isNaN(num) ? void 0 : (
|
|
490
79
|
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
*
|
|
495
|
-
* This is a curried version of roundAt that returns a function configured to
|
|
496
|
-
* always round to the specified number of decimal places. Useful for creating
|
|
497
|
-
* consistent rounding behavior across multiple values.
|
|
498
|
-
*
|
|
499
|
-
* @param digit - The number of decimal places for rounding
|
|
500
|
-
* @returns A function that rounds numbers to the specified precision
|
|
501
|
-
*/
|
|
502
|
-
Num.round = (digit) => {
|
|
503
|
-
const powAmount = 10 ** digit;
|
|
504
|
-
return (target) =>
|
|
505
|
-
// eslint-disable-next-line total-functions/no-partial-division
|
|
506
|
-
Num.roundToInt(powAmount * target) / powAmount;
|
|
507
|
-
};
|
|
508
|
-
/**
|
|
509
|
-
* Converts NaN values to undefined while preserving all other numbers.
|
|
510
|
-
*
|
|
511
|
-
* This function is useful for handling potentially invalid numeric operations
|
|
512
|
-
* in a type-safe way, converting NaN results to undefined for easier handling
|
|
513
|
-
* with optional chaining or nullish coalescing.
|
|
514
|
-
*
|
|
515
|
-
* @template N - The numeric type (literal or number)
|
|
516
|
-
* @param num - The number to check
|
|
517
|
-
* @returns The original number if not NaN, otherwise undefined
|
|
518
|
-
*/
|
|
519
|
-
Num.mapNaN2Undefined = (num) => Number.isNaN(num)
|
|
520
|
-
? undefined
|
|
521
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
522
|
-
num;
|
|
523
|
-
/**
|
|
524
|
-
* Type-safe increment operation for SmallUint values.
|
|
525
|
-
*
|
|
526
|
-
* Increments a SmallUint (0-40) by 1 with the result type computed at compile
|
|
527
|
-
* time. This provides type-level arithmetic for small unsigned integers,
|
|
528
|
-
* useful for type-safe counter operations.
|
|
529
|
-
*
|
|
530
|
-
* @template N - A SmallUint literal type (0-40)
|
|
531
|
-
* @param n - The SmallUint value to increment
|
|
532
|
-
* @returns The incremented value with type Increment<N>
|
|
533
|
-
*/
|
|
534
|
-
Num.increment = (n) =>
|
|
80
|
+
num
|
|
81
|
+
);
|
|
82
|
+
Num2.increment = (n) => (
|
|
535
83
|
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
*
|
|
540
|
-
* Decrements a positive SmallInt (1-40) by 1 with the result type computed at
|
|
541
|
-
* compile time. This provides type-level arithmetic for small positive
|
|
542
|
-
* integers, useful for type-safe countdown operations.
|
|
543
|
-
*
|
|
544
|
-
* @template N - A positive SmallInt literal type (1-40)
|
|
545
|
-
* @param n - The positive SmallInt value to decrement
|
|
546
|
-
* @returns The decremented value with type Decrement<N>
|
|
547
|
-
*/
|
|
548
|
-
Num.decrement = (n) =>
|
|
84
|
+
n + 1
|
|
85
|
+
);
|
|
86
|
+
Num2.decrement = (n) => (
|
|
549
87
|
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
550
|
-
|
|
88
|
+
n - 1
|
|
89
|
+
);
|
|
551
90
|
})(Num || (Num = {}));
|
|
552
91
|
|
|
553
92
|
export { Num };
|