ts-data-forge 3.2.0 → 3.3.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/README.md +45 -21
- package/dist/array/impl/array-utils-creation.d.mts +116 -0
- package/dist/array/impl/array-utils-creation.d.mts.map +1 -0
- package/dist/array/impl/array-utils-creation.mjs +110 -0
- package/dist/array/impl/array-utils-creation.mjs.map +1 -0
- package/dist/array/impl/array-utils-element-access.d.mts +61 -0
- package/dist/array/impl/array-utils-element-access.d.mts.map +1 -0
- package/dist/array/impl/array-utils-element-access.mjs +66 -0
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -0
- package/dist/array/impl/array-utils-iterators.d.mts +59 -0
- package/dist/array/impl/array-utils-iterators.d.mts.map +1 -0
- package/dist/array/impl/array-utils-iterators.mjs +104 -0
- package/dist/array/impl/array-utils-iterators.mjs.map +1 -0
- package/dist/array/impl/array-utils-modification.d.mts +154 -0
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -0
- package/dist/array/impl/array-utils-modification.mjs +139 -0
- package/dist/array/impl/array-utils-modification.mjs.map +1 -0
- package/dist/array/impl/array-utils-reducing-value.d.mts +214 -0
- package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -0
- package/dist/array/impl/array-utils-reducing-value.mjs +160 -0
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -0
- package/dist/array/impl/array-utils-search.d.mts +179 -0
- package/dist/array/impl/array-utils-search.d.mts.map +1 -0
- package/dist/array/impl/array-utils-search.mjs +153 -0
- package/dist/array/impl/array-utils-search.mjs.map +1 -0
- package/dist/array/impl/array-utils-set-op.d.mts +100 -0
- package/dist/array/impl/array-utils-set-op.d.mts.map +1 -0
- package/dist/array/impl/array-utils-set-op.mjs +137 -0
- package/dist/array/impl/array-utils-set-op.mjs.map +1 -0
- package/dist/array/impl/array-utils-size.d.mts +24 -0
- package/dist/array/impl/array-utils-size.d.mts.map +1 -0
- package/dist/array/impl/array-utils-size.mjs +28 -0
- package/dist/array/impl/array-utils-size.mjs.map +1 -0
- package/dist/array/impl/array-utils-slice-clamped.d.mts +18 -0
- package/dist/array/impl/array-utils-slice-clamped.d.mts.map +1 -0
- package/dist/array/impl/array-utils-slice-clamped.mjs +49 -0
- package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -0
- package/dist/array/impl/array-utils-slicing.d.mts +120 -0
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -0
- package/dist/array/impl/array-utils-slicing.mjs +140 -0
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -0
- package/dist/array/impl/array-utils-transformation.d.mts +348 -0
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -0
- package/dist/array/impl/array-utils-transformation.mjs +331 -0
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -0
- package/dist/array/impl/array-utils-validation.d.mts +149 -0
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -0
- package/dist/array/impl/array-utils-validation.mjs +166 -0
- package/dist/array/impl/array-utils-validation.mjs.map +1 -0
- package/dist/array/impl/index.d.mts +13 -0
- package/dist/array/impl/index.d.mts.map +1 -0
- package/dist/array/impl/index.mjs +13 -0
- package/dist/array/impl/index.mjs.map +1 -0
- package/dist/array/index.d.mts +1 -1
- package/dist/array/index.d.mts.map +1 -1
- package/dist/array/index.mjs +2 -1
- package/dist/array/index.mjs.map +1 -1
- package/dist/collections/imap-mapped.d.mts +83 -253
- package/dist/collections/imap-mapped.d.mts.map +1 -1
- package/dist/collections/imap-mapped.mjs +33 -164
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.d.mts +436 -163
- package/dist/collections/imap.d.mts.map +1 -1
- package/dist/collections/imap.mjs +74 -94
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/iset-mapped.d.mts +828 -345
- package/dist/collections/iset-mapped.d.mts.map +1 -1
- package/dist/collections/iset-mapped.mjs +200 -242
- package/dist/collections/iset-mapped.mjs.map +1 -1
- package/dist/collections/iset.d.mts +397 -205
- package/dist/collections/iset.d.mts.map +1 -1
- package/dist/collections/iset.mjs +102 -184
- package/dist/collections/iset.mjs.map +1 -1
- package/dist/collections/queue.d.mts +155 -135
- package/dist/collections/queue.d.mts.map +1 -1
- package/dist/collections/queue.mjs +55 -156
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.d.mts +154 -154
- package/dist/collections/stack.d.mts.map +1 -1
- package/dist/collections/stack.mjs +54 -203
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/entry-point.d.mts +3 -0
- package/dist/entry-point.d.mts.map +1 -0
- package/dist/entry-point.mjs +62 -0
- package/dist/entry-point.mjs.map +1 -0
- package/dist/expect-type.d.mts +43 -172
- package/dist/expect-type.d.mts.map +1 -1
- package/dist/expect-type.mjs +43 -172
- package/dist/expect-type.mjs.map +1 -1
- package/dist/functional/match.d.mts +35 -140
- package/dist/functional/match.d.mts.map +1 -1
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional.d.mts +282 -160
- package/dist/functional/optional.d.mts.map +1 -1
- package/dist/functional/optional.mjs +131 -71
- package/dist/functional/optional.mjs.map +1 -1
- package/dist/functional/pipe.d.mts +59 -113
- package/dist/functional/pipe.d.mts.map +1 -1
- package/dist/functional/pipe.mjs.map +1 -1
- package/dist/functional/result.d.mts +433 -332
- package/dist/functional/result.d.mts.map +1 -1
- package/dist/functional/result.mjs +233 -239
- package/dist/functional/result.mjs.map +1 -1
- package/dist/globals.d.mts +12 -5
- package/dist/guard/has-key.d.mts +23 -74
- package/dist/guard/has-key.d.mts.map +1 -1
- package/dist/guard/has-key.mjs +23 -74
- package/dist/guard/has-key.mjs.map +1 -1
- package/dist/guard/is-non-empty-string.d.mts +20 -87
- package/dist/guard/is-non-empty-string.d.mts.map +1 -1
- package/dist/guard/is-non-empty-string.mjs +20 -87
- package/dist/guard/is-non-empty-string.mjs.map +1 -1
- package/dist/guard/is-non-null-object.d.mts +14 -84
- package/dist/guard/is-non-null-object.d.mts.map +1 -1
- package/dist/guard/is-non-null-object.mjs +14 -84
- package/dist/guard/is-non-null-object.mjs.map +1 -1
- package/dist/guard/is-primitive.d.mts +13 -126
- package/dist/guard/is-primitive.d.mts.map +1 -1
- package/dist/guard/is-primitive.mjs +13 -126
- package/dist/guard/is-primitive.mjs.map +1 -1
- package/dist/guard/is-record.d.mts +21 -132
- package/dist/guard/is-record.d.mts.map +1 -1
- package/dist/guard/is-record.mjs +21 -132
- package/dist/guard/is-record.mjs.map +1 -1
- package/dist/guard/is-type.d.mts +201 -238
- package/dist/guard/is-type.d.mts.map +1 -1
- package/dist/guard/is-type.mjs +201 -238
- package/dist/guard/is-type.mjs.map +1 -1
- package/dist/guard/key-is-in.d.mts +22 -139
- package/dist/guard/key-is-in.d.mts.map +1 -1
- package/dist/guard/key-is-in.mjs +22 -139
- package/dist/guard/key-is-in.mjs.map +1 -1
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -1
- package/dist/index.mjs.map +1 -1
- package/dist/iterator/range.d.mts +29 -62
- package/dist/iterator/range.d.mts.map +1 -1
- package/dist/iterator/range.mjs.map +1 -1
- package/dist/json/json.d.mts +191 -121
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +238 -136
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/branded-types/finite-number.d.mts +24 -156
- package/dist/number/branded-types/finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/finite-number.mjs +27 -159
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +122 -120
- package/dist/number/branded-types/int.d.mts.map +1 -1
- package/dist/number/branded-types/int.mjs +122 -120
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.d.mts +22 -30
- package/dist/number/branded-types/int16.d.mts.map +1 -1
- package/dist/number/branded-types/int16.mjs +22 -30
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +22 -31
- package/dist/number/branded-types/int32.d.mts.map +1 -1
- package/dist/number/branded-types/int32.mjs +22 -31
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +28 -36
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs +31 -39
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +24 -34
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs +24 -34
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +26 -35
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs +26 -35
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +22 -37
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.mjs +25 -40
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.d.mts +15 -30
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int.mjs +15 -30
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.d.mts +27 -35
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs +27 -35
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +29 -36
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs +29 -36
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +37 -38
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +39 -40
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +24 -35
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs +24 -35
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +24 -35
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs +24 -35
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +26 -40
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs +29 -43
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +133 -123
- package/dist/number/branded-types/positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +133 -123
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +24 -35
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int16.mjs +24 -35
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +24 -35
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int32.mjs +24 -35
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +159 -33
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +160 -34
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +24 -35
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint16.mjs +24 -35
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +26 -36
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint32.mjs +26 -36
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +140 -99
- package/dist/number/branded-types/safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +142 -101
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +24 -33
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
- package/dist/number/branded-types/safe-uint.mjs +25 -34
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +121 -30
- package/dist/number/branded-types/uint.d.mts.map +1 -1
- package/dist/number/branded-types/uint.mjs +121 -30
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +26 -34
- package/dist/number/branded-types/uint16.d.mts.map +1 -1
- package/dist/number/branded-types/uint16.mjs +26 -34
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +26 -68
- package/dist/number/branded-types/uint32.d.mts.map +1 -1
- package/dist/number/branded-types/uint32.mjs +26 -68
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.d.mts +37 -101
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/int8.mjs +39 -170
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.d.mts +45 -55
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/number/enum/uint8.mjs +46 -155
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/number/num.d.mts +145 -206
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +143 -199
- package/dist/number/num.mjs.map +1 -1
- package/dist/number/refined-number-utils.d.mts +97 -21
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs +91 -20
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/object/object.d.mts +126 -208
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +68 -102
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/cast-mutable.d.mts +12 -88
- package/dist/others/cast-mutable.d.mts.map +1 -1
- package/dist/others/cast-mutable.mjs +13 -89
- package/dist/others/cast-mutable.mjs.map +1 -1
- package/dist/others/cast-readonly.d.mts +12 -168
- package/dist/others/cast-readonly.d.mts.map +1 -1
- package/dist/others/cast-readonly.mjs +13 -169
- package/dist/others/cast-readonly.mjs.map +1 -1
- package/dist/others/if-then.d.mts +6 -83
- package/dist/others/if-then.d.mts.map +1 -1
- package/dist/others/if-then.mjs +6 -83
- package/dist/others/if-then.mjs.map +1 -1
- package/dist/others/map-nullable.d.mts +12 -136
- package/dist/others/map-nullable.d.mts.map +1 -1
- package/dist/others/map-nullable.mjs.map +1 -1
- package/dist/others/memoize-function.d.mts +14 -157
- package/dist/others/memoize-function.d.mts.map +1 -1
- package/dist/others/memoize-function.mjs +14 -157
- package/dist/others/memoize-function.mjs.map +1 -1
- package/dist/others/tuple.d.mts +33 -151
- package/dist/others/tuple.d.mts.map +1 -1
- package/dist/others/tuple.mjs +33 -151
- package/dist/others/tuple.mjs.map +1 -1
- package/dist/others/unknown-to-string.d.mts +11 -125
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs +14 -127
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/dist/promise/promise.d.mts +33 -20
- package/dist/promise/promise.d.mts.map +1 -1
- package/dist/promise/promise.mjs +34 -21
- package/dist/promise/promise.mjs.map +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +54 -50
- package/src/array/impl/array-utils-creation.mts +192 -0
- package/src/array/{array-utils-creation.test.mts → impl/array-utils-creation.test.mts} +121 -72
- package/src/array/impl/array-utils-element-access.mts +115 -0
- package/src/array/impl/array-utils-element-access.test.mts +151 -0
- package/src/array/impl/array-utils-iterators.mts +79 -0
- package/src/array/impl/array-utils-iterators.test.mts +98 -0
- package/src/array/impl/array-utils-modification.mts +434 -0
- package/src/array/{array-utils-modification.test.mts → impl/array-utils-modification.test.mts} +41 -28
- package/src/array/{array-utils-overload-type-error.test.mts → impl/array-utils-overload-type-error.test.mts} +33 -33
- package/src/array/impl/array-utils-reducing-value.mts +551 -0
- package/src/array/{array-utils-reducing-value.test.mts → impl/array-utils-reducing-value.test.mts} +45 -50
- package/src/array/impl/array-utils-search.mts +509 -0
- package/src/array/impl/array-utils-search.test.mts +346 -0
- package/src/array/impl/array-utils-set-op.mts +166 -0
- package/src/array/{array-utils-set-op.test.mts → impl/array-utils-set-op.test.mts} +42 -35
- package/src/array/impl/array-utils-size.mts +30 -0
- package/src/array/impl/array-utils-size.test.mts +9 -0
- package/src/array/impl/array-utils-slice-clamped.mts +51 -0
- package/src/array/{array-utils-slice-clamped.test.mts → impl/array-utils-slice-clamped.test.mts} +12 -12
- package/src/array/impl/array-utils-slicing.mts +275 -0
- package/src/array/impl/array-utils-slicing.test.mts +158 -0
- package/src/array/impl/array-utils-transformation.mts +746 -0
- package/src/array/{array-utils-transformation.test.mts → impl/array-utils-transformation.test.mts} +662 -889
- package/src/array/impl/array-utils-validation.mts +241 -0
- package/src/array/{array-utils-validation.test.mts → impl/array-utils-validation.test.mts} +194 -107
- package/src/array/{array.test.mts → impl/array.test.mts} +2 -2
- package/src/array/impl/index.mts +12 -0
- package/src/array/index.mts +1 -1
- package/src/collections/imap-mapped.mts +99 -265
- package/src/collections/imap.mts +477 -174
- package/src/collections/imap.test.mts +12 -19
- package/src/collections/iset-mapped.mts +892 -358
- package/src/collections/iset.mts +429 -213
- package/src/collections/queue.mts +174 -200
- package/src/collections/stack.mts +172 -245
- package/src/collections/stack.test.mts +9 -1
- package/src/entry-point.mts +2 -0
- package/src/expect-type.mts +43 -172
- package/src/functional/match.mts +35 -145
- package/src/functional/optional.mts +285 -163
- package/src/functional/optional.test.mts +4 -1
- package/src/functional/pipe.mts +60 -113
- package/src/functional/result.mts +452 -351
- package/src/functional/result.test.mts +9 -2
- package/src/globals.d.mts +12 -5
- package/src/guard/has-key.mts +23 -74
- package/src/guard/is-non-empty-string.mts +20 -87
- package/src/guard/is-non-null-object.mts +14 -84
- package/src/guard/is-non-null-object.test.mts +1 -1
- package/src/guard/is-primitive.mts +13 -126
- package/src/guard/is-primitive.test.mts +1 -1
- package/src/guard/is-record.mts +21 -132
- package/src/guard/is-record.test.mts +0 -1
- package/src/guard/is-type.mts +201 -238
- package/src/guard/is-type.test.mts +7 -7
- package/src/guard/key-is-in.mts +22 -139
- package/src/index.mts +0 -1
- package/src/iterator/range.mts +29 -62
- package/src/json/json.mts +202 -134
- package/src/json/json.test.mts +1 -3
- package/src/number/branded-types/finite-number.mts +27 -159
- package/src/number/branded-types/int.mts +122 -120
- package/src/number/branded-types/int16.mts +22 -30
- package/src/number/branded-types/int16.test.mts +24 -24
- package/src/number/branded-types/int32.mts +22 -31
- package/src/number/branded-types/int32.test.mts +39 -39
- package/src/number/branded-types/non-negative-finite-number.mts +31 -39
- package/src/number/branded-types/non-negative-int16.mts +24 -34
- package/src/number/branded-types/non-negative-int16.test.mts +16 -16
- package/src/number/branded-types/non-negative-int32.mts +26 -35
- package/src/number/branded-types/non-negative-int32.test.mts +30 -30
- package/src/number/branded-types/non-zero-finite-number.mts +25 -40
- package/src/number/branded-types/non-zero-int.mts +15 -30
- package/src/number/branded-types/non-zero-int16.mts +27 -35
- package/src/number/branded-types/non-zero-int16.test.mts +26 -26
- package/src/number/branded-types/non-zero-int32.mts +29 -36
- package/src/number/branded-types/non-zero-int32.test.mts +45 -42
- package/src/number/branded-types/non-zero-safe-int.mts +39 -40
- package/src/number/branded-types/non-zero-uint16.mts +24 -35
- package/src/number/branded-types/non-zero-uint16.test.mts +16 -16
- package/src/number/branded-types/non-zero-uint32.mts +24 -35
- package/src/number/branded-types/non-zero-uint32.test.mts +28 -28
- package/src/number/branded-types/positive-finite-number.mts +29 -43
- package/src/number/branded-types/positive-int.mts +134 -124
- package/src/number/branded-types/positive-int16.mts +24 -35
- package/src/number/branded-types/positive-int16.test.mts +14 -14
- package/src/number/branded-types/positive-int32.mts +24 -35
- package/src/number/branded-types/positive-int32.test.mts +26 -26
- package/src/number/branded-types/positive-safe-int.mts +160 -34
- package/src/number/branded-types/positive-uint16.mts +24 -35
- package/src/number/branded-types/positive-uint16.test.mts +16 -16
- package/src/number/branded-types/positive-uint32.mts +26 -36
- package/src/number/branded-types/positive-uint32.test.mts +31 -28
- package/src/number/branded-types/safe-int.mts +142 -101
- package/src/number/branded-types/safe-uint.mts +25 -34
- package/src/number/branded-types/uint.mts +121 -30
- package/src/number/branded-types/uint16.mts +26 -34
- package/src/number/branded-types/uint16.test.mts +16 -16
- package/src/number/branded-types/uint32.mts +26 -68
- package/src/number/branded-types/uint32.test.mts +28 -28
- package/src/number/enum/int8.mts +39 -170
- package/src/number/enum/uint8.mts +46 -155
- package/src/number/num.mts +157 -212
- package/src/number/num.test.mts +4 -4
- package/src/number/refined-number-utils.mts +109 -26
- package/src/object/object.mts +130 -212
- package/src/object/object.test.mts +29 -0
- package/src/others/cast-mutable.mts +13 -89
- package/src/others/cast-mutable.test.mts +80 -0
- package/src/others/cast-readonly.mts +13 -169
- package/src/others/if-then.mts +6 -83
- package/src/others/map-nullable.mts +12 -136
- package/src/others/map-nullable.test.mts +6 -6
- package/src/others/memoize-function.mts +14 -157
- package/src/others/tuple.mts +33 -151
- package/src/others/unknown-to-string.mts +15 -127
- package/src/others/unknown-to-string.test.mts +14 -2
- package/src/promise/promise.mts +34 -21
- package/src/promise/promise.test.mts +43 -0
- package/dist/array/array-utils.d.mts +0 -2956
- package/dist/array/array-utils.d.mts.map +0 -1
- package/dist/array/array-utils.mjs +0 -1838
- package/dist/array/array-utils.mjs.map +0 -1
- package/src/array/array-utils-search.test.mts +0 -169
- package/src/array/array-utils-slicing.test.mts +0 -274
- package/src/array/array-utils.mts +0 -4834
|
@@ -371,7 +371,10 @@ describe('Result', () => {
|
|
|
371
371
|
describe('flatMap', () => {
|
|
372
372
|
test('should chain operations that return Result', () => {
|
|
373
373
|
const divide = (a: number, b: number): Result<number, string> =>
|
|
374
|
-
b === 0
|
|
374
|
+
b === 0
|
|
375
|
+
? Result.err('Division by zero')
|
|
376
|
+
: // eslint-disable-next-line total-functions/no-partial-division
|
|
377
|
+
Result.ok(a / b);
|
|
375
378
|
|
|
376
379
|
const result = Result.flatMap(Result.ok(10), (x) => divide(x, 2));
|
|
377
380
|
expect(Result.unwrapOk(result)).toBe(5);
|
|
@@ -394,6 +397,7 @@ describe('Result', () => {
|
|
|
394
397
|
};
|
|
395
398
|
|
|
396
399
|
const divide = (a: number, b: number): Result<number, string> =>
|
|
400
|
+
// eslint-disable-next-line total-functions/no-partial-division
|
|
397
401
|
b === 0 ? Result.err('Division by zero') : Result.ok(a / b);
|
|
398
402
|
|
|
399
403
|
const result = Result.flatMap(
|
|
@@ -405,7 +409,10 @@ describe('Result', () => {
|
|
|
405
409
|
|
|
406
410
|
test('should support curried form', () => {
|
|
407
411
|
const divide = (a: number, b: number): Result<number, string> =>
|
|
408
|
-
b === 0
|
|
412
|
+
b === 0
|
|
413
|
+
? Result.err('Division by zero')
|
|
414
|
+
: // eslint-disable-next-line total-functions/no-partial-division
|
|
415
|
+
Result.ok(a / b);
|
|
409
416
|
|
|
410
417
|
const divideBy2 = Result.flatMap((x: number) => divide(x, 2));
|
|
411
418
|
|
package/src/globals.d.mts
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
/* eslint-disable import/unambiguous */
|
|
2
|
-
|
|
3
1
|
/// <reference types="ts-type-forge" />
|
|
4
2
|
|
|
5
3
|
type SmallPositiveInt = WithSmallInt<PositiveInt>;
|
|
6
4
|
|
|
7
|
-
/**
|
|
8
|
-
* Represents the type of keys that can be used in a standard JavaScript Map.
|
|
9
|
-
*/
|
|
5
|
+
/** Represents the type of keys that can be used in a standard JavaScript Map. */
|
|
10
6
|
type MapSetKeyType = Primitive;
|
|
11
7
|
|
|
8
|
+
type ArrayIndex<Ar extends readonly unknown[]> =
|
|
9
|
+
IsFixedLengthList<Ar> extends true ? IndexOfTuple<Ar> : SizeType.Arr;
|
|
10
|
+
|
|
11
|
+
type ArgArrayIndex<Ar extends readonly unknown[]> =
|
|
12
|
+
IsFixedLengthList<Ar> extends true ? IndexOfTuple<Ar> : SizeType.ArgArr;
|
|
13
|
+
|
|
14
|
+
type ArgArrayIndexWithNegative<Ar extends readonly unknown[]> =
|
|
15
|
+
IsFixedLengthList<Ar> extends true
|
|
16
|
+
? IndexOfTuple<[...Ar, 0]> | NegativeIndexOfTuple<Ar>
|
|
17
|
+
: SizeType.ArgArrWithNegative;
|
|
18
|
+
|
|
12
19
|
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/length
|
|
13
20
|
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/length
|
|
14
21
|
// Max array length : 2^32 - 1
|
package/src/guard/has-key.mts
CHANGED
|
@@ -1,91 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Type guard function that checks if an object has a specific key as its own
|
|
2
|
+
* Type guard function that checks if an object has a specific key as its own
|
|
3
|
+
* property.
|
|
3
4
|
*
|
|
4
|
-
* This function uses `Object.hasOwn()` to check if the given object has the
|
|
5
|
-
* as its own property (not inherited). It acts as a type guard
|
|
6
|
-
* object to guarantee the key exists, enabling
|
|
5
|
+
* This function uses `Object.hasOwn()` to check if the given object has the
|
|
6
|
+
* specified key as its own property (not inherited). It acts as a type guard
|
|
7
|
+
* that narrows the type of the object to guarantee the key exists, enabling
|
|
8
|
+
* type-safe property access.
|
|
7
9
|
*
|
|
8
10
|
* **Type Narrowing Behavior:**
|
|
9
|
-
*
|
|
11
|
+
*
|
|
12
|
+
* - When the guard returns `true`, TypeScript narrows the object type to include
|
|
13
|
+
* the checked key
|
|
10
14
|
* - For union types, only union members that contain the key are preserved
|
|
11
15
|
* - The key's value type is preserved from the original object type when possible
|
|
12
16
|
*
|
|
13
|
-
* @template R - The type of the input object, must extend UnknownRecord
|
|
14
|
-
* @template K - The type of the key to check for, must extend PropertyKey (string | number | symbol)
|
|
15
|
-
* @param obj - The object to check for the presence of the key
|
|
16
|
-
* @param key - The key to check for in the object
|
|
17
|
-
* @returns `true` if the object has the specified key as its own property, `false` otherwise.
|
|
18
|
-
* When `true`, TypeScript narrows the object type to guarantee the key exists.
|
|
19
|
-
*
|
|
20
17
|
* @example
|
|
21
|
-
* Basic usage with known object structure:
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const obj = { a: 1, b: 'hello' };
|
|
24
|
-
*
|
|
25
|
-
* if (hasKey(obj, 'a')) {
|
|
26
|
-
* // obj is narrowed to guarantee 'a' exists
|
|
27
|
-
* console.log(obj.a); // TypeScript knows 'a' exists and is type number
|
|
28
|
-
* // No need for optional chaining or undefined checks
|
|
29
|
-
* }
|
|
30
18
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* console.log(obj.c); // But TypeScript would know 'c' exists if it did
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* Working with dynamic objects and unknown keys:
|
|
39
|
-
* ```typescript
|
|
40
|
-
* const dynamicObj: Record<string, unknown> = { x: 10, y: 20 };
|
|
41
|
-
* const userInput: string = getUserInput();
|
|
19
|
+
* ```ts
|
|
20
|
+
* const maybeUser: { id?: number; name?: string } = { id: 42 };
|
|
42
21
|
*
|
|
43
|
-
* if (hasKey(
|
|
44
|
-
* //
|
|
45
|
-
*
|
|
46
|
-
* console.log(`Value for ${userInput}:`, value);
|
|
22
|
+
* if (hasKey(maybeUser, 'id')) {
|
|
23
|
+
* // `maybeUser` is now known to have an `id` property.
|
|
24
|
+
* assert(maybeUser.id === 42);
|
|
47
25
|
* } else {
|
|
48
|
-
*
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* Type narrowing with union types:
|
|
54
|
-
* ```typescript
|
|
55
|
-
* type UserPreferences =
|
|
56
|
-
* | { theme: 'dark'; notifications: boolean }
|
|
57
|
-
* | { theme: 'light' }
|
|
58
|
-
* | { autoSave: true; interval: number };
|
|
59
|
-
*
|
|
60
|
-
* const preferences: UserPreferences = getPreferences();
|
|
61
|
-
*
|
|
62
|
-
* if (hasKey(preferences, 'theme')) {
|
|
63
|
-
* // preferences is narrowed to the first two union members
|
|
64
|
-
* console.log(preferences.theme); // 'dark' | 'light'
|
|
65
|
-
* }
|
|
66
|
-
*
|
|
67
|
-
* if (hasKey(preferences, 'autoSave')) {
|
|
68
|
-
* // preferences is narrowed to the third union member
|
|
69
|
-
* console.log(preferences.interval); // number (we know this exists)
|
|
70
|
-
* }
|
|
71
|
-
* ```
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* Basic usage with isRecord for progressive narrowing:
|
|
75
|
-
* ```typescript
|
|
76
|
-
* const data: unknown = parseApiResponse();
|
|
77
|
-
*
|
|
78
|
-
* if (isRecord(data) && hasKey(data, 'user')) {
|
|
79
|
-
* // data is now Record<string, unknown> with guaranteed 'user' key
|
|
80
|
-
* const user = data.user;
|
|
81
|
-
*
|
|
82
|
-
* if (isRecord(user) && hasKey(user, 'name')) {
|
|
83
|
-
* // Safely access nested properties
|
|
84
|
-
* console.log('User name:', user.name);
|
|
85
|
-
* }
|
|
26
|
+
* assert.fail('Expected the object to contain the id key.');
|
|
86
27
|
* }
|
|
87
28
|
* ```
|
|
88
29
|
*
|
|
30
|
+
* @template R - The type of the input object, must extend UnknownRecord
|
|
31
|
+
* @template K - The type of the key to check for, must extend PropertyKey
|
|
32
|
+
* (string | number | symbol)
|
|
33
|
+
* @param obj - The object to check for the presence of the key
|
|
34
|
+
* @param key - The key to check for in the object
|
|
35
|
+
* @returns `true` if the object has the specified key as its own property,
|
|
36
|
+
* `false` otherwise. When `true`, TypeScript narrows the object type to
|
|
37
|
+
* guarantee the key exists.
|
|
89
38
|
* @see {@link keyIsIn} - Similar function that narrows the key type instead of the object type
|
|
90
39
|
*/
|
|
91
40
|
export const hasKey = <
|
|
@@ -1,106 +1,39 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type guard that checks if a value is a non-empty string.
|
|
3
3
|
*
|
|
4
|
-
* This function performs both a type check (ensuring the value is a string) and
|
|
5
|
-
* (ensuring the string is not empty). It acts as a type guard
|
|
6
|
-
* exclude `null`, `undefined`, and empty
|
|
4
|
+
* This function performs both a type check (ensuring the value is a string) and
|
|
5
|
+
* a length check (ensuring the string is not empty). It acts as a type guard
|
|
6
|
+
* that narrows the input type to exclude `null`, `undefined`, and empty
|
|
7
|
+
* strings.
|
|
7
8
|
*
|
|
8
9
|
* **Type Narrowing Behavior:**
|
|
10
|
+
*
|
|
9
11
|
* - Eliminates `null` and `undefined` from the type
|
|
10
12
|
* - Excludes empty string (`""`) from the type
|
|
11
13
|
* - Preserves literal string types when possible
|
|
12
14
|
* - Whitespace-only strings are considered non-empty
|
|
13
15
|
*
|
|
14
|
-
* @template S - The input type, which can include `string`, `null`, or `undefined`
|
|
15
|
-
* @param s - The value to check
|
|
16
|
-
* @returns `true` if `s` is a string with length > 0, `false` otherwise.
|
|
17
|
-
* When `true`, TypeScript narrows the type to exclude empty strings and nullish values.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* Basic usage with different string types:
|
|
21
|
-
* ```typescript
|
|
22
|
-
* isNonEmptyString("hello"); // true
|
|
23
|
-
* isNonEmptyString(" "); // true (whitespace is considered non-empty)
|
|
24
|
-
* isNonEmptyString("\t\n"); // true (whitespace characters are non-empty)
|
|
25
|
-
* isNonEmptyString(""); // false
|
|
26
|
-
* isNonEmptyString(null); // false
|
|
27
|
-
* isNonEmptyString(undefined); // false
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* Type guard usage with nullable strings:
|
|
32
|
-
* ```typescript
|
|
33
|
-
* const userInput: string | null | undefined = getUserInput();
|
|
34
|
-
*
|
|
35
|
-
* if (isNonEmptyString(userInput)) {
|
|
36
|
-
* // userInput is now typed as non-empty string
|
|
37
|
-
* console.log(userInput.charAt(0)); // Safe to access string methods
|
|
38
|
-
* console.log(userInput.toUpperCase()); // No need for null checks
|
|
39
|
-
* const length = userInput.length; // Guaranteed to be > 0
|
|
40
|
-
* } else {
|
|
41
|
-
* console.log('No valid input provided');
|
|
42
|
-
* }
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* Working with literal string types:
|
|
47
|
-
* ```typescript
|
|
48
|
-
* type Status = "active" | "inactive" | "" | null;
|
|
49
|
-
* const status: Status = getStatus();
|
|
50
|
-
*
|
|
51
|
-
* if (isNonEmptyString(status)) {
|
|
52
|
-
* // status is now typed as "active" | "inactive"
|
|
53
|
-
* console.log(`Status is: ${status}`);
|
|
54
|
-
* }
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
16
|
* @example
|
|
58
|
-
* Form validation patterns:
|
|
59
|
-
* ```typescript
|
|
60
|
-
* interface FormData {
|
|
61
|
-
* name?: string;
|
|
62
|
-
* email?: string;
|
|
63
|
-
* phone?: string;
|
|
64
|
-
* }
|
|
65
|
-
*
|
|
66
|
-
* function validateForm(data: FormData): string[] {
|
|
67
|
-
* const errors: string[] = [];
|
|
68
|
-
*
|
|
69
|
-
* if (!isNonEmptyString(data.name)) {
|
|
70
|
-
* errors.push('Name is required');
|
|
71
|
-
* }
|
|
72
|
-
*
|
|
73
|
-
* if (!isNonEmptyString(data.email)) {
|
|
74
|
-
* errors.push('Email is required');
|
|
75
|
-
* } else if (!data.email.includes('@')) {
|
|
76
|
-
* // Safe to access string methods after guard
|
|
77
|
-
* errors.push('Invalid email format');
|
|
78
|
-
* }
|
|
79
17
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* Filtering arrays of potentially empty strings:
|
|
86
|
-
* ```typescript
|
|
87
|
-
* const responses: (string | null | undefined)[] = [
|
|
88
|
-
* "hello",
|
|
89
|
-
* "",
|
|
90
|
-
* "world",
|
|
91
|
-
* null,
|
|
18
|
+
* ```ts
|
|
19
|
+
* const words: readonly (string | undefined)[] = [
|
|
20
|
+
* 'Ada',
|
|
92
21
|
* undefined,
|
|
93
|
-
*
|
|
94
|
-
*
|
|
22
|
+
* '',
|
|
23
|
+
* 'Lovelace',
|
|
24
|
+
* ] as const;
|
|
95
25
|
*
|
|
96
|
-
* const
|
|
97
|
-
* // validResponses is now string[] containing ["hello", "world", " "]
|
|
26
|
+
* const nonEmpty = words.filter(isNonEmptyString);
|
|
98
27
|
*
|
|
99
|
-
*
|
|
100
|
-
* // Each response is guaranteed to be a non-empty string
|
|
101
|
-
* console.log(response.trim().toUpperCase());
|
|
102
|
-
* });
|
|
28
|
+
* assert.deepStrictEqual(nonEmpty, ['Ada', 'Lovelace']);
|
|
103
29
|
* ```
|
|
30
|
+
*
|
|
31
|
+
* @template S - The input type, which can include `string`, `null`, or
|
|
32
|
+
* `undefined`
|
|
33
|
+
* @param s - The value to check
|
|
34
|
+
* @returns `true` if `s` is a string with length > 0, `false` otherwise. When
|
|
35
|
+
* `true`, TypeScript narrows the type to exclude empty strings and nullish
|
|
36
|
+
* values.
|
|
104
37
|
*/
|
|
105
38
|
export const isNonEmptyString = <S extends string | null | undefined>(
|
|
106
39
|
s: S,
|
|
@@ -1,104 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type guard that checks if a value is a non-null object.
|
|
3
3
|
*
|
|
4
|
-
* This function checks if a value has type `"object"` according to the `typeof`
|
|
5
|
-
* and is not `null`. This includes all object types such as plain
|
|
6
|
-
* regular expressions, and other object instances, but
|
|
4
|
+
* This function checks if a value has type `"object"` according to the `typeof`
|
|
5
|
+
* operator and is not `null`. This includes all object types such as plain
|
|
6
|
+
* objects, arrays, dates, regular expressions, and other object instances, but
|
|
7
|
+
* excludes functions.
|
|
7
8
|
*
|
|
8
9
|
* **Type Narrowing Behavior:**
|
|
10
|
+
*
|
|
9
11
|
* - Narrows `unknown` to `object`
|
|
10
12
|
* - Excludes `null`, `undefined`, and all primitive types
|
|
11
13
|
* - Excludes functions (they have `typeof` === `"function"`, not `"object"`)
|
|
12
14
|
* - Includes arrays, dates, regex, and other object instances
|
|
13
15
|
*
|
|
14
|
-
* **Note:** This function returns `true` for arrays. If you need to check for
|
|
15
|
-
* specifically (excluding arrays), use `isRecord()` instead.
|
|
16
|
-
*
|
|
17
|
-
* @param u - The value to check
|
|
18
|
-
* @returns `true` if `u` is an object and not `null`, `false` otherwise.
|
|
19
|
-
* When `true`, TypeScript narrows the type to `object`.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* Basic usage with different value types:
|
|
23
|
-
* ```typescript
|
|
24
|
-
* isNonNullObject({}); // true (plain object)
|
|
25
|
-
* isNonNullObject([]); // true (arrays are objects)
|
|
26
|
-
* isNonNullObject(new Date()); // true (Date instance)
|
|
27
|
-
* isNonNullObject(/regex/); // true (RegExp instance)
|
|
28
|
-
* isNonNullObject(new Map()); // true (Map instance)
|
|
29
|
-
* isNonNullObject(null); // false (null is not considered object here)
|
|
30
|
-
* isNonNullObject(undefined); // false (primitive)
|
|
31
|
-
* isNonNullObject("string"); // false (primitive)
|
|
32
|
-
* isNonNullObject(42); // false (primitive)
|
|
33
|
-
* isNonNullObject(true); // false (primitive)
|
|
34
|
-
* isNonNullObject(() => {}); // false (functions are not objects in this context)
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* Type guard usage with unknown values:
|
|
39
|
-
* ```typescript
|
|
40
|
-
* const value: unknown = parseJsonData();
|
|
41
|
-
*
|
|
42
|
-
* if (isNonNullObject(value)) {
|
|
43
|
-
* // value is now typed as object
|
|
44
|
-
* console.log('Value is an object');
|
|
45
|
-
*
|
|
46
|
-
* // You can now safely use object-specific operations
|
|
47
|
-
* console.log(Object.keys(value)); // Safe to call Object.keys
|
|
48
|
-
* console.log(value.toString()); // Safe to call methods
|
|
49
|
-
*
|
|
50
|
-
* // But you may need additional checks for specific object types
|
|
51
|
-
* if (Array.isArray(value)) {
|
|
52
|
-
* console.log('It\'s an array with length:', value.length);
|
|
53
|
-
* }
|
|
54
|
-
* } else {
|
|
55
|
-
* console.log('Value is not an object');
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
16
|
+
* **Note:** This function returns `true` for arrays. If you need to check for
|
|
17
|
+
* plain objects specifically (excluding arrays), use `isRecord()` instead.
|
|
58
18
|
*
|
|
59
19
|
* @example
|
|
60
|
-
* Filtering arrays to find objects:
|
|
61
|
-
* ```typescript
|
|
62
|
-
* const mixedArray: unknown[] = [
|
|
63
|
-
* { name: 'John' },
|
|
64
|
-
* 'string',
|
|
65
|
-
* [1, 2, 3],
|
|
66
|
-
* 42,
|
|
67
|
-
* null,
|
|
68
|
-
* new Date(),
|
|
69
|
-
* () => 'function'
|
|
70
|
-
* ];
|
|
71
20
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* // Note: includes both plain objects and arrays
|
|
21
|
+
* ```ts
|
|
22
|
+
* const mixed: unknown[] = [{ id: 1 }, null, 'Ada'] as const;
|
|
75
23
|
*
|
|
76
|
-
* objects.
|
|
77
|
-
* // Each obj is guaranteed to be an object
|
|
78
|
-
* console.log('Object type:', obj.constructor.name);
|
|
79
|
-
* });
|
|
80
|
-
* ```
|
|
24
|
+
* const objects = mixed.filter(isNonNullObject);
|
|
81
25
|
*
|
|
82
|
-
*
|
|
83
|
-
* Progressive type narrowing with other guards:
|
|
84
|
-
* ```typescript
|
|
85
|
-
* const apiResponse: unknown = await fetchData();
|
|
86
|
-
*
|
|
87
|
-
* if (isNonNullObject(apiResponse)) {
|
|
88
|
-
* // apiResponse is now object
|
|
89
|
-
*
|
|
90
|
-
* if (isRecord(apiResponse)) {
|
|
91
|
-
* // Further narrowed to UnknownRecord (plain object, not array)
|
|
92
|
-
*
|
|
93
|
-
* if (hasKey(apiResponse, 'status')) {
|
|
94
|
-
* console.log('API status:', apiResponse.status);
|
|
95
|
-
* }
|
|
96
|
-
* } else if (Array.isArray(apiResponse)) {
|
|
97
|
-
* console.log('Response is an array with length:', apiResponse.length);
|
|
98
|
-
* }
|
|
99
|
-
* }
|
|
26
|
+
* assert.deepStrictEqual(objects, [{ id: 1 }]);
|
|
100
27
|
* ```
|
|
101
28
|
*
|
|
29
|
+
* @param u - The value to check
|
|
30
|
+
* @returns `true` if `u` is an object and not `null`, `false` otherwise. When
|
|
31
|
+
* `true`, TypeScript narrows the type to `object`.
|
|
102
32
|
* @see {@link isRecord} - For checking plain objects specifically (excludes arrays)
|
|
103
33
|
*/
|
|
104
34
|
// eslint-disable-next-line @typescript-eslint/no-restricted-types
|
|
@@ -50,7 +50,7 @@ describe('isNonNullObject', () => {
|
|
|
50
50
|
expect(isNonNullObject(true)).toBe(false);
|
|
51
51
|
expect(isNonNullObject(false)).toBe(false);
|
|
52
52
|
expect(isNonNullObject(Symbol('test'))).toBe(false);
|
|
53
|
-
expect(isNonNullObject(
|
|
53
|
+
expect(isNonNullObject(123n)).toBe(false);
|
|
54
54
|
});
|
|
55
55
|
|
|
56
56
|
test('should act as a type guard', () => {
|
|
@@ -3,146 +3,33 @@ import { expectType } from '../expect-type.mjs';
|
|
|
3
3
|
/**
|
|
4
4
|
* Type guard that checks if a value is a primitive type.
|
|
5
5
|
*
|
|
6
|
-
* This function identifies JavaScript primitive types, which are immutable data
|
|
7
|
-
* not objects. The primitive types are: `string`, `number`,
|
|
8
|
-
* `bigint`, and `null`.
|
|
6
|
+
* This function identifies JavaScript primitive types, which are immutable data
|
|
7
|
+
* types that are not objects. The primitive types are: `string`, `number`,
|
|
8
|
+
* `boolean`, `undefined`, `symbol`, `bigint`, and `null`.
|
|
9
9
|
*
|
|
10
|
-
* **Important Note:** Although `null` has `typeof null === "object"` due to a
|
|
11
|
-
* JavaScript quirk, this function correctly identifies `null` as a
|
|
10
|
+
* **Important Note:** Although `null` has `typeof null === "object"` due to a
|
|
11
|
+
* historical JavaScript quirk, this function correctly identifies `null` as a
|
|
12
|
+
* primitive value.
|
|
12
13
|
*
|
|
13
14
|
* **Type Narrowing Behavior:**
|
|
15
|
+
*
|
|
14
16
|
* - Narrows the input type to `Primitive` (union of all primitive types)
|
|
15
17
|
* - Excludes object types, arrays, functions, and other non-primitive values
|
|
16
18
|
* - Includes `null` despite its misleading `typeof` result
|
|
17
19
|
*
|
|
18
|
-
* @param u - The value to check
|
|
19
|
-
* @returns `true` if `u` is a primitive type, `false` otherwise.
|
|
20
|
-
* When `true`, TypeScript narrows the type to `Primitive`.
|
|
21
|
-
*
|
|
22
20
|
* @example
|
|
23
|
-
* Basic usage with different value types:
|
|
24
|
-
* ```typescript
|
|
25
|
-
* isPrimitive("hello"); // true (string)
|
|
26
|
-
* isPrimitive(42); // true (number)
|
|
27
|
-
* isPrimitive(true); // true (boolean)
|
|
28
|
-
* isPrimitive(undefined); // true (undefined)
|
|
29
|
-
* isPrimitive(Symbol('test')); // true (symbol)
|
|
30
|
-
* isPrimitive(123n); // true (bigint)
|
|
31
|
-
* isPrimitive(null); // true (null is primitive despite typeof quirk)
|
|
32
|
-
*
|
|
33
|
-
* isPrimitive({}); // false (object)
|
|
34
|
-
* isPrimitive([]); // false (array)
|
|
35
|
-
* isPrimitive(() => {}); // false (function)
|
|
36
|
-
* isPrimitive(new Date()); // false (object instance)
|
|
37
|
-
* isPrimitive(/regex/); // false (RegExp object)
|
|
38
|
-
* ```
|
|
39
21
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* ```typescript
|
|
43
|
-
* const values: unknown[] = [
|
|
44
|
-
* 'string',
|
|
45
|
-
* 42,
|
|
46
|
-
* true,
|
|
47
|
-
* null,
|
|
48
|
-
* undefined,
|
|
49
|
-
* {},
|
|
50
|
-
* [],
|
|
51
|
-
* new Date()
|
|
52
|
-
* ];
|
|
22
|
+
* ```ts
|
|
23
|
+
* const values: readonly unknown[] = [42, 'Ada', null, { id: 1 }] as const;
|
|
53
24
|
*
|
|
54
25
|
* const primitives = values.filter(isPrimitive);
|
|
55
|
-
* const objects = values.filter(value => !isPrimitive(value));
|
|
56
|
-
*
|
|
57
|
-
* primitives.forEach(primitive => {
|
|
58
|
-
* // primitive is now typed as Primitive
|
|
59
|
-
* console.log('Primitive value:', primitive);
|
|
60
|
-
* console.log('Type:', typeof primitive);
|
|
61
|
-
* });
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* Deep cloning detection - primitives don't need cloning:
|
|
66
|
-
* ```typescript
|
|
67
|
-
* function deepClone<T>(value: T): T {
|
|
68
|
-
* if (isPrimitive(value)) {
|
|
69
|
-
* // Primitives are immutable, return as-is
|
|
70
|
-
* return value;
|
|
71
|
-
* }
|
|
72
26
|
*
|
|
73
|
-
*
|
|
74
|
-
* if (Array.isArray(value)) {
|
|
75
|
-
* return value.map(deepClone) as T;
|
|
76
|
-
* }
|
|
77
|
-
*
|
|
78
|
-
* if (isRecord(value)) {
|
|
79
|
-
* const cloned = {} as T;
|
|
80
|
-
* for (const key in value) {
|
|
81
|
-
* if (Object.hasOwn(value, key)) {
|
|
82
|
-
* cloned[key] = deepClone(value[key]);
|
|
83
|
-
* }
|
|
84
|
-
* }
|
|
85
|
-
* return cloned;
|
|
86
|
-
* }
|
|
87
|
-
*
|
|
88
|
-
* // For other object types, return as-is or implement specific cloning
|
|
89
|
-
* return value;
|
|
90
|
-
* }
|
|
91
|
-
* ```
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* Serialization helpers:
|
|
95
|
-
* ```typescript
|
|
96
|
-
* function canSerializeDirectly(value: unknown): boolean {
|
|
97
|
-
* if (isPrimitive(value)) {
|
|
98
|
-
* // Most primitives can be serialized directly
|
|
99
|
-
* return typeof value !== 'symbol' && typeof value !== 'bigint';
|
|
100
|
-
* }
|
|
101
|
-
* return false;
|
|
102
|
-
* }
|
|
103
|
-
*
|
|
104
|
-
* function safeStringify(value: unknown): string {
|
|
105
|
-
* if (isPrimitive(value)) {
|
|
106
|
-
* if (value === null) return 'null';
|
|
107
|
-
* if (value === undefined) return 'undefined';
|
|
108
|
-
* if (typeof value === 'symbol') return value.toString();
|
|
109
|
-
* if (typeof value === 'bigint') return value.toString() + 'n';
|
|
110
|
-
* return String(value);
|
|
111
|
-
* }
|
|
112
|
-
*
|
|
113
|
-
* return JSON.stringify(value);
|
|
114
|
-
* }
|
|
27
|
+
* assert.deepStrictEqual(primitives, [42, 'Ada', null]);
|
|
115
28
|
* ```
|
|
116
29
|
*
|
|
117
|
-
* @
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* function processValue(value: unknown): string {
|
|
121
|
-
* if (isPrimitive(value)) {
|
|
122
|
-
* // value is now Primitive type
|
|
123
|
-
* switch (typeof value) {
|
|
124
|
-
* case 'string':
|
|
125
|
-
* return `String: ${value}`;
|
|
126
|
-
* case 'number':
|
|
127
|
-
* return `Number: ${value}`;
|
|
128
|
-
* case 'boolean':
|
|
129
|
-
* return `Boolean: ${value}`;
|
|
130
|
-
* case 'undefined':
|
|
131
|
-
* return 'Undefined';
|
|
132
|
-
* case 'symbol':
|
|
133
|
-
* return `Symbol: ${value.description || 'unnamed'}`;
|
|
134
|
-
* case 'bigint':
|
|
135
|
-
* return `BigInt: ${value}n`;
|
|
136
|
-
* case 'object': // This is null
|
|
137
|
-
* return 'Null';
|
|
138
|
-
* default:
|
|
139
|
-
* return 'Unknown primitive';
|
|
140
|
-
* }
|
|
141
|
-
* } else {
|
|
142
|
-
* return `Object: ${value?.constructor?.name || 'Unknown'}`;
|
|
143
|
-
* }
|
|
144
|
-
* }
|
|
145
|
-
* ```
|
|
30
|
+
* @param u - The value to check
|
|
31
|
+
* @returns `true` if `u` is a primitive type, `false` otherwise. When `true`,
|
|
32
|
+
* TypeScript narrows the type to `Primitive`.
|
|
146
33
|
*/
|
|
147
34
|
export const isPrimitive = (u: unknown): u is Primitive => {
|
|
148
35
|
switch (typeof u) {
|
|
@@ -34,7 +34,7 @@ describe('isPrimitive', () => {
|
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
test('should return true for bigint primitives', () => {
|
|
37
|
-
expect(isPrimitive(
|
|
37
|
+
expect(isPrimitive(123n)).toBe(true);
|
|
38
38
|
expect(isPrimitive(0n)).toBe(true);
|
|
39
39
|
expect(isPrimitive(-123n)).toBe(true);
|
|
40
40
|
});
|