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
|
@@ -3,24 +3,29 @@ import { range } from '../iterator/index.mjs';
|
|
|
3
3
|
import { asUint32, Uint32 } from '../number/index.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Interface for a high-performance stack with LIFO (Last-In, First-Out)
|
|
6
|
+
* Interface for a high-performance stack with LIFO (Last-In, First-Out)
|
|
7
|
+
* behavior.
|
|
7
8
|
*
|
|
8
|
-
* This interface defines a mutable stack data structure where elements are
|
|
9
|
-
* from the top, following the Last-In, First-Out
|
|
10
|
-
*
|
|
9
|
+
* This interface defines a mutable stack data structure where elements are
|
|
10
|
+
* added to and removed from the top, following the Last-In, First-Out
|
|
11
|
+
* principle. The implementation uses a dynamic array for optimal performance,
|
|
12
|
+
* providing O(1) operations for both push and pop operations.
|
|
11
13
|
*
|
|
12
14
|
* **LIFO Behavior:**
|
|
15
|
+
*
|
|
13
16
|
* - **push**: Adds elements to the top of the stack
|
|
14
17
|
* - **pop**: Removes and returns elements from the top of the stack
|
|
15
18
|
* - The last element added is the first element to be removed
|
|
16
19
|
*
|
|
17
20
|
* **Performance Characteristics:**
|
|
18
|
-
*
|
|
19
|
-
* -
|
|
20
|
-
* -
|
|
21
|
+
*
|
|
22
|
+
* - Push: O(1) amortized (O(n) when buffer needs resizing)
|
|
23
|
+
* - Pop: O(1) always
|
|
24
|
+
* - Size/isEmpty: O(1) always
|
|
21
25
|
* - Memory efficient with automatic garbage collection of removed elements
|
|
22
26
|
*
|
|
23
27
|
* **Use Cases:**
|
|
28
|
+
*
|
|
24
29
|
* - Function call management and recursion
|
|
25
30
|
* - Undo/redo functionality
|
|
26
31
|
* - Expression evaluation and parsing
|
|
@@ -29,66 +34,138 @@ import { asUint32, Uint32 } from '../number/index.mjs';
|
|
|
29
34
|
* - Browser history management
|
|
30
35
|
*
|
|
31
36
|
* @template T The type of elements stored in the stack.
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* ```typescript
|
|
35
|
-
* import { createStack, Stack } from './stack';
|
|
36
|
-
*
|
|
37
|
-
* // Example 1: Basic LIFO operations
|
|
38
|
-
* const operationStack: Stack<string> = createStack<string>();
|
|
39
|
-
*
|
|
40
|
-
* operationStack.push("operation1"); // Add to top
|
|
41
|
-
* operationStack.push("operation2"); // Add to top
|
|
42
|
-
* operationStack.push("operation3"); // Add to top
|
|
43
|
-
*
|
|
44
|
-
* console.log(operationStack.size); // Output: 3
|
|
45
|
-
*
|
|
46
|
-
* // Process operations in LIFO order
|
|
47
|
-
* console.log(operationStack.pop().unwrap()); // "operation3" (last in, first out)
|
|
48
|
-
* console.log(operationStack.pop().unwrap()); // "operation2"
|
|
49
|
-
* console.log(operationStack.size); // Output: 1
|
|
50
|
-
*
|
|
51
|
-
* // Example 2: Undo functionality
|
|
52
|
-
* type Action = { type: string; data: any; timestamp: number };
|
|
53
|
-
* const undoStack: Stack<Action> = createStack<Action>();
|
|
54
|
-
*
|
|
55
|
-
* undoStack.push({ type: "delete", data: { id: 123 }, timestamp: Date.now() });
|
|
56
|
-
* undoStack.push({ type: "edit", data: { field: "name", oldValue: "old" }, timestamp: Date.now() });
|
|
57
|
-
*
|
|
58
|
-
* // Undo last action
|
|
59
|
-
* if (!undoStack.isEmpty) {
|
|
60
|
-
* const lastAction = undoStack.pop().unwrap();
|
|
61
|
-
* console.log(`Undoing: ${lastAction.type}`);
|
|
62
|
-
* }
|
|
63
|
-
* ```
|
|
64
37
|
*/
|
|
65
38
|
export type Stack<T> = Readonly<{
|
|
66
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Checks if the stack is empty.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const stack = createStack<string>();
|
|
46
|
+
*
|
|
47
|
+
* assert.ok(stack.isEmpty);
|
|
48
|
+
* assert(stack.size === 0);
|
|
49
|
+
*
|
|
50
|
+
* stack.push('first');
|
|
51
|
+
* // eslint-disable-next-line unicorn/prefer-single-call
|
|
52
|
+
* stack.push('second');
|
|
53
|
+
*
|
|
54
|
+
* assert.notOk(stack.isEmpty);
|
|
55
|
+
* assert(stack.size === 2);
|
|
56
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('second'));
|
|
57
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('first'));
|
|
58
|
+
* assert.deepStrictEqual(stack.pop(), Optional.none);
|
|
59
|
+
*
|
|
60
|
+
* const seededStack = createStack([10, 20, 30]);
|
|
61
|
+
*
|
|
62
|
+
* assert(seededStack.size === 3);
|
|
63
|
+
* assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
67
66
|
isEmpty: boolean;
|
|
68
67
|
|
|
69
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* The number of elements in the stack.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* const stack = createStack<string>();
|
|
75
|
+
*
|
|
76
|
+
* assert.ok(stack.isEmpty);
|
|
77
|
+
* assert(stack.size === 0);
|
|
78
|
+
*
|
|
79
|
+
* stack.push('first');
|
|
80
|
+
* // eslint-disable-next-line unicorn/prefer-single-call
|
|
81
|
+
* stack.push('second');
|
|
82
|
+
*
|
|
83
|
+
* assert.notOk(stack.isEmpty);
|
|
84
|
+
* assert(stack.size === 2);
|
|
85
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('second'));
|
|
86
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('first'));
|
|
87
|
+
* assert.deepStrictEqual(stack.pop(), Optional.none);
|
|
88
|
+
*
|
|
89
|
+
* const seededStack = createStack([10, 20, 30]);
|
|
90
|
+
*
|
|
91
|
+
* assert(seededStack.size === 3);
|
|
92
|
+
* assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
70
95
|
size: SizeType.Arr;
|
|
71
96
|
|
|
72
97
|
/**
|
|
73
98
|
* Removes and returns the element at the top of the stack.
|
|
74
|
-
*
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
*
|
|
102
|
+
* ```ts
|
|
103
|
+
* const stack = createStack<string>();
|
|
104
|
+
*
|
|
105
|
+
* assert.ok(stack.isEmpty);
|
|
106
|
+
* assert(stack.size === 0);
|
|
107
|
+
*
|
|
108
|
+
* stack.push('first');
|
|
109
|
+
* // eslint-disable-next-line unicorn/prefer-single-call
|
|
110
|
+
* stack.push('second');
|
|
111
|
+
*
|
|
112
|
+
* assert.notOk(stack.isEmpty);
|
|
113
|
+
* assert(stack.size === 2);
|
|
114
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('second'));
|
|
115
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('first'));
|
|
116
|
+
* assert.deepStrictEqual(stack.pop(), Optional.none);
|
|
117
|
+
*
|
|
118
|
+
* const seededStack = createStack([10, 20, 30]);
|
|
119
|
+
*
|
|
120
|
+
* assert(seededStack.size === 3);
|
|
121
|
+
* assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @returns The element at the top of the stack, or `Optional.none` if the
|
|
125
|
+
* stack is empty.
|
|
75
126
|
*/
|
|
76
127
|
pop: () => Optional<T>;
|
|
77
128
|
|
|
78
129
|
/**
|
|
79
130
|
* Adds an element to the top of the stack.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
* const stack = createStack<string>();
|
|
136
|
+
*
|
|
137
|
+
* assert.ok(stack.isEmpty);
|
|
138
|
+
* assert(stack.size === 0);
|
|
139
|
+
*
|
|
140
|
+
* stack.push('first');
|
|
141
|
+
* // eslint-disable-next-line unicorn/prefer-single-call
|
|
142
|
+
* stack.push('second');
|
|
143
|
+
*
|
|
144
|
+
* assert.notOk(stack.isEmpty);
|
|
145
|
+
* assert(stack.size === 2);
|
|
146
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('second'));
|
|
147
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('first'));
|
|
148
|
+
* assert.deepStrictEqual(stack.pop(), Optional.none);
|
|
149
|
+
*
|
|
150
|
+
* const seededStack = createStack([10, 20, 30]);
|
|
151
|
+
*
|
|
152
|
+
* assert(seededStack.size === 3);
|
|
153
|
+
* assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
80
156
|
* @param value The element to add.
|
|
81
157
|
*/
|
|
82
158
|
push: (value: T) => void;
|
|
83
159
|
}>;
|
|
84
160
|
|
|
85
161
|
/**
|
|
86
|
-
* Class implementation for a stack with LIFO (Last-In, First-Out) behavior
|
|
87
|
-
* This implementation provides O(1) amortized push and
|
|
88
|
-
* that grows as needed.
|
|
162
|
+
* Class implementation for a stack with LIFO (Last-In, First-Out) behavior
|
|
163
|
+
* using a dynamic array. This implementation provides O(1) amortized push and
|
|
164
|
+
* O(1) pop operations by using a resizable buffer that grows as needed.
|
|
89
165
|
*
|
|
90
|
-
* The underlying array automatically resizes when it becomes full, ensuring
|
|
91
|
-
* can grow to accommodate any number of elements while
|
|
166
|
+
* The underlying array automatically resizes when it becomes full, ensuring
|
|
167
|
+
* that the stack can grow to accommodate any number of elements while
|
|
168
|
+
* maintaining efficient operations.
|
|
92
169
|
*
|
|
93
170
|
* @template T The type of elements in the stack.
|
|
94
171
|
* @implements Stack
|
|
@@ -108,6 +185,7 @@ class StackClass<T> implements Stack<T> {
|
|
|
108
185
|
|
|
109
186
|
/**
|
|
110
187
|
* Constructs a new StackClass instance.
|
|
188
|
+
*
|
|
111
189
|
* @param initialValues Optional initial values to populate the stack.
|
|
112
190
|
*/
|
|
113
191
|
constructor(initialValues: readonly T[] = []) {
|
|
@@ -141,50 +219,16 @@ class StackClass<T> implements Stack<T> {
|
|
|
141
219
|
/**
|
|
142
220
|
* Removes and returns the element at the top of the stack (LIFO).
|
|
143
221
|
*
|
|
144
|
-
* This operation removes the element that was added most recently (last-in)
|
|
145
|
-
* If the stack is empty, returns `Optional.none`. The
|
|
146
|
-
* and does not require any array resizing
|
|
147
|
-
*
|
|
148
|
-
* **Time Complexity:** O(1) - constant time operation
|
|
149
|
-
* **Space Complexity:** O(1) - no additional memory allocation
|
|
222
|
+
* This operation removes the element that was added most recently (last-in)
|
|
223
|
+
* and returns it. If the stack is empty, returns `Optional.none`. The
|
|
224
|
+
* operation is guaranteed to be O(1) and does not require any array resizing
|
|
225
|
+
* or memory reallocation.
|
|
150
226
|
*
|
|
151
|
-
*
|
|
227
|
+
* **Time Complexity:** O(1) - constant time operation **Space Complexity:**
|
|
228
|
+
* O(1) - no additional memory allocation
|
|
152
229
|
*
|
|
153
|
-
* @
|
|
154
|
-
*
|
|
155
|
-
* const stack = createStack<string>();
|
|
156
|
-
*
|
|
157
|
-
* // Add some elements
|
|
158
|
-
* stack.push("bottom");
|
|
159
|
-
* stack.push("middle");
|
|
160
|
-
* stack.push("top");
|
|
161
|
-
*
|
|
162
|
-
* // Remove elements in LIFO order
|
|
163
|
-
* const top = stack.pop();
|
|
164
|
-
* if (top.isSome) {
|
|
165
|
-
* console.log(top.value); // "top" (last pushed, first popped)
|
|
166
|
-
* }
|
|
167
|
-
*
|
|
168
|
-
* const middle = stack.pop().unwrap(); // "middle"
|
|
169
|
-
* console.log(stack.size); // 1
|
|
170
|
-
*
|
|
171
|
-
* // Safe handling of empty stack
|
|
172
|
-
* const emptyStack = createStack<number>();
|
|
173
|
-
* const result = emptyStack.pop();
|
|
174
|
-
* if (result.isNone) {
|
|
175
|
-
* console.log("Stack is empty");
|
|
176
|
-
* }
|
|
177
|
-
*
|
|
178
|
-
* // Typical usage in algorithms
|
|
179
|
-
* const pathStack = createStack<string>();
|
|
180
|
-
* pathStack.push("/home");
|
|
181
|
-
* pathStack.push("/users");
|
|
182
|
-
* pathStack.push("/documents");
|
|
183
|
-
*
|
|
184
|
-
* // Backtrack one level
|
|
185
|
-
* const currentDir = pathStack.pop().unwrap(); // "/documents"
|
|
186
|
-
* const parentDir = pathStack.pop().unwrap(); // "/users"
|
|
187
|
-
* ```
|
|
230
|
+
* @returns An Optional containing the removed element, or `Optional.none` if
|
|
231
|
+
* the stack is empty.
|
|
188
232
|
*/
|
|
189
233
|
pop(): Optional<T> {
|
|
190
234
|
if (this.isEmpty) {
|
|
@@ -202,60 +246,19 @@ class StackClass<T> implements Stack<T> {
|
|
|
202
246
|
/**
|
|
203
247
|
* Adds an element to the top of the stack (LIFO).
|
|
204
248
|
*
|
|
205
|
-
* This operation adds the element to the top of the stack, where it will be
|
|
206
|
-
* to be popped (last-in, first-out ordering). The operation is
|
|
207
|
-
* meaning it's O(1) for most operations with occasional O(n)
|
|
249
|
+
* This operation adds the element to the top of the stack, where it will be
|
|
250
|
+
* the first to be popped (last-in, first-out ordering). The operation is
|
|
251
|
+
* amortized O(1), meaning it's O(1) for most operations with occasional O(n)
|
|
252
|
+
* when the buffer needs resizing.
|
|
208
253
|
*
|
|
209
|
-
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
|
|
210
|
-
* **Space Complexity:** O(1) - constant additional memory per element
|
|
254
|
+
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is
|
|
255
|
+
* needed **Space Complexity:** O(1) - constant additional memory per element
|
|
211
256
|
*
|
|
212
|
-
* **Buffer Resizing:** When the internal buffer becomes full, it
|
|
213
|
-
* in size and copies existing elements to maintain the
|
|
257
|
+
* **Buffer Resizing:** When the internal buffer becomes full, it
|
|
258
|
+
* automatically doubles in size and copies existing elements to maintain the
|
|
259
|
+
* stack structure.
|
|
214
260
|
*
|
|
215
261
|
* @param value The element to add to the top of the stack.
|
|
216
|
-
*
|
|
217
|
-
* @example
|
|
218
|
-
* ```typescript
|
|
219
|
-
* const actionStack = createStack<string>();
|
|
220
|
-
*
|
|
221
|
-
* // Add actions in chronological order
|
|
222
|
-
* actionStack.push("open file"); // O(1)
|
|
223
|
-
* actionStack.push("edit content"); // O(1)
|
|
224
|
-
* actionStack.push("save file"); // O(1)
|
|
225
|
-
*
|
|
226
|
-
* console.log(actionStack.size); // 3
|
|
227
|
-
*
|
|
228
|
-
* // Actions will be undone in reverse order (LIFO)
|
|
229
|
-
* while (!actionStack.isEmpty) {
|
|
230
|
-
* const action = actionStack.pop().unwrap();
|
|
231
|
-
* console.log(`Undoing: ${action}`);
|
|
232
|
-
* }
|
|
233
|
-
* // Output:
|
|
234
|
-
* // Undoing: save file
|
|
235
|
-
* // Undoing: edit content
|
|
236
|
-
* // Undoing: open file
|
|
237
|
-
*
|
|
238
|
-
* // High-volume pushing (demonstrates amortized O(1) performance)
|
|
239
|
-
* const dataStack = createStack<number>();
|
|
240
|
-
*
|
|
241
|
-
* for (const i of range(1000000)) {
|
|
242
|
-
* dataStack.push(i); // Each operation is O(1) amortized
|
|
243
|
-
* }
|
|
244
|
-
*
|
|
245
|
-
* console.log(dataStack.size); // 1000000
|
|
246
|
-
*
|
|
247
|
-
* // Function call stack simulation
|
|
248
|
-
* type StackFrame = { function: string; variables: Record<string, any> };
|
|
249
|
-
* const callStack = createStack<StackFrame>();
|
|
250
|
-
*
|
|
251
|
-
* callStack.push({ function: "main", variables: { argc: 1, argv: ["program"] } });
|
|
252
|
-
* callStack.push({ function: "process", variables: { data: [1, 2, 3] } });
|
|
253
|
-
* callStack.push({ function: "validate", variables: { input: "test" } });
|
|
254
|
-
*
|
|
255
|
-
* // Current function context is at the top
|
|
256
|
-
* const currentFrame = callStack.pop().unwrap();
|
|
257
|
-
* console.log(`Current function: ${currentFrame.function}`);
|
|
258
|
-
* ```
|
|
259
262
|
*/
|
|
260
263
|
push(value: T): void {
|
|
261
264
|
// Resize if buffer is full
|
|
@@ -290,13 +293,16 @@ class StackClass<T> implements Stack<T> {
|
|
|
290
293
|
}
|
|
291
294
|
|
|
292
295
|
/**
|
|
293
|
-
* Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a
|
|
296
|
+
* Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a
|
|
297
|
+
* high-performance dynamic array.
|
|
294
298
|
*
|
|
295
|
-
* This factory function creates an optimized stack implementation that
|
|
296
|
-
* characteristics for both push and pop
|
|
297
|
-
*
|
|
299
|
+
* This factory function creates an optimized stack implementation that
|
|
300
|
+
* maintains excellent performance characteristics for both push and pop
|
|
301
|
+
* operations. The underlying dynamic array automatically resizes to accommodate
|
|
302
|
+
* growing workloads while providing predictable O(1) operations.
|
|
298
303
|
*
|
|
299
304
|
* **Implementation Features:**
|
|
305
|
+
*
|
|
300
306
|
* - **O(1) push operations** (amortized - occasionally O(n) when resizing)
|
|
301
307
|
* - **O(1) pop operations** (always)
|
|
302
308
|
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
@@ -304,121 +310,42 @@ class StackClass<T> implements Stack<T> {
|
|
|
304
310
|
* - **Dynamic array design** - eliminates need for complex memory management
|
|
305
311
|
*
|
|
306
312
|
* **Performance Benefits:**
|
|
313
|
+
*
|
|
307
314
|
* - No array shifting required for stack operations
|
|
308
315
|
* - Minimal memory allocation overhead
|
|
309
316
|
* - Predictable performance under high load
|
|
310
317
|
* - Efficient memory usage with automatic cleanup
|
|
311
318
|
*
|
|
312
|
-
* @template T The type of elements stored in the stack.
|
|
313
|
-
* @param initialValues Optional array of initial elements to populate the stack.
|
|
314
|
-
* Elements will be popped in reverse order of how they appear in the array
|
|
315
|
-
* (last array element will be popped first).
|
|
316
|
-
* If provided, the initial buffer capacity will be at least twice the array length.
|
|
317
|
-
* @returns A new Stack instance optimized for high-performance LIFO operations.
|
|
318
|
-
*
|
|
319
319
|
* @example
|
|
320
|
-
* ```typescript
|
|
321
|
-
* import { createStack } from './stack';
|
|
322
|
-
*
|
|
323
|
-
* // Example 1: Function call simulation
|
|
324
|
-
* type FunctionCall = { name: string; args: any[]; context: any };
|
|
325
|
-
* const callStack = createStack<FunctionCall>();
|
|
326
|
-
*
|
|
327
|
-
* // Simulate function calls (push onto stack)
|
|
328
|
-
* callStack.push({ name: "main", args: [], context: {} }); // O(1)
|
|
329
|
-
* callStack.push({ name: "processData", args: [data], context: {} }); // O(1)
|
|
330
|
-
* callStack.push({ name: "validateInput", args: [input], context: {} }); // O(1)
|
|
331
|
-
*
|
|
332
|
-
* // Simulate function returns (pop from stack)
|
|
333
|
-
* while (!callStack.isEmpty) {
|
|
334
|
-
* const call = callStack.pop().unwrap(); // O(1)
|
|
335
|
-
* console.log(`Returning from: ${call.name}`);
|
|
336
|
-
* }
|
|
337
|
-
* // Output:
|
|
338
|
-
* // Returning from: validateInput
|
|
339
|
-
* // Returning from: processData
|
|
340
|
-
* // Returning from: main
|
|
341
|
-
*
|
|
342
|
-
* // Example 2: Expression evaluation with operator precedence
|
|
343
|
-
* const operatorStack = createStack<string>();
|
|
344
|
-
* const operandStack = createStack<number>();
|
|
345
320
|
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
* operatorStack.push("+");
|
|
349
|
-
* operandStack.push(4);
|
|
350
|
-
* operatorStack.push("*"); // Higher precedence
|
|
351
|
-
* operandStack.push(2);
|
|
321
|
+
* ```ts
|
|
322
|
+
* const stack = createStack<string>();
|
|
352
323
|
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
* const b = operandStack.pop().unwrap(); // 2
|
|
356
|
-
* const a = operandStack.pop().unwrap(); // 4
|
|
357
|
-
* operandStack.push(a * b); // Push result: 8
|
|
324
|
+
* assert.ok(stack.isEmpty);
|
|
325
|
+
* assert(stack.size === 0);
|
|
358
326
|
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
* position: number;
|
|
363
|
-
* oldValue: string;
|
|
364
|
-
* newValue: string;
|
|
365
|
-
* };
|
|
327
|
+
* stack.push('first');
|
|
328
|
+
* // eslint-disable-next-line unicorn/prefer-single-call
|
|
329
|
+
* stack.push('second');
|
|
366
330
|
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
331
|
+
* assert.notOk(stack.isEmpty);
|
|
332
|
+
* assert(stack.size === 2);
|
|
333
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('second'));
|
|
334
|
+
* assert.deepStrictEqual(stack.pop(), Optional.some('first'));
|
|
335
|
+
* assert.deepStrictEqual(stack.pop(), Optional.none);
|
|
369
336
|
*
|
|
370
|
-
*
|
|
371
|
-
* const edit1: EditAction = { type: 'insert', position: 0, oldValue: '', newValue: 'Hello' };
|
|
372
|
-
* const edit2: EditAction = { type: 'insert', position: 5, oldValue: '', newValue: ' World' };
|
|
337
|
+
* const seededStack = createStack([10, 20, 30]);
|
|
373
338
|
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* // Undo last edit
|
|
378
|
-
* if (!undoStack.isEmpty) {
|
|
379
|
-
* const lastEdit = undoStack.pop().unwrap();
|
|
380
|
-
* redoStack.push(lastEdit);
|
|
381
|
-
* console.log(`Undid: ${lastEdit.type} at position ${lastEdit.position}`);
|
|
382
|
-
* }
|
|
383
|
-
*
|
|
384
|
-
* // Example 4: High-throughput data processing
|
|
385
|
-
* const processingStack = createStack<number>();
|
|
386
|
-
*
|
|
387
|
-
* // Add large amount of data (demonstrates amortized O(1) performance)
|
|
388
|
-
* for (const i of range(100000)) {
|
|
389
|
-
* processingStack.push(i); // Each push is O(1) amortized
|
|
390
|
-
* }
|
|
391
|
-
*
|
|
392
|
-
* // Process data in LIFO order
|
|
393
|
-
* let processedCount = 0;
|
|
394
|
-
* while (!processingStack.isEmpty) {
|
|
395
|
-
* const value = processingStack.pop().unwrap(); // O(1)
|
|
396
|
-
* // Process value...
|
|
397
|
-
* processedCount++;
|
|
398
|
-
* }
|
|
399
|
-
* console.log(`Processed ${processedCount} items`); // 100000
|
|
400
|
-
*
|
|
401
|
-
* // Example 5: Stack with pre-populated data
|
|
402
|
-
* const historyStack = createStack<string>([
|
|
403
|
-
* "page1.html",
|
|
404
|
-
* "page2.html",
|
|
405
|
-
* "page3.html",
|
|
406
|
-
* "page4.html"
|
|
407
|
-
* ]);
|
|
408
|
-
*
|
|
409
|
-
* console.log(historyStack.size); // Output: 4
|
|
410
|
-
*
|
|
411
|
-
* // Navigate back through history (LIFO order)
|
|
412
|
-
* while (!historyStack.isEmpty) {
|
|
413
|
-
* const page = historyStack.pop().unwrap();
|
|
414
|
-
* console.log(`Going back to: ${page}`);
|
|
415
|
-
* }
|
|
416
|
-
* // Output:
|
|
417
|
-
* // Going back to: page4.html (last added, first removed)
|
|
418
|
-
* // Going back to: page3.html
|
|
419
|
-
* // Going back to: page2.html
|
|
420
|
-
* // Going back to: page1.html
|
|
339
|
+
* assert(seededStack.size === 3);
|
|
340
|
+
* assert.deepStrictEqual(seededStack.pop(), Optional.some(30));
|
|
421
341
|
* ```
|
|
342
|
+
*
|
|
343
|
+
* @template T The type of elements stored in the stack.
|
|
344
|
+
* @param initialValues Optional array of initial elements to populate the
|
|
345
|
+
* stack. Elements will be popped in reverse order of how they appear in the
|
|
346
|
+
* array (last array element will be popped first). If provided, the initial
|
|
347
|
+
* buffer capacity will be at least twice the array length.
|
|
348
|
+
* @returns A new Stack instance optimized for high-performance LIFO operations.
|
|
422
349
|
*/
|
|
423
350
|
export const createStack = <T,>(initialValues?: readonly T[]): Stack<T> =>
|
|
424
351
|
new StackClass<T>(initialValues);
|
|
@@ -45,7 +45,9 @@ describe('Stack', () => {
|
|
|
45
45
|
const stack = createStack<string>();
|
|
46
46
|
|
|
47
47
|
stack.push('first');
|
|
48
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
48
49
|
stack.push('second');
|
|
50
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
49
51
|
stack.push('third');
|
|
50
52
|
|
|
51
53
|
expect(stack.size).toBe(3);
|
|
@@ -98,10 +100,12 @@ describe('Stack', () => {
|
|
|
98
100
|
const stack = createStack<string>();
|
|
99
101
|
|
|
100
102
|
stack.push('a');
|
|
103
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
101
104
|
stack.push('b');
|
|
102
105
|
expect(Optional.unwrap(stack.pop())).toBe('b');
|
|
103
106
|
|
|
104
107
|
stack.push('c');
|
|
108
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
105
109
|
stack.push('d');
|
|
106
110
|
expect(Optional.unwrap(stack.pop())).toBe('d');
|
|
107
111
|
expect(Optional.unwrap(stack.pop())).toBe('c');
|
|
@@ -118,6 +122,7 @@ describe('Stack', () => {
|
|
|
118
122
|
const item2: Item = { id: 2, name: 'second' };
|
|
119
123
|
|
|
120
124
|
stack.push(item1);
|
|
125
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
121
126
|
stack.push(item2);
|
|
122
127
|
|
|
123
128
|
expect(Optional.unwrap(stack.pop())).toStrictEqual(item2);
|
|
@@ -127,7 +132,7 @@ describe('Stack', () => {
|
|
|
127
132
|
|
|
128
133
|
test('should handle large number of operations efficiently', () => {
|
|
129
134
|
const stack = createStack<number>();
|
|
130
|
-
const n = asPositiveSafeInt(
|
|
135
|
+
const n = asPositiveSafeInt(10_000);
|
|
131
136
|
|
|
132
137
|
// Push many elements
|
|
133
138
|
for (const i of range(n)) {
|
|
@@ -167,8 +172,11 @@ describe('Stack', () => {
|
|
|
167
172
|
const stack = createStack<string | null | undefined>();
|
|
168
173
|
|
|
169
174
|
stack.push('value');
|
|
175
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
170
176
|
stack.push(null);
|
|
177
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
171
178
|
stack.push(undefined);
|
|
179
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
172
180
|
stack.push('another');
|
|
173
181
|
|
|
174
182
|
expect(Optional.unwrap(stack.pop())).toBe('another');
|
package/src/entry-point.mts
CHANGED