ts-data-forge 1.0.0 → 1.0.2
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 +1 -1
- package/dist/array/array-utils.d.mts +2617 -0
- package/dist/array/array-utils.d.mts.map +1 -0
- package/dist/array/array-utils.mjs +2915 -0
- package/dist/array/array-utils.mjs.map +1 -0
- package/dist/array/index.d.mts +3 -0
- package/dist/array/index.d.mts.map +1 -0
- package/dist/array/index.mjs +3 -0
- package/dist/array/index.mjs.map +1 -0
- package/dist/array/tuple-utils.d.mts +421 -0
- package/dist/array/tuple-utils.d.mts.map +1 -0
- package/dist/array/tuple-utils.mjs +391 -0
- package/dist/array/tuple-utils.mjs.map +1 -0
- package/dist/collections/imap-mapped.d.mts +445 -0
- package/dist/collections/imap-mapped.d.mts.map +1 -0
- package/dist/collections/imap-mapped.mjs +424 -0
- package/dist/collections/imap-mapped.mjs.map +1 -0
- package/dist/collections/imap.d.mts +359 -0
- package/dist/collections/imap.d.mts.map +1 -0
- package/dist/collections/imap.mjs +338 -0
- package/dist/collections/imap.mjs.map +1 -0
- package/dist/collections/index.d.mts +7 -0
- package/dist/collections/index.d.mts.map +1 -0
- package/dist/collections/index.mjs +7 -0
- package/dist/collections/index.mjs.map +1 -0
- package/dist/collections/iset-mapped.d.mts +576 -0
- package/dist/collections/iset-mapped.d.mts.map +1 -0
- package/dist/collections/iset-mapped.mjs +522 -0
- package/dist/collections/iset-mapped.mjs.map +1 -0
- package/dist/collections/iset.d.mts +426 -0
- package/dist/collections/iset.d.mts.map +1 -0
- package/dist/collections/iset.mjs +437 -0
- package/dist/collections/iset.mjs.map +1 -0
- package/dist/collections/queue.d.mts +190 -0
- package/dist/collections/queue.d.mts.map +1 -0
- package/dist/collections/queue.mjs +317 -0
- package/dist/collections/queue.mjs.map +1 -0
- package/dist/collections/stack.d.mts +210 -0
- package/dist/collections/stack.d.mts.map +1 -0
- package/dist/collections/stack.mjs +353 -0
- package/dist/collections/stack.mjs.map +1 -0
- package/dist/expect-type.d.mts +199 -0
- package/dist/expect-type.d.mts.map +1 -0
- package/dist/expect-type.mjs +201 -0
- package/dist/expect-type.mjs.map +1 -0
- package/dist/functional/index.d.mts +5 -0
- package/dist/functional/index.d.mts.map +1 -0
- package/dist/functional/index.mjs +5 -0
- package/dist/functional/index.mjs.map +1 -0
- package/dist/functional/match.d.mts +215 -0
- package/dist/functional/match.d.mts.map +1 -0
- package/dist/functional/match.mjs +139 -0
- package/dist/functional/match.mjs.map +1 -0
- package/dist/functional/optional.d.mts +517 -0
- package/dist/functional/optional.d.mts.map +1 -0
- package/dist/functional/optional.mjs +532 -0
- package/dist/functional/optional.mjs.map +1 -0
- package/dist/functional/pipe.d.mts +185 -0
- package/dist/functional/pipe.d.mts.map +1 -0
- package/dist/functional/pipe.mjs +129 -0
- package/dist/functional/pipe.mjs.map +1 -0
- package/dist/functional/result.d.mts +796 -0
- package/dist/functional/result.d.mts.map +1 -0
- package/dist/functional/result.mjs +844 -0
- package/dist/functional/result.mjs.map +1 -0
- package/dist/globals.d.mts +38 -0
- package/dist/guard/has-key.d.mts +100 -0
- package/dist/guard/has-key.d.mts.map +1 -0
- package/dist/guard/has-key.mjs +94 -0
- package/dist/guard/has-key.mjs.map +1 -0
- package/dist/guard/index.d.mts +8 -0
- package/dist/guard/index.d.mts.map +1 -0
- package/dist/guard/index.mjs +8 -0
- package/dist/guard/index.mjs.map +1 -0
- package/dist/guard/is-non-empty-string.d.mts +106 -0
- package/dist/guard/is-non-empty-string.d.mts.map +1 -0
- package/dist/guard/is-non-empty-string.mjs +108 -0
- package/dist/guard/is-non-empty-string.mjs.map +1 -0
- package/dist/guard/is-non-null-object.d.mts +105 -0
- package/dist/guard/is-non-null-object.d.mts.map +1 -0
- package/dist/guard/is-non-null-object.mjs +108 -0
- package/dist/guard/is-non-null-object.mjs.map +1 -0
- package/dist/guard/is-primitive.d.mts +146 -0
- package/dist/guard/is-primitive.d.mts.map +1 -0
- package/dist/guard/is-primitive.mjs +161 -0
- package/dist/guard/is-primitive.mjs.map +1 -0
- package/dist/guard/is-record.d.mts +151 -0
- package/dist/guard/is-record.d.mts.map +1 -0
- package/dist/guard/is-record.mjs +155 -0
- package/dist/guard/is-record.mjs.map +1 -0
- package/dist/guard/is-type.d.mts +430 -0
- package/dist/guard/is-type.d.mts.map +1 -0
- package/dist/guard/is-type.mjs +432 -0
- package/dist/guard/is-type.mjs.map +1 -0
- package/dist/guard/key-is-in.d.mts +158 -0
- package/dist/guard/key-is-in.d.mts.map +1 -0
- package/dist/guard/key-is-in.mjs +160 -0
- package/dist/guard/key-is-in.mjs.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +61 -0
- package/dist/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +2 -0
- package/dist/iterator/index.d.mts.map +1 -0
- package/dist/iterator/index.mjs +2 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/iterator/range.d.mts +97 -0
- package/dist/iterator/range.d.mts.map +1 -0
- package/dist/iterator/range.mjs +130 -0
- package/dist/iterator/range.mjs.map +1 -0
- package/dist/json/index.d.mts +2 -0
- package/dist/json/index.d.mts.map +1 -0
- package/dist/json/index.mjs +2 -0
- package/dist/json/index.mjs.map +1 -0
- package/dist/json/json.d.mts +597 -0
- package/dist/json/json.d.mts.map +1 -0
- package/dist/json/json.mjs +687 -0
- package/dist/json/json.mjs.map +1 -0
- package/dist/number/branded-types/finite-number.d.mts +291 -0
- package/dist/number/branded-types/finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/finite-number.mjs +296 -0
- package/dist/number/branded-types/finite-number.mjs.map +1 -0
- package/dist/number/branded-types/index.d.mts +27 -0
- package/dist/number/branded-types/index.d.mts.map +1 -0
- package/dist/number/branded-types/index.mjs +27 -0
- package/dist/number/branded-types/index.mjs.map +1 -0
- package/dist/number/branded-types/int.d.mts +242 -0
- package/dist/number/branded-types/int.d.mts.map +1 -0
- package/dist/number/branded-types/int.mjs +239 -0
- package/dist/number/branded-types/int.mjs.map +1 -0
- package/dist/number/branded-types/int16.d.mts +162 -0
- package/dist/number/branded-types/int16.d.mts.map +1 -0
- package/dist/number/branded-types/int16.mjs +141 -0
- package/dist/number/branded-types/int16.mjs.map +1 -0
- package/dist/number/branded-types/int32.d.mts +155 -0
- package/dist/number/branded-types/int32.d.mts.map +1 -0
- package/dist/number/branded-types/int32.mjs +142 -0
- package/dist/number/branded-types/int32.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts +165 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int16.d.mts +160 -0
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int16.mjs +138 -0
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int32.d.mts +156 -0
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int32.mjs +138 -0
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts +154 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int.d.mts +131 -0
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int.mjs +128 -0
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int16.d.mts +166 -0
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int16.mjs +145 -0
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int32.d.mts +158 -0
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int32.mjs +145 -0
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts +148 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs +145 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts +160 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts +156 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -0
- package/dist/number/branded-types/positive-finite-number.d.mts +171 -0
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/positive-finite-number.mjs +165 -0
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/positive-int.d.mts +270 -0
- package/dist/number/branded-types/positive-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int.mjs +257 -0
- package/dist/number/branded-types/positive-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-int16.d.mts +162 -0
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int16.mjs +139 -0
- package/dist/number/branded-types/positive-int16.mjs.map +1 -0
- package/dist/number/branded-types/positive-int32.d.mts +158 -0
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int32.mjs +139 -0
- package/dist/number/branded-types/positive-int32.mjs.map +1 -0
- package/dist/number/branded-types/positive-safe-int.d.mts +152 -0
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-safe-int.mjs +138 -0
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint16.d.mts +160 -0
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint16.mjs +139 -0
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint32.d.mts +156 -0
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint32.mjs +139 -0
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -0
- package/dist/number/branded-types/safe-int.d.mts +243 -0
- package/dist/number/branded-types/safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/safe-int.mjs +240 -0
- package/dist/number/branded-types/safe-int.mjs.map +1 -0
- package/dist/number/branded-types/safe-uint.d.mts +151 -0
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -0
- package/dist/number/branded-types/safe-uint.mjs +138 -0
- package/dist/number/branded-types/safe-uint.mjs.map +1 -0
- package/dist/number/branded-types/uint.d.mts +144 -0
- package/dist/number/branded-types/uint.d.mts.map +1 -0
- package/dist/number/branded-types/uint.mjs +132 -0
- package/dist/number/branded-types/uint.mjs.map +1 -0
- package/dist/number/branded-types/uint16.d.mts +157 -0
- package/dist/number/branded-types/uint16.d.mts.map +1 -0
- package/dist/number/branded-types/uint16.mjs +137 -0
- package/dist/number/branded-types/uint16.mjs.map +1 -0
- package/dist/number/branded-types/uint32.d.mts +185 -0
- package/dist/number/branded-types/uint32.d.mts.map +1 -0
- package/dist/number/branded-types/uint32.mjs +169 -0
- package/dist/number/branded-types/uint32.mjs.map +1 -0
- package/dist/number/enum/index.d.mts +3 -0
- package/dist/number/enum/index.d.mts.map +1 -0
- package/dist/number/enum/index.mjs +3 -0
- package/dist/number/enum/index.mjs.map +1 -0
- package/dist/number/enum/int8.d.mts +202 -0
- package/dist/number/enum/int8.d.mts.map +1 -0
- package/dist/number/enum/int8.mjs +296 -0
- package/dist/number/enum/int8.mjs.map +1 -0
- package/dist/number/enum/uint8.d.mts +128 -0
- package/dist/number/enum/uint8.d.mts.map +1 -0
- package/dist/number/enum/uint8.mjs +251 -0
- package/dist/number/enum/uint8.mjs.map +1 -0
- package/dist/number/index.d.mts +5 -0
- package/dist/number/index.d.mts.map +1 -0
- package/dist/number/index.mjs +31 -0
- package/dist/number/index.mjs.map +1 -0
- package/dist/number/num.d.mts +515 -0
- package/dist/number/num.d.mts.map +1 -0
- package/dist/number/num.mjs +513 -0
- package/dist/number/num.mjs.map +1 -0
- package/dist/number/refined-number-utils.d.mts +191 -0
- package/dist/number/refined-number-utils.d.mts.map +1 -0
- package/dist/number/refined-number-utils.mjs +179 -0
- package/dist/number/refined-number-utils.mjs.map +1 -0
- package/dist/object/index.d.mts +2 -0
- package/dist/object/index.d.mts.map +1 -0
- package/dist/object/index.mjs +2 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/object/object.d.mts +296 -0
- package/dist/object/object.d.mts.map +1 -0
- package/dist/object/object.mjs +295 -0
- package/dist/object/object.mjs.map +1 -0
- package/dist/others/cast-mutable.d.mts +110 -0
- package/dist/others/cast-mutable.d.mts.map +1 -0
- package/dist/others/cast-mutable.mjs +114 -0
- package/dist/others/cast-mutable.mjs.map +1 -0
- package/dist/others/cast-readonly.d.mts +189 -0
- package/dist/others/cast-readonly.d.mts.map +1 -0
- package/dist/others/cast-readonly.mjs +193 -0
- package/dist/others/cast-readonly.mjs.map +1 -0
- package/dist/others/if-then.d.mts +98 -0
- package/dist/others/if-then.d.mts.map +1 -0
- package/dist/others/if-then.mjs +100 -0
- package/dist/others/if-then.mjs.map +1 -0
- package/dist/others/index.d.mts +8 -0
- package/dist/others/index.d.mts.map +1 -0
- package/dist/others/index.mjs +8 -0
- package/dist/others/index.mjs.map +1 -0
- package/dist/others/map-nullable.d.mts +151 -0
- package/dist/others/map-nullable.d.mts.map +1 -0
- package/dist/others/map-nullable.mjs +159 -0
- package/dist/others/map-nullable.mjs.map +1 -0
- package/dist/others/memoize-function.d.mts +173 -0
- package/dist/others/memoize-function.d.mts.map +1 -0
- package/dist/others/memoize-function.mjs +189 -0
- package/dist/others/memoize-function.mjs.map +1 -0
- package/dist/others/tuple.d.mts +159 -0
- package/dist/others/tuple.d.mts.map +1 -0
- package/dist/others/tuple.mjs +161 -0
- package/dist/others/tuple.mjs.map +1 -0
- package/dist/others/unknown-to-string.d.mts +180 -0
- package/dist/others/unknown-to-string.d.mts.map +1 -0
- package/dist/others/unknown-to-string.mjs +211 -0
- package/dist/others/unknown-to-string.mjs.map +1 -0
- package/dist/tsconfig.json +1 -0
- package/package.json +18 -16
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { Arr } from '../array/array-utils.mjs';
|
|
2
|
+
import '../array/tuple-utils.mjs';
|
|
3
|
+
import { Optional } from '../functional/optional.mjs';
|
|
4
|
+
import '../functional/result.mjs';
|
|
5
|
+
import '../number/branded-types/finite-number.mjs';
|
|
6
|
+
import '../number/branded-types/int.mjs';
|
|
7
|
+
import '../number/branded-types/int16.mjs';
|
|
8
|
+
import '../number/branded-types/int32.mjs';
|
|
9
|
+
import '../number/branded-types/non-negative-finite-number.mjs';
|
|
10
|
+
import '../number/branded-types/non-negative-int16.mjs';
|
|
11
|
+
import '../number/branded-types/non-negative-int32.mjs';
|
|
12
|
+
import '../number/branded-types/non-zero-finite-number.mjs';
|
|
13
|
+
import '../number/branded-types/non-zero-int.mjs';
|
|
14
|
+
import '../number/branded-types/non-zero-int16.mjs';
|
|
15
|
+
import '../number/branded-types/non-zero-int32.mjs';
|
|
16
|
+
import '../number/branded-types/non-zero-safe-int.mjs';
|
|
17
|
+
import '../number/branded-types/non-zero-uint16.mjs';
|
|
18
|
+
import '../number/branded-types/non-zero-uint32.mjs';
|
|
19
|
+
import '../number/branded-types/positive-finite-number.mjs';
|
|
20
|
+
import '../number/branded-types/positive-int.mjs';
|
|
21
|
+
import '../number/branded-types/positive-int16.mjs';
|
|
22
|
+
import '../number/branded-types/positive-int32.mjs';
|
|
23
|
+
import '../number/branded-types/positive-safe-int.mjs';
|
|
24
|
+
import '../number/branded-types/positive-uint16.mjs';
|
|
25
|
+
import '../number/branded-types/positive-uint32.mjs';
|
|
26
|
+
import '../number/branded-types/safe-int.mjs';
|
|
27
|
+
import '../number/branded-types/safe-uint.mjs';
|
|
28
|
+
import '../number/branded-types/uint.mjs';
|
|
29
|
+
import '../number/branded-types/uint16.mjs';
|
|
30
|
+
import { asUint32 } from '../number/branded-types/uint32.mjs';
|
|
31
|
+
import '../number/enum/int8.mjs';
|
|
32
|
+
import '../number/enum/uint8.mjs';
|
|
33
|
+
import '../number/num.mjs';
|
|
34
|
+
import '../number/refined-number-utils.mjs';
|
|
35
|
+
import { castMutable } from '../others/cast-mutable.mjs';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Class implementation for a stack with LIFO (Last-In, First-Out) behavior using a dynamic array.
|
|
39
|
+
* This implementation provides O(1) amortized push and O(1) pop operations by using a resizable buffer
|
|
40
|
+
* that grows as needed.
|
|
41
|
+
*
|
|
42
|
+
* The underlying array automatically resizes when it becomes full, ensuring that the stack
|
|
43
|
+
* can grow to accommodate any number of elements while maintaining efficient operations.
|
|
44
|
+
*
|
|
45
|
+
* @template T The type of elements in the stack.
|
|
46
|
+
* @implements Stack
|
|
47
|
+
*/
|
|
48
|
+
class StackClass {
|
|
49
|
+
/** @internal Dynamic array to store stack elements. */
|
|
50
|
+
#buffer;
|
|
51
|
+
/** @internal Current number of elements in the stack. */
|
|
52
|
+
#mut_size;
|
|
53
|
+
/** @internal Current capacity of the buffer. */
|
|
54
|
+
#capacity;
|
|
55
|
+
/** @internal Initial capacity for new stacks. */
|
|
56
|
+
static #INITIAL_CAPACITY = 8;
|
|
57
|
+
/**
|
|
58
|
+
* Constructs a new StackClass instance.
|
|
59
|
+
* @param initialValues Optional initial values to populate the stack.
|
|
60
|
+
*/
|
|
61
|
+
constructor(initialValues = []) {
|
|
62
|
+
const initialCapacity = asUint32(Math.max(StackClass.#INITIAL_CAPACITY, initialValues.length * 2));
|
|
63
|
+
this.#buffer = castMutable(Arr.create(initialCapacity, undefined));
|
|
64
|
+
this.#mut_size = 0;
|
|
65
|
+
this.#capacity = initialCapacity;
|
|
66
|
+
// Add initial values
|
|
67
|
+
for (const value of initialValues) {
|
|
68
|
+
this.push(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** @inheritdoc */
|
|
72
|
+
get isEmpty() {
|
|
73
|
+
return this.#mut_size === 0;
|
|
74
|
+
}
|
|
75
|
+
/** @inheritdoc */
|
|
76
|
+
get size() {
|
|
77
|
+
return asUint32(this.#mut_size);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Removes and returns the element at the top of the stack (LIFO).
|
|
81
|
+
*
|
|
82
|
+
* This operation removes the element that was added most recently (last-in) and returns it.
|
|
83
|
+
* If the stack is empty, returns `Optional.none`. The operation is guaranteed to be O(1)
|
|
84
|
+
* and does not require any array resizing or memory reallocation.
|
|
85
|
+
*
|
|
86
|
+
* **Time Complexity:** O(1) - constant time operation
|
|
87
|
+
* **Space Complexity:** O(1) - no additional memory allocation
|
|
88
|
+
*
|
|
89
|
+
* @returns An Optional containing the removed element, or `Optional.none` if the stack is empty.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const stack = createStack<string>();
|
|
94
|
+
*
|
|
95
|
+
* // Add some elements
|
|
96
|
+
* stack.push("bottom");
|
|
97
|
+
* stack.push("middle");
|
|
98
|
+
* stack.push("top");
|
|
99
|
+
*
|
|
100
|
+
* // Remove elements in LIFO order
|
|
101
|
+
* const top = stack.pop();
|
|
102
|
+
* if (top.isSome) {
|
|
103
|
+
* console.log(top.value); // "top" (last pushed, first popped)
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* const middle = stack.pop().unwrap(); // "middle"
|
|
107
|
+
* console.log(stack.size); // 1
|
|
108
|
+
*
|
|
109
|
+
* // Safe handling of empty stack
|
|
110
|
+
* const emptyStack = createStack<number>();
|
|
111
|
+
* const result = emptyStack.pop();
|
|
112
|
+
* if (result.isNone) {
|
|
113
|
+
* console.log("Stack is empty");
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* // Typical usage in algorithms
|
|
117
|
+
* const pathStack = createStack<string>();
|
|
118
|
+
* pathStack.push("/home");
|
|
119
|
+
* pathStack.push("/users");
|
|
120
|
+
* pathStack.push("/documents");
|
|
121
|
+
*
|
|
122
|
+
* // Backtrack one level
|
|
123
|
+
* const currentDir = pathStack.pop().unwrap(); // "/documents"
|
|
124
|
+
* const parentDir = pathStack.pop().unwrap(); // "/users"
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
pop() {
|
|
128
|
+
if (this.isEmpty) {
|
|
129
|
+
return Optional.none;
|
|
130
|
+
}
|
|
131
|
+
this.#mut_size -= 1;
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
133
|
+
const element = this.#buffer[this.#mut_size];
|
|
134
|
+
this.#buffer[this.#mut_size] = undefined; // Clear reference for garbage collection
|
|
135
|
+
return Optional.some(element);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Adds an element to the top of the stack (LIFO).
|
|
139
|
+
*
|
|
140
|
+
* This operation adds the element to the top of the stack, where it will be the first
|
|
141
|
+
* to be popped (last-in, first-out ordering). The operation is amortized O(1),
|
|
142
|
+
* meaning it's O(1) for most operations with occasional O(n) when the buffer needs resizing.
|
|
143
|
+
*
|
|
144
|
+
* **Time Complexity:** O(1) amortized - O(n) only when buffer resize is needed
|
|
145
|
+
* **Space Complexity:** O(1) - constant additional memory per element
|
|
146
|
+
*
|
|
147
|
+
* **Buffer Resizing:** When the internal buffer becomes full, it automatically doubles
|
|
148
|
+
* in size and copies existing elements to maintain the stack structure.
|
|
149
|
+
*
|
|
150
|
+
* @param value The element to add to the top of the stack.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const actionStack = createStack<string>();
|
|
155
|
+
*
|
|
156
|
+
* // Add actions in chronological order
|
|
157
|
+
* actionStack.push("open file"); // O(1)
|
|
158
|
+
* actionStack.push("edit content"); // O(1)
|
|
159
|
+
* actionStack.push("save file"); // O(1)
|
|
160
|
+
*
|
|
161
|
+
* console.log(actionStack.size); // 3
|
|
162
|
+
*
|
|
163
|
+
* // Actions will be undone in reverse order (LIFO)
|
|
164
|
+
* while (!actionStack.isEmpty) {
|
|
165
|
+
* const action = actionStack.pop().unwrap();
|
|
166
|
+
* console.log(`Undoing: ${action}`);
|
|
167
|
+
* }
|
|
168
|
+
* // Output:
|
|
169
|
+
* // Undoing: save file
|
|
170
|
+
* // Undoing: edit content
|
|
171
|
+
* // Undoing: open file
|
|
172
|
+
*
|
|
173
|
+
* // High-volume pushing (demonstrates amortized O(1) performance)
|
|
174
|
+
* const dataStack = createStack<number>();
|
|
175
|
+
*
|
|
176
|
+
* for (let i = 0; i < 1000000; i++) {
|
|
177
|
+
* dataStack.push(i); // Each operation is O(1) amortized
|
|
178
|
+
* }
|
|
179
|
+
*
|
|
180
|
+
* console.log(dataStack.size); // 1000000
|
|
181
|
+
*
|
|
182
|
+
* // Function call stack simulation
|
|
183
|
+
* type StackFrame = { function: string; variables: Record<string, any> };
|
|
184
|
+
* const callStack = createStack<StackFrame>();
|
|
185
|
+
*
|
|
186
|
+
* callStack.push({ function: "main", variables: { argc: 1, argv: ["program"] } });
|
|
187
|
+
* callStack.push({ function: "process", variables: { data: [1, 2, 3] } });
|
|
188
|
+
* callStack.push({ function: "validate", variables: { input: "test" } });
|
|
189
|
+
*
|
|
190
|
+
* // Current function context is at the top
|
|
191
|
+
* const currentFrame = callStack.pop().unwrap();
|
|
192
|
+
* console.log(`Current function: ${currentFrame.function}`);
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
push(value) {
|
|
196
|
+
// Resize if buffer is full
|
|
197
|
+
if (this.#mut_size === this.#capacity) {
|
|
198
|
+
this.#resize();
|
|
199
|
+
}
|
|
200
|
+
this.#buffer[this.#mut_size] = value;
|
|
201
|
+
this.#mut_size += 1;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* @internal
|
|
205
|
+
* Resizes the buffer when it becomes full.
|
|
206
|
+
* Doubles the capacity while preserving all elements.
|
|
207
|
+
*/
|
|
208
|
+
#resize() {
|
|
209
|
+
const newCapacity = asUint32(this.#capacity * 2);
|
|
210
|
+
const newBuffer = castMutable(Arr.create(newCapacity, undefined));
|
|
211
|
+
// Copy existing elements
|
|
212
|
+
for (let i = 0; i < this.#mut_size; i++) {
|
|
213
|
+
newBuffer[i] = this.#buffer[i];
|
|
214
|
+
}
|
|
215
|
+
this.#buffer = newBuffer;
|
|
216
|
+
this.#capacity = newCapacity;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a high-performance dynamic array.
|
|
221
|
+
*
|
|
222
|
+
* This factory function creates an optimized stack implementation that maintains excellent performance
|
|
223
|
+
* characteristics for both push and pop operations. The underlying dynamic array automatically
|
|
224
|
+
* resizes to accommodate growing workloads while providing predictable O(1) operations.
|
|
225
|
+
*
|
|
226
|
+
* **Implementation Features:**
|
|
227
|
+
* - **O(1) push operations** (amortized - occasionally O(n) when resizing)
|
|
228
|
+
* - **O(1) pop operations** (always)
|
|
229
|
+
* - **Automatic buffer resizing** - starts at 8 elements, doubles when full
|
|
230
|
+
* - **Memory efficient** - garbage collects removed elements immediately
|
|
231
|
+
* - **Dynamic array design** - eliminates need for complex memory management
|
|
232
|
+
*
|
|
233
|
+
* **Performance Benefits:**
|
|
234
|
+
* - No array shifting required for stack operations
|
|
235
|
+
* - Minimal memory allocation overhead
|
|
236
|
+
* - Predictable performance under high load
|
|
237
|
+
* - Efficient memory usage with automatic cleanup
|
|
238
|
+
*
|
|
239
|
+
* @template T The type of elements stored in the stack.
|
|
240
|
+
* @param initialValues Optional array of initial elements to populate the stack.
|
|
241
|
+
* Elements will be popped in reverse order of how they appear in the array
|
|
242
|
+
* (last array element will be popped first).
|
|
243
|
+
* If provided, the initial buffer capacity will be at least twice the array length.
|
|
244
|
+
* @returns A new Stack instance optimized for high-performance LIFO operations.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* import { createStack } from './stack';
|
|
249
|
+
*
|
|
250
|
+
* // Example 1: Function call simulation
|
|
251
|
+
* type FunctionCall = { name: string; args: any[]; context: any };
|
|
252
|
+
* const callStack = createStack<FunctionCall>();
|
|
253
|
+
*
|
|
254
|
+
* // Simulate function calls (push onto stack)
|
|
255
|
+
* callStack.push({ name: "main", args: [], context: {} }); // O(1)
|
|
256
|
+
* callStack.push({ name: "processData", args: [data], context: {} }); // O(1)
|
|
257
|
+
* callStack.push({ name: "validateInput", args: [input], context: {} }); // O(1)
|
|
258
|
+
*
|
|
259
|
+
* // Simulate function returns (pop from stack)
|
|
260
|
+
* while (!callStack.isEmpty) {
|
|
261
|
+
* const call = callStack.pop().unwrap(); // O(1)
|
|
262
|
+
* console.log(`Returning from: ${call.name}`);
|
|
263
|
+
* }
|
|
264
|
+
* // Output:
|
|
265
|
+
* // Returning from: validateInput
|
|
266
|
+
* // Returning from: processData
|
|
267
|
+
* // Returning from: main
|
|
268
|
+
*
|
|
269
|
+
* // Example 2: Expression evaluation with operator precedence
|
|
270
|
+
* const operatorStack = createStack<string>();
|
|
271
|
+
* const operandStack = createStack<number>();
|
|
272
|
+
*
|
|
273
|
+
* // Simulate parsing "3 + 4 * 2"
|
|
274
|
+
* operandStack.push(3);
|
|
275
|
+
* operatorStack.push("+");
|
|
276
|
+
* operandStack.push(4);
|
|
277
|
+
* operatorStack.push("*"); // Higher precedence
|
|
278
|
+
* operandStack.push(2);
|
|
279
|
+
*
|
|
280
|
+
* // Process higher precedence first (LIFO)
|
|
281
|
+
* const op = operatorStack.pop().unwrap(); // "*"
|
|
282
|
+
* const b = operandStack.pop().unwrap(); // 2
|
|
283
|
+
* const a = operandStack.pop().unwrap(); // 4
|
|
284
|
+
* operandStack.push(a * b); // Push result: 8
|
|
285
|
+
*
|
|
286
|
+
* // Example 3: Undo/Redo functionality
|
|
287
|
+
* type EditAction = {
|
|
288
|
+
* type: 'insert' | 'delete' | 'modify';
|
|
289
|
+
* position: number;
|
|
290
|
+
* oldValue: string;
|
|
291
|
+
* newValue: string;
|
|
292
|
+
* };
|
|
293
|
+
*
|
|
294
|
+
* const undoStack = createStack<EditAction>();
|
|
295
|
+
* const redoStack = createStack<EditAction>();
|
|
296
|
+
*
|
|
297
|
+
* // Perform edits (push to undo stack)
|
|
298
|
+
* const edit1: EditAction = { type: 'insert', position: 0, oldValue: '', newValue: 'Hello' };
|
|
299
|
+
* const edit2: EditAction = { type: 'insert', position: 5, oldValue: '', newValue: ' World' };
|
|
300
|
+
*
|
|
301
|
+
* undoStack.push(edit1);
|
|
302
|
+
* undoStack.push(edit2);
|
|
303
|
+
*
|
|
304
|
+
* // Undo last edit
|
|
305
|
+
* if (!undoStack.isEmpty) {
|
|
306
|
+
* const lastEdit = undoStack.pop().unwrap();
|
|
307
|
+
* redoStack.push(lastEdit);
|
|
308
|
+
* console.log(`Undid: ${lastEdit.type} at position ${lastEdit.position}`);
|
|
309
|
+
* }
|
|
310
|
+
*
|
|
311
|
+
* // Example 4: High-throughput data processing
|
|
312
|
+
* const processingStack = createStack<number>();
|
|
313
|
+
*
|
|
314
|
+
* // Add large amount of data (demonstrates amortized O(1) performance)
|
|
315
|
+
* for (let i = 0; i < 100000; i++) {
|
|
316
|
+
* processingStack.push(i); // Each push is O(1) amortized
|
|
317
|
+
* }
|
|
318
|
+
*
|
|
319
|
+
* // Process data in LIFO order
|
|
320
|
+
* let processedCount = 0;
|
|
321
|
+
* while (!processingStack.isEmpty) {
|
|
322
|
+
* const value = processingStack.pop().unwrap(); // O(1)
|
|
323
|
+
* // Process value...
|
|
324
|
+
* processedCount++;
|
|
325
|
+
* }
|
|
326
|
+
* console.log(`Processed ${processedCount} items`); // 100000
|
|
327
|
+
*
|
|
328
|
+
* // Example 5: Stack with pre-populated data
|
|
329
|
+
* const historyStack = createStack<string>([
|
|
330
|
+
* "page1.html",
|
|
331
|
+
* "page2.html",
|
|
332
|
+
* "page3.html",
|
|
333
|
+
* "page4.html"
|
|
334
|
+
* ]);
|
|
335
|
+
*
|
|
336
|
+
* console.log(historyStack.size); // Output: 4
|
|
337
|
+
*
|
|
338
|
+
* // Navigate back through history (LIFO order)
|
|
339
|
+
* while (!historyStack.isEmpty) {
|
|
340
|
+
* const page = historyStack.pop().unwrap();
|
|
341
|
+
* console.log(`Going back to: ${page}`);
|
|
342
|
+
* }
|
|
343
|
+
* // Output:
|
|
344
|
+
* // Going back to: page4.html (last added, first removed)
|
|
345
|
+
* // Going back to: page3.html
|
|
346
|
+
* // Going back to: page2.html
|
|
347
|
+
* // Going back to: page1.html
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
const createStack = (initialValues) => new StackClass(initialValues);
|
|
351
|
+
|
|
352
|
+
export { createStack };
|
|
353
|
+
//# sourceMappingURL=stack.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.mjs","sources":["../../src/collections/stack.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqFA;;;;;;;;;;AAUG;AACH,MAAM,UAAU,CAAA;;AAEd,IAAA,OAAO;;AAGP,IAAA,SAAS;;AAGT,IAAA,SAAS;;AAGT,IAAA,OAAgB,iBAAiB,GAAG,CAAC;AAErC;;;AAGG;AACH,IAAA,WAAA,CAAY,gBAA8B,EAAE,EAAA;AAC1C,QAAA,MAAM,eAAe,GAAG,QAAQ,CAC9B,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,iBAAiB,EAAE,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CACjE;AAED,QAAA,IAAI,CAAC,OAAO,GAAG,WAAW,CACxB,GAAG,CAAC,MAAM,CAAgB,eAAe,EAAE,SAAS,CAAC,CACtD;AACD,QAAA,IAAI,CAAC,SAAS,GAAG,CAAC;AAClB,QAAA,IAAI,CAAC,SAAS,GAAG,eAAe;;AAGhC,QAAA,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE;AACjC,YAAA,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;;;;AAKpB,IAAA,IAAI,OAAO,GAAA;AACT,QAAA,OAAO,IAAI,CAAC,SAAS,KAAK,CAAC;;;AAI7B,IAAA,IAAI,IAAI,GAAA;AACN,QAAA,OAAO,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC;;AAGjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CG;IACH,GAAG,GAAA;AACD,QAAA,IAAI,IAAI,CAAC,OAAO,EAAE;YAChB,OAAO,QAAQ,CAAC,IAAI;;AAGtB,QAAA,IAAI,CAAC,SAAS,IAAI,CAAC;;QAEnB,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAE;QAC7C,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;AAEzC,QAAA,OAAO,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;;AAG/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDG;AACH,IAAA,IAAI,CAAC,KAAQ,EAAA;;QAEX,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,CAAC,SAAS,EAAE;YACrC,IAAI,CAAC,OAAO,EAAE;;QAGhB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,KAAK;AACpC,QAAA,IAAI,CAAC,SAAS,IAAI,CAAC;;AAGrB;;;;AAIG;IACH,OAAO,GAAA;QACL,MAAM,WAAW,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;AAChD,QAAA,MAAM,SAAS,GAAG,WAAW,CAC3B,GAAG,CAAC,MAAM,CAAgB,WAAW,EAAE,SAAS,CAAC,CAClD;;AAGD,QAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE;YACvC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;;AAGhC,QAAA,IAAI,CAAC,OAAO,GAAG,SAAS;AACxB,QAAA,IAAI,CAAC,SAAS,GAAG,WAAW;;;AAIhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkIG;AACI,MAAM,WAAW,GAAG,CAAK,aAA4B,KAC1D,IAAI,UAAU,CAAI,aAAa;;;;"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compile-time type assertion utility for TypeScript type testing.
|
|
3
|
+
*
|
|
4
|
+
* This function performs static type relationship checking at compile-time and has no runtime effect.
|
|
5
|
+
* It is primarily used in test files to verify that TypeScript's type inference and type relationships
|
|
6
|
+
* work as expected. The function will cause TypeScript compilation errors if the specified type
|
|
7
|
+
* relationship does not hold.
|
|
8
|
+
*
|
|
9
|
+
* ## Supported Type Relations
|
|
10
|
+
*
|
|
11
|
+
* ### Equality Relations
|
|
12
|
+
* - **`"="` (strict equality)**: Asserts that types `A` and `B` are exactly the same type.
|
|
13
|
+
* Uses TypeScript's internal type equality checking.
|
|
14
|
+
* - **`"!="` (strict inequality)**: Asserts that types `A` and `B` are not exactly the same type.
|
|
15
|
+
*
|
|
16
|
+
* ### Assignability Relations
|
|
17
|
+
* - **`"~="` (mutual assignability)**: Asserts that `A` extends `B` AND `B` extends `A`.
|
|
18
|
+
* Types are structurally equivalent and mutually assignable.
|
|
19
|
+
* - **`"<="` (subtype relation)**: Asserts that type `A` extends (is assignable to) type `B`.
|
|
20
|
+
* Type `A` is a subtype of `B`.
|
|
21
|
+
* - **`">="` (supertype relation)**: Asserts that type `B` extends (is assignable to) type `A`.
|
|
22
|
+
* Type `A` is a supertype of `B`.
|
|
23
|
+
*
|
|
24
|
+
* ### Negative Assignability Relations
|
|
25
|
+
* - **`"!<="` (not subtype)**: Asserts that type `A` does NOT extend type `B`.
|
|
26
|
+
* - **`"!>="` (not supertype)**: Asserts that type `B` does NOT extend type `A`.
|
|
27
|
+
*
|
|
28
|
+
* ## Type Parameter Constraints
|
|
29
|
+
*
|
|
30
|
+
* @template A - The first type for comparison. Can be any TypeScript type including:
|
|
31
|
+
* - Primitive types (string, number, boolean, etc.)
|
|
32
|
+
* - Object types and interfaces
|
|
33
|
+
* - Union and intersection types
|
|
34
|
+
* - Generic types and type parameters
|
|
35
|
+
* - Literal types and branded types
|
|
36
|
+
* - Function types and return types
|
|
37
|
+
* @template B - The second type for comparison. Same constraints as type `A`.
|
|
38
|
+
*
|
|
39
|
+
* @param _relation - A string literal representing the expected type relationship.
|
|
40
|
+
* TypeScript's type system automatically infers and restricts the available operators
|
|
41
|
+
* based on the actual relationship between types `A` and `B`. If an invalid relationship
|
|
42
|
+
* is specified, TypeScript will show a compilation error.
|
|
43
|
+
*
|
|
44
|
+
* ## Usage Patterns
|
|
45
|
+
*
|
|
46
|
+
* ### Basic Type Testing
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { expectType } from './expect-type.mjs';
|
|
50
|
+
*
|
|
51
|
+
* // Primitive type equality
|
|
52
|
+
* expectType<string, string>("="); // ✓ exact match
|
|
53
|
+
* expectType<number, string>("!="); // ✓ different types
|
|
54
|
+
* expectType<42, number>("<="); // ✓ literal extends primitive
|
|
55
|
+
* expectType<number, 42>(">="); // ✓ primitive is supertype
|
|
56
|
+
*
|
|
57
|
+
* // Type assertions will cause compilation errors for wrong relationships:
|
|
58
|
+
* // expectType<string, number>("="); // ❌ TypeScript error
|
|
59
|
+
* // expectType<number, string>("<="); // ❌ TypeScript error
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* ### Array and Tuple Type Validation
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* // Testing array utility function return types
|
|
66
|
+
* const zeros = Arr.zeros(3);
|
|
67
|
+
* expectType<typeof zeros, readonly [0, 0, 0]>("=");
|
|
68
|
+
*
|
|
69
|
+
* const sequence = Arr.seq(5);
|
|
70
|
+
* expectType<typeof sequence, readonly [0, 1, 2, 3, 4]>("=");
|
|
71
|
+
*
|
|
72
|
+
* // Dynamic length arrays
|
|
73
|
+
* const dynamicArray = Arr.zeros(someLength);
|
|
74
|
+
* expectType<typeof dynamicArray, readonly 0[]>("=");
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* ### Function Return Type Testing
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* // Testing function return types
|
|
81
|
+
* const createUser = () => ({ id: 1, name: 'John' });
|
|
82
|
+
* expectType<ReturnType<typeof createUser>, { id: number; name: string }>("~=");
|
|
83
|
+
*
|
|
84
|
+
* // Generic function type inference
|
|
85
|
+
* const identity = <T>(x: T): T => x;
|
|
86
|
+
* const result = identity('hello');
|
|
87
|
+
* expectType<typeof result, string>("=");
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* ### Union and Intersection Types
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* // Union type relationships
|
|
94
|
+
* expectType<string, string | number>("<="); // string extends union
|
|
95
|
+
* expectType<string | number, string>(">="); // union contains string
|
|
96
|
+
* expectType<string | number, number>(">="); // union contains number
|
|
97
|
+
*
|
|
98
|
+
* // Intersection type relationships
|
|
99
|
+
* type A = { a: number };
|
|
100
|
+
* type B = { b: string };
|
|
101
|
+
* expectType<A & B, A>(">="); // intersection extends component
|
|
102
|
+
* expectType<A, A & B>("<="); // component extends intersection
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* ### Branded Type Validation
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // Testing branded number types
|
|
109
|
+
* expectType<PositiveInt, number>("<="); // branded type extends base
|
|
110
|
+
* expectType<number, PositiveInt>(">="); // base type is supertype
|
|
111
|
+
* expectType<PositiveInt, NegativeInt>("!="); // different branded types
|
|
112
|
+
*
|
|
113
|
+
* // Type guard function validation
|
|
114
|
+
* if (isPositiveInt(value)) {
|
|
115
|
+
* expectType<typeof value, PositiveInt>("<=");
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* ### Optional and Result Type Testing
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* // Optional type narrowing
|
|
123
|
+
* const optional: Optional<number> = Optional.some(42);
|
|
124
|
+
* if (Optional.isSome(optional)) {
|
|
125
|
+
* expectType<typeof optional, Optional.Some<number>>("<=");
|
|
126
|
+
* }
|
|
127
|
+
* if (Optional.isNone(optional)) {
|
|
128
|
+
* expectType<typeof optional, Optional.None>("<=");
|
|
129
|
+
* }
|
|
130
|
+
*
|
|
131
|
+
* // Result type validation
|
|
132
|
+
* const result: Result<string, Error> = Result.ok('success');
|
|
133
|
+
* expectType<typeof result, Result<string, Error>>("<=");
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* ### Type Guard and Validation Testing
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* // Testing type guard functions
|
|
140
|
+
* if (isRecord(value)) {
|
|
141
|
+
* expectType<typeof value, UnknownRecord>("<=");
|
|
142
|
+
* }
|
|
143
|
+
*
|
|
144
|
+
* // Testing compile-time type predicates
|
|
145
|
+
* const obj = { key: 'value' };
|
|
146
|
+
* if (hasKey(obj, 'key')) {
|
|
147
|
+
* expectType<typeof obj.key, unknown>("<=");
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* ## Common Testing Patterns
|
|
152
|
+
*
|
|
153
|
+
* ### Dual Testing Strategy
|
|
154
|
+
* Combine `expectType` with runtime assertions for comprehensive testing:
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* describe('Arr.zeros', () => {
|
|
159
|
+
* test('should create array of zeros with correct type', () => {
|
|
160
|
+
* const result = Arr.zeros(3);
|
|
161
|
+
*
|
|
162
|
+
* // Compile-time type assertion
|
|
163
|
+
* expectType<typeof result, readonly [0, 0, 0]>("=");
|
|
164
|
+
*
|
|
165
|
+
* // Runtime behavior assertion
|
|
166
|
+
* expect(result).toStrictEqual([0, 0, 0]);
|
|
167
|
+
* });
|
|
168
|
+
* });
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* ### Type Relationship Validation
|
|
172
|
+
* Test complex type hierarchies and relationships:
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* // Ensure proper type hierarchy
|
|
177
|
+
* expectType<PositiveInt, Int>("<="); // positive is subset of int
|
|
178
|
+
* expectType<Int, FiniteNumber>("<="); // int is subset of finite
|
|
179
|
+
* expectType<FiniteNumber, number>("<="); // finite is subset of number
|
|
180
|
+
*
|
|
181
|
+
* // Verify mutual exclusion
|
|
182
|
+
* expectType<PositiveInt, NegativeInt>("!="); // different int types
|
|
183
|
+
* expectType<PositiveInt, NegativeInt>("!<="); // neither extends the other
|
|
184
|
+
* expectType<NegativeInt, PositiveInt>("!<=");
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* ## Important Notes
|
|
188
|
+
*
|
|
189
|
+
* - **Compile-time only**: This function has no runtime behavior and will be optimized away.
|
|
190
|
+
* - **Type inference**: The available relation operators are automatically inferred by TypeScript
|
|
191
|
+
* based on the actual type relationship between `A` and `B`.
|
|
192
|
+
* - **Error feedback**: Invalid type relationships will cause clear TypeScript compilation errors.
|
|
193
|
+
* - **Test organization**: Typically used in `.test.mts` files alongside runtime assertions.
|
|
194
|
+
* - **Performance**: Has zero runtime overhead as it's purely a compile-time construct.
|
|
195
|
+
*
|
|
196
|
+
* @since 1.0.0
|
|
197
|
+
*/
|
|
198
|
+
export declare const expectType: <A, B>(_relation: TypeEq<A, B> extends true ? "<=" | "=" | ">=" | "~=" : "!=" | (TypeExtends<A, B> extends true ? "<=" | (TypeExtends<B, A> extends true ? ">=" | "~=" : "!>=") : "!<=" | (TypeExtends<B, A> extends true ? ">=" : "!>="))) => void;
|
|
199
|
+
//# sourceMappingURL=expect-type.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expect-type.d.mts","sourceRoot":"","sources":["../src/expect-type.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoMG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,EAC7B,WAAW,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,IAAI,GAChC,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAEpB,IAAI,GACJ,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,IAAI,GAC3B,IAAI,GAAG,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,CAAC,GAC7D,KAAK,GAAG,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,GAAG,KAAK,CAAC,CAAC,KACnE,IAAiB,CAAC"}
|