ts-data-forge 3.3.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 +37 -13
- 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 +1 -0
- package/dist/entry-point.d.mts.map +1 -1
- package/dist/entry-point.mjs +2 -1
- package/dist/entry-point.mjs.map +1 -1
- 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 +12 -126
- 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/package.json +47 -39
- 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 +1 -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-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 +12 -126
- package/src/others/unknown-to-string.test.mts +13 -1
- 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
|
@@ -1,144 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Applies a function to a value if the value is not `null` or `undefined`.
|
|
3
|
-
*
|
|
2
|
+
* Applies a function to a value if the value is not `null` or `undefined`. If
|
|
3
|
+
* the value is `null` or `undefined`, it returns `undefined`.
|
|
4
4
|
*
|
|
5
|
-
* This function provides a safe way to transform nullable values without
|
|
6
|
-
* It's similar to Optional.map() but works directly with
|
|
7
|
-
* Supports both regular and curried usage for
|
|
5
|
+
* This function provides a safe way to transform nullable values without
|
|
6
|
+
* explicit null checks. It's similar to Optional.map() but works directly with
|
|
7
|
+
* TypeScript's nullable types. Supports both regular and curried usage for
|
|
8
|
+
* functional composition.
|
|
8
9
|
*
|
|
9
10
|
* @template A - The type of the input value (excluding null/undefined)
|
|
10
11
|
* @template B - The type of the value returned by the mapping function
|
|
11
|
-
* @param value - The value to potentially transform (can be `A`, `null`, or
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
* @
|
|
16
|
-
*
|
|
17
|
-
* // Safe string transformation
|
|
18
|
-
* mapNullable("hello", s => s.toUpperCase()); // "HELLO"
|
|
19
|
-
* mapNullable(null, s => s.toUpperCase()); // undefined
|
|
20
|
-
* mapNullable(undefined, s => s.toUpperCase()); // undefined
|
|
21
|
-
*
|
|
22
|
-
* // Number operations
|
|
23
|
-
* mapNullable(5, n => n * 2); // 10
|
|
24
|
-
* mapNullable(0, n => n * 2); // 0 (note: 0 is not null/undefined)
|
|
25
|
-
* mapNullable(null as number | null, n => n * 2); // undefined
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @example Working with optional object properties
|
|
29
|
-
* ```typescript
|
|
30
|
-
* interface User {
|
|
31
|
-
* id: number;
|
|
32
|
-
* name?: string;
|
|
33
|
-
* email?: string;
|
|
34
|
-
* }
|
|
35
|
-
*
|
|
36
|
-
* function formatUserDisplay(user: User): string {
|
|
37
|
-
* const displayName = mapNullable(user.name, name => name.toUpperCase()) ?? 'Anonymous';
|
|
38
|
-
* const emailDomain = mapNullable(user.email, email => email.split('@')[1]);
|
|
39
|
-
*
|
|
40
|
-
* return `${displayName} ${emailDomain ? `(${emailDomain})` : ''}`;
|
|
41
|
-
* }
|
|
42
|
-
*
|
|
43
|
-
* formatUserDisplay({ id: 1, name: 'John', email: 'john@example.com' }); // "JOHN (example.com)"
|
|
44
|
-
* formatUserDisplay({ id: 2 }); // "Anonymous "
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* @example Curried usage for functional composition
|
|
48
|
-
* ```typescript
|
|
49
|
-
* // Create reusable transformers
|
|
50
|
-
* const toUpperCase = mapNullable((s: string) => s.toUpperCase());
|
|
51
|
-
* const addPrefix = mapNullable((s: string) => `PREFIX_${s}`);
|
|
52
|
-
* const parseNumber = mapNullable((s: string) => parseInt(s, 10));
|
|
53
|
-
*
|
|
54
|
-
* // Use in different contexts
|
|
55
|
-
* toUpperCase("hello"); // "HELLO"
|
|
56
|
-
* toUpperCase(null); // undefined
|
|
57
|
-
*
|
|
58
|
-
* // Compose transformations
|
|
59
|
-
* const processString = (s: string | null) => {
|
|
60
|
-
* const upper = toUpperCase(s);
|
|
61
|
-
* return addPrefix(upper);
|
|
62
|
-
* };
|
|
63
|
-
*
|
|
64
|
-
* processString("test"); // "PREFIX_TEST"
|
|
65
|
-
* processString(null); // undefined
|
|
66
|
-
* ```
|
|
67
|
-
*
|
|
68
|
-
* @example Chaining nullable operations
|
|
69
|
-
* ```typescript
|
|
70
|
-
* // API response handling
|
|
71
|
-
* interface ApiResponse {
|
|
72
|
-
* data?: {
|
|
73
|
-
* user?: {
|
|
74
|
-
* profile?: {
|
|
75
|
-
* displayName?: string;
|
|
76
|
-
* };
|
|
77
|
-
* };
|
|
78
|
-
* };
|
|
79
|
-
* }
|
|
80
|
-
*
|
|
81
|
-
* function getDisplayName(response: ApiResponse): string | undefined {
|
|
82
|
-
* return mapNullable(
|
|
83
|
-
* response.data?.user?.profile?.displayName,
|
|
84
|
-
* name => name.trim().toUpperCase()
|
|
85
|
-
* );
|
|
86
|
-
* }
|
|
87
|
-
*
|
|
88
|
-
* // Chain multiple transformations
|
|
89
|
-
* function processNullableChain(value: string | null): string | undefined {
|
|
90
|
-
* const step1 = mapNullable(value, v => v.trim());
|
|
91
|
-
* const step2 = mapNullable(step1, v => v.length > 0 ? v : null);
|
|
92
|
-
* const step3 = mapNullable(step2, v => v.toUpperCase());
|
|
93
|
-
* return step3;
|
|
94
|
-
* }
|
|
95
|
-
* ```
|
|
96
|
-
*
|
|
97
|
-
* @example Integration with array methods
|
|
98
|
-
* ```typescript
|
|
99
|
-
* const nullableNumbers: (number | null | undefined)[] = [1, null, 3, undefined, 5];
|
|
100
|
-
*
|
|
101
|
-
* // Transform and filter in one step
|
|
102
|
-
* const doubled = nullableNumbers
|
|
103
|
-
* .map(n => mapNullable(n, x => x * 2))
|
|
104
|
-
* .filter((n): n is number => n !== undefined);
|
|
105
|
-
* // Result: [2, 6, 10]
|
|
106
|
-
*
|
|
107
|
-
* // Process optional array elements
|
|
108
|
-
* const users: Array<{ name?: string }> = [
|
|
109
|
-
* { name: 'Alice' },
|
|
110
|
-
* { name: undefined },
|
|
111
|
-
* { name: 'Bob' }
|
|
112
|
-
* ];
|
|
113
|
-
*
|
|
114
|
-
* const upperNames = users
|
|
115
|
-
* .map(u => mapNullable(u.name, n => n.toUpperCase()))
|
|
116
|
-
* .filter((n): n is string => n !== undefined);
|
|
117
|
-
* // Result: ['ALICE', 'BOB']
|
|
118
|
-
* ```
|
|
119
|
-
*
|
|
120
|
-
* @example Error handling patterns
|
|
121
|
-
* ```typescript
|
|
122
|
-
* // Safe JSON parsing
|
|
123
|
-
* function parseJsonSafe<T>(json: string | null): T | undefined {
|
|
124
|
-
* return mapNullable(json, j => {
|
|
125
|
-
* try {
|
|
126
|
-
* return JSON.parse(j) as T;
|
|
127
|
-
* } catch {
|
|
128
|
-
* return null;
|
|
129
|
-
* }
|
|
130
|
-
* }) ?? undefined;
|
|
131
|
-
* }
|
|
132
|
-
*
|
|
133
|
-
* // Safe property access with computation
|
|
134
|
-
* function calculateAge(birthYear: number | null): string | undefined {
|
|
135
|
-
* return mapNullable(birthYear, year => {
|
|
136
|
-
* const age = new Date().getFullYear() - year;
|
|
137
|
-
* return age >= 0 ? `${age} years old` : null;
|
|
138
|
-
* }) ?? undefined;
|
|
139
|
-
* }
|
|
140
|
-
* ```
|
|
141
|
-
*
|
|
12
|
+
* @param value - The value to potentially transform (can be `A`, `null`, or
|
|
13
|
+
* `undefined`)
|
|
14
|
+
* @param mapFn - A function that transforms a non-nullable value of type `A` to
|
|
15
|
+
* type `B`
|
|
16
|
+
* @returns The result of applying `mapFn` to `value` if value is not
|
|
17
|
+
* null/undefined; otherwise `undefined`
|
|
142
18
|
* @see Optional - For more complex optional value handling
|
|
143
19
|
* @see Result - For error handling with detailed error information
|
|
144
20
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-nullable.d.mts","sourceRoot":"","sources":["../../src/others/map-nullable.mts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"map-nullable.d.mts","sourceRoot":"","sources":["../../src/others/map-nullable.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EAC1C,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAC3B,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GACjB,CAAC,GAAG,SAAS,CAAC;AAGjB,wBAAgB,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EAC1C,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GACjB,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,KAAK,CAAC,GAAG,SAAS,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-nullable.mjs","sources":["../../src/others/map-nullable.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"map-nullable.mjs","sources":["../../src/others/map-nullable.mts"],"sourcesContent":[null],"names":[],"mappings":"AA8BM,SAAU,WAAW,CACzB,GAAG,IAE8B,EAAA;AAEjC,IAAA,QAAQ,IAAI,CAAC,MAAM;QACjB,KAAK,CAAC,EAAE;AACN,YAAA,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,IAAI;AAC3B,YAAA,OAAO,KAAK,IAAI,IAAI,GAAG,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC;QACjD;QACA,KAAK,CAAC,EAAE;AACN,YAAA,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI;YACpB,OAAO,CAAC,KAA2B,KAAK,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC;QACnE;;AAEJ;;;;"}
|
|
@@ -1,172 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Creates a memoized version of a function that caches results based on input
|
|
2
|
+
* Creates a memoized version of a function that caches results based on input
|
|
3
|
+
* arguments.
|
|
3
4
|
*
|
|
4
|
-
* The memoized function stores results in an internal Map and returns cached
|
|
5
|
-
* for repeated calls with the same arguments. This can significantly
|
|
6
|
-
* for expensive computations or I/O operations.
|
|
5
|
+
* The memoized function stores results in an internal Map and returns cached
|
|
6
|
+
* values for repeated calls with the same arguments. This can significantly
|
|
7
|
+
* improve performance for expensive computations or I/O operations.
|
|
7
8
|
*
|
|
8
9
|
* **Important considerations:**
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
10
|
+
*
|
|
11
|
+
* - The cache grows unbounded - consider memory implications for long-running
|
|
12
|
+
* applications
|
|
13
|
+
* - Cache keys must be primitives (string, number, boolean, symbol, null,
|
|
14
|
+
* undefined, bigint)
|
|
11
15
|
* - Object arguments require careful key generation to ensure uniqueness
|
|
12
16
|
* - Pure functions only - memoizing functions with side effects can lead to bugs
|
|
13
17
|
*
|
|
14
18
|
* @template A - The tuple type of the function arguments
|
|
15
19
|
* @template R - The return type of the function
|
|
16
|
-
* @template K - The primitive type used as the cache key (must be valid Map
|
|
20
|
+
* @template K - The primitive type used as the cache key (must be valid Map
|
|
21
|
+
* key)
|
|
17
22
|
* @param fn - The pure function to memoize
|
|
18
|
-
* @param argsToCacheKey - Function that converts arguments to a unique cache
|
|
23
|
+
* @param argsToCacheKey - Function that converts arguments to a unique cache
|
|
24
|
+
* key
|
|
19
25
|
* @returns A memoized version of the input function with the same signature
|
|
20
26
|
*
|
|
21
|
-
* @example Basic memoization for expensive calculations
|
|
22
|
-
* ```typescript
|
|
23
|
-
* // Fibonacci calculation (exponential time complexity)
|
|
24
|
-
* const fibonacci = (n: number): number => {
|
|
25
|
-
* console.log(`Computing fib(${n})`);
|
|
26
|
-
* if (n <= 1) return n;
|
|
27
|
-
* return fibonacci(n - 1) + fibonacci(n - 2);
|
|
28
|
-
* };
|
|
29
|
-
*
|
|
30
|
-
* const memoizedFib = memoizeFunction(
|
|
31
|
-
* fibonacci,
|
|
32
|
-
* (n) => n // Number itself as key
|
|
33
|
-
* );
|
|
34
|
-
*
|
|
35
|
-
* memoizedFib(40); // Much faster than unmemoized version
|
|
36
|
-
* memoizedFib(40); // Returns instantly from cache
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @example Multi-argument functions with composite keys
|
|
40
|
-
* ```typescript
|
|
41
|
-
* // Grid calculation with x,y coordinates
|
|
42
|
-
* const calculateGridValue = (x: number, y: number, scale: number): number => {
|
|
43
|
-
* console.log(`Computing grid(${x},${y},${scale})`);
|
|
44
|
-
* // Expensive computation...
|
|
45
|
-
* return Math.sin(x * scale) * Math.cos(y * scale);
|
|
46
|
-
* };
|
|
47
|
-
*
|
|
48
|
-
* const memoizedGrid = memoizeFunction(
|
|
49
|
-
* calculateGridValue,
|
|
50
|
-
* (x, y, scale) => `${x},${y},${scale}` // String concatenation for composite key
|
|
51
|
-
* );
|
|
52
|
-
*
|
|
53
|
-
* // Alternative: Using bit manipulation for integer coordinates
|
|
54
|
-
* const memoizedGrid2 = memoizeFunction(
|
|
55
|
-
* calculateGridValue,
|
|
56
|
-
* (x, y, scale) => (x << 20) | (y << 10) | scale // Assuming small positive integers
|
|
57
|
-
* );
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* @example Object arguments with selective memoization
|
|
61
|
-
* ```typescript
|
|
62
|
-
* interface User {
|
|
63
|
-
* id: number;
|
|
64
|
-
* name: string;
|
|
65
|
-
* email: string;
|
|
66
|
-
* metadata?: Record<string, unknown>;
|
|
67
|
-
* }
|
|
68
|
-
*
|
|
69
|
-
* const fetchUserPermissions = async (user: User): Promise<string[]> => {
|
|
70
|
-
* console.log(`Fetching permissions for user ${user.id}`);
|
|
71
|
-
* const response = await api.get(`/permissions/${user.id}`);
|
|
72
|
-
* return response.data;
|
|
73
|
-
* };
|
|
74
|
-
*
|
|
75
|
-
* // Memoize based only on user ID, ignoring other fields
|
|
76
|
-
* const memoizedFetchPermissions = memoizeFunction(
|
|
77
|
-
* fetchUserPermissions,
|
|
78
|
-
* (user) => user.id // Only cache by ID
|
|
79
|
-
* );
|
|
80
|
-
*
|
|
81
|
-
* // For multiple identifying fields
|
|
82
|
-
* const processUserData = (user: User, orgId: number): ProcessedData => {
|
|
83
|
-
* // Complex processing...
|
|
84
|
-
* };
|
|
85
|
-
*
|
|
86
|
-
* const memoizedProcess = memoizeFunction(
|
|
87
|
-
* processUserData,
|
|
88
|
-
* (user, orgId) => `${user.id}:${orgId}` // Composite key with separator
|
|
89
|
-
* );
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* @example Memoizing recursive functions
|
|
93
|
-
* ```typescript
|
|
94
|
-
* // Recursive path finding
|
|
95
|
-
* const findPaths = (start: string, end: string, visited: Set<string> = new Set()): string[][] => {
|
|
96
|
-
* if (start === end) return [[end]];
|
|
97
|
-
* // ... complex recursive logic
|
|
98
|
-
* };
|
|
99
|
-
*
|
|
100
|
-
* // Use sorted, serialized visited set for consistent keys
|
|
101
|
-
* const memoizedFindPaths = memoizeFunction(
|
|
102
|
-
* findPaths,
|
|
103
|
-
* (start, end, visited = new Set()) =>
|
|
104
|
-
* `${start}->${end}:[${[...visited].toSorted().join(',')}]`
|
|
105
|
-
* );
|
|
106
|
-
* ```
|
|
107
|
-
*
|
|
108
|
-
* @example Cache key strategies
|
|
109
|
-
* ```typescript
|
|
110
|
-
* // 1. Simple primitive argument
|
|
111
|
-
* memoizeFunction(fn, (x: number) => x);
|
|
112
|
-
*
|
|
113
|
-
* // 2. Multiple arguments with separator
|
|
114
|
-
* memoizeFunction(fn, (a: string, b: number) => `${a}|${b}`);
|
|
115
|
-
*
|
|
116
|
-
* // 3. Object with specific fields
|
|
117
|
-
* memoizeFunction(fn, (obj: { id: number; version: number }) =>
|
|
118
|
-
* `${obj.id}:v${obj.version}`
|
|
119
|
-
* );
|
|
120
|
-
*
|
|
121
|
-
* // 4. Array argument with JSON serialization
|
|
122
|
-
* memoizeFunction(fn, (arr: number[]) => JSON.stringify(arr));
|
|
123
|
-
*
|
|
124
|
-
* // 5. Boolean flags as bit field
|
|
125
|
-
* memoizeFunction(fn, (a: boolean, b: boolean, c: boolean) =>
|
|
126
|
-
* (a ? 4 : 0) | (b ? 2 : 0) | (c ? 1 : 0)
|
|
127
|
-
* );
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
* @example Memory-conscious memoization with weak references
|
|
131
|
-
* ```typescript
|
|
132
|
-
* // For object keys, consider using WeakMap externally
|
|
133
|
-
* const cache = new WeakMap<object, Result>();
|
|
134
|
-
*
|
|
135
|
-
* function memoizeWithWeakMap<T extends object, R>(
|
|
136
|
-
* fn: (obj: T) => R
|
|
137
|
-
* ): (obj: T) => R {
|
|
138
|
-
* return (obj: T): R => {
|
|
139
|
-
* if (cache.has(obj)) {
|
|
140
|
-
* return cache.get(obj)!;
|
|
141
|
-
* }
|
|
142
|
-
* const result = fn(obj);
|
|
143
|
-
* cache.set(obj, result);
|
|
144
|
-
* return result;
|
|
145
|
-
* };
|
|
146
|
-
* }
|
|
147
|
-
* ```
|
|
148
|
-
*
|
|
149
|
-
* @example Anti-patterns to avoid
|
|
150
|
-
* ```typescript
|
|
151
|
-
* // ❌ Bad: Memoizing impure functions
|
|
152
|
-
* const memoizedRandom = memoizeFunction(
|
|
153
|
-
* () => Math.random(),
|
|
154
|
-
* () => 'key' // Always returns cached random value!
|
|
155
|
-
* );
|
|
156
|
-
*
|
|
157
|
-
* // ❌ Bad: Memoizing functions with side effects
|
|
158
|
-
* const memoizedLog = memoizeFunction(
|
|
159
|
-
* (msg: string) => { console.log(msg); return msg; },
|
|
160
|
-
* (msg) => msg // Logs only on first call!
|
|
161
|
-
* );
|
|
162
|
-
*
|
|
163
|
-
* // ❌ Bad: Non-unique cache keys
|
|
164
|
-
* const memoizedProcess = memoizeFunction(
|
|
165
|
-
* (user: User) => processUser(user),
|
|
166
|
-
* (user) => user.name // Multiple users can have same name!
|
|
167
|
-
* );
|
|
168
|
-
* ```
|
|
169
|
-
*
|
|
170
27
|
* @see https://en.wikipedia.org/wiki/Memoization
|
|
171
28
|
*/
|
|
172
29
|
export declare const memoizeFunction: <const A extends readonly unknown[], R, K extends Primitive>(fn: (...args: A) => R, argsToCacheKey: (...args: A) => K) => ((...args: A) => R);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memoize-function.d.mts","sourceRoot":"","sources":["../../src/others/memoize-function.mts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"memoize-function.d.mts","sourceRoot":"","sources":["../../src/others/memoize-function.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,eAAe,GAC1B,KAAK,CAAC,CAAC,SAAS,SAAS,OAAO,EAAE,EAClC,CAAC,EACD,CAAC,SAAS,SAAS,EAEnB,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,EACrB,gBAAgB,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,KAChC,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,CAiBpB,CAAC"}
|
|
@@ -1,172 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Creates a memoized version of a function that caches results based on input
|
|
2
|
+
* Creates a memoized version of a function that caches results based on input
|
|
3
|
+
* arguments.
|
|
3
4
|
*
|
|
4
|
-
* The memoized function stores results in an internal Map and returns cached
|
|
5
|
-
* for repeated calls with the same arguments. This can significantly
|
|
6
|
-
* for expensive computations or I/O operations.
|
|
5
|
+
* The memoized function stores results in an internal Map and returns cached
|
|
6
|
+
* values for repeated calls with the same arguments. This can significantly
|
|
7
|
+
* improve performance for expensive computations or I/O operations.
|
|
7
8
|
*
|
|
8
9
|
* **Important considerations:**
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
10
|
+
*
|
|
11
|
+
* - The cache grows unbounded - consider memory implications for long-running
|
|
12
|
+
* applications
|
|
13
|
+
* - Cache keys must be primitives (string, number, boolean, symbol, null,
|
|
14
|
+
* undefined, bigint)
|
|
11
15
|
* - Object arguments require careful key generation to ensure uniqueness
|
|
12
16
|
* - Pure functions only - memoizing functions with side effects can lead to bugs
|
|
13
17
|
*
|
|
14
18
|
* @template A - The tuple type of the function arguments
|
|
15
19
|
* @template R - The return type of the function
|
|
16
|
-
* @template K - The primitive type used as the cache key (must be valid Map
|
|
20
|
+
* @template K - The primitive type used as the cache key (must be valid Map
|
|
21
|
+
* key)
|
|
17
22
|
* @param fn - The pure function to memoize
|
|
18
|
-
* @param argsToCacheKey - Function that converts arguments to a unique cache
|
|
23
|
+
* @param argsToCacheKey - Function that converts arguments to a unique cache
|
|
24
|
+
* key
|
|
19
25
|
* @returns A memoized version of the input function with the same signature
|
|
20
26
|
*
|
|
21
|
-
* @example Basic memoization for expensive calculations
|
|
22
|
-
* ```typescript
|
|
23
|
-
* // Fibonacci calculation (exponential time complexity)
|
|
24
|
-
* const fibonacci = (n: number): number => {
|
|
25
|
-
* console.log(`Computing fib(${n})`);
|
|
26
|
-
* if (n <= 1) return n;
|
|
27
|
-
* return fibonacci(n - 1) + fibonacci(n - 2);
|
|
28
|
-
* };
|
|
29
|
-
*
|
|
30
|
-
* const memoizedFib = memoizeFunction(
|
|
31
|
-
* fibonacci,
|
|
32
|
-
* (n) => n // Number itself as key
|
|
33
|
-
* );
|
|
34
|
-
*
|
|
35
|
-
* memoizedFib(40); // Much faster than unmemoized version
|
|
36
|
-
* memoizedFib(40); // Returns instantly from cache
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @example Multi-argument functions with composite keys
|
|
40
|
-
* ```typescript
|
|
41
|
-
* // Grid calculation with x,y coordinates
|
|
42
|
-
* const calculateGridValue = (x: number, y: number, scale: number): number => {
|
|
43
|
-
* console.log(`Computing grid(${x},${y},${scale})`);
|
|
44
|
-
* // Expensive computation...
|
|
45
|
-
* return Math.sin(x * scale) * Math.cos(y * scale);
|
|
46
|
-
* };
|
|
47
|
-
*
|
|
48
|
-
* const memoizedGrid = memoizeFunction(
|
|
49
|
-
* calculateGridValue,
|
|
50
|
-
* (x, y, scale) => `${x},${y},${scale}` // String concatenation for composite key
|
|
51
|
-
* );
|
|
52
|
-
*
|
|
53
|
-
* // Alternative: Using bit manipulation for integer coordinates
|
|
54
|
-
* const memoizedGrid2 = memoizeFunction(
|
|
55
|
-
* calculateGridValue,
|
|
56
|
-
* (x, y, scale) => (x << 20) | (y << 10) | scale // Assuming small positive integers
|
|
57
|
-
* );
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* @example Object arguments with selective memoization
|
|
61
|
-
* ```typescript
|
|
62
|
-
* interface User {
|
|
63
|
-
* id: number;
|
|
64
|
-
* name: string;
|
|
65
|
-
* email: string;
|
|
66
|
-
* metadata?: Record<string, unknown>;
|
|
67
|
-
* }
|
|
68
|
-
*
|
|
69
|
-
* const fetchUserPermissions = async (user: User): Promise<string[]> => {
|
|
70
|
-
* console.log(`Fetching permissions for user ${user.id}`);
|
|
71
|
-
* const response = await api.get(`/permissions/${user.id}`);
|
|
72
|
-
* return response.data;
|
|
73
|
-
* };
|
|
74
|
-
*
|
|
75
|
-
* // Memoize based only on user ID, ignoring other fields
|
|
76
|
-
* const memoizedFetchPermissions = memoizeFunction(
|
|
77
|
-
* fetchUserPermissions,
|
|
78
|
-
* (user) => user.id // Only cache by ID
|
|
79
|
-
* );
|
|
80
|
-
*
|
|
81
|
-
* // For multiple identifying fields
|
|
82
|
-
* const processUserData = (user: User, orgId: number): ProcessedData => {
|
|
83
|
-
* // Complex processing...
|
|
84
|
-
* };
|
|
85
|
-
*
|
|
86
|
-
* const memoizedProcess = memoizeFunction(
|
|
87
|
-
* processUserData,
|
|
88
|
-
* (user, orgId) => `${user.id}:${orgId}` // Composite key with separator
|
|
89
|
-
* );
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* @example Memoizing recursive functions
|
|
93
|
-
* ```typescript
|
|
94
|
-
* // Recursive path finding
|
|
95
|
-
* const findPaths = (start: string, end: string, visited: Set<string> = new Set()): string[][] => {
|
|
96
|
-
* if (start === end) return [[end]];
|
|
97
|
-
* // ... complex recursive logic
|
|
98
|
-
* };
|
|
99
|
-
*
|
|
100
|
-
* // Use sorted, serialized visited set for consistent keys
|
|
101
|
-
* const memoizedFindPaths = memoizeFunction(
|
|
102
|
-
* findPaths,
|
|
103
|
-
* (start, end, visited = new Set()) =>
|
|
104
|
-
* `${start}->${end}:[${[...visited].toSorted().join(',')}]`
|
|
105
|
-
* );
|
|
106
|
-
* ```
|
|
107
|
-
*
|
|
108
|
-
* @example Cache key strategies
|
|
109
|
-
* ```typescript
|
|
110
|
-
* // 1. Simple primitive argument
|
|
111
|
-
* memoizeFunction(fn, (x: number) => x);
|
|
112
|
-
*
|
|
113
|
-
* // 2. Multiple arguments with separator
|
|
114
|
-
* memoizeFunction(fn, (a: string, b: number) => `${a}|${b}`);
|
|
115
|
-
*
|
|
116
|
-
* // 3. Object with specific fields
|
|
117
|
-
* memoizeFunction(fn, (obj: { id: number; version: number }) =>
|
|
118
|
-
* `${obj.id}:v${obj.version}`
|
|
119
|
-
* );
|
|
120
|
-
*
|
|
121
|
-
* // 4. Array argument with JSON serialization
|
|
122
|
-
* memoizeFunction(fn, (arr: number[]) => JSON.stringify(arr));
|
|
123
|
-
*
|
|
124
|
-
* // 5. Boolean flags as bit field
|
|
125
|
-
* memoizeFunction(fn, (a: boolean, b: boolean, c: boolean) =>
|
|
126
|
-
* (a ? 4 : 0) | (b ? 2 : 0) | (c ? 1 : 0)
|
|
127
|
-
* );
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
* @example Memory-conscious memoization with weak references
|
|
131
|
-
* ```typescript
|
|
132
|
-
* // For object keys, consider using WeakMap externally
|
|
133
|
-
* const cache = new WeakMap<object, Result>();
|
|
134
|
-
*
|
|
135
|
-
* function memoizeWithWeakMap<T extends object, R>(
|
|
136
|
-
* fn: (obj: T) => R
|
|
137
|
-
* ): (obj: T) => R {
|
|
138
|
-
* return (obj: T): R => {
|
|
139
|
-
* if (cache.has(obj)) {
|
|
140
|
-
* return cache.get(obj)!;
|
|
141
|
-
* }
|
|
142
|
-
* const result = fn(obj);
|
|
143
|
-
* cache.set(obj, result);
|
|
144
|
-
* return result;
|
|
145
|
-
* };
|
|
146
|
-
* }
|
|
147
|
-
* ```
|
|
148
|
-
*
|
|
149
|
-
* @example Anti-patterns to avoid
|
|
150
|
-
* ```typescript
|
|
151
|
-
* // ❌ Bad: Memoizing impure functions
|
|
152
|
-
* const memoizedRandom = memoizeFunction(
|
|
153
|
-
* () => Math.random(),
|
|
154
|
-
* () => 'key' // Always returns cached random value!
|
|
155
|
-
* );
|
|
156
|
-
*
|
|
157
|
-
* // ❌ Bad: Memoizing functions with side effects
|
|
158
|
-
* const memoizedLog = memoizeFunction(
|
|
159
|
-
* (msg: string) => { console.log(msg); return msg; },
|
|
160
|
-
* (msg) => msg // Logs only on first call!
|
|
161
|
-
* );
|
|
162
|
-
*
|
|
163
|
-
* // ❌ Bad: Non-unique cache keys
|
|
164
|
-
* const memoizedProcess = memoizeFunction(
|
|
165
|
-
* (user: User) => processUser(user),
|
|
166
|
-
* (user) => user.name // Multiple users can have same name!
|
|
167
|
-
* );
|
|
168
|
-
* ```
|
|
169
|
-
*
|
|
170
27
|
* @see https://en.wikipedia.org/wiki/Memoization
|
|
171
28
|
*/
|
|
172
29
|
const memoizeFunction = (fn, argsToCacheKey) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memoize-function.mjs","sources":["../../src/others/memoize-function.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"memoize-function.mjs","sources":["../../src/others/memoize-function.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;MACU,eAAe,GAAG,CAK7B,EAAqB,EACrB,cAAiC,KACV;AACvB,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAQ;AAEjC,IAAA,OAAO,CAAC,GAAG,IAAO,KAAO;AACvB,QAAA,MAAM,GAAG,GAAG,cAAc,CAAC,GAAG,IAAI,CAAC;AAEnC,QAAA,IAAI,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;;AAEtB,YAAA,OAAO,SAAS,CAAC,GAAG,CAAC,GAAG,CAAE;QAC5B;aAAO;AACL,YAAA,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC;AAE1B,YAAA,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC;AAE1B,YAAA,OAAO,MAAM;QACf;AACF,IAAA,CAAC;AACH;;;;"}
|