ts-data-forge 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +16 -14
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that checks if a key exists as an own property in an object.
|
|
3
|
+
*
|
|
4
|
+
* This function is similar to `hasKey()` but with reversed parameter order and different
|
|
5
|
+
* type narrowing behavior. While `hasKey()` narrows the object type, `keyIsIn()` narrows
|
|
6
|
+
* the key type to be a valid key of the given object.
|
|
7
|
+
*
|
|
8
|
+
* **Type Narrowing Behavior:**
|
|
9
|
+
* - Narrows the key type to be a key that exists in the object (`K & keyof R`)
|
|
10
|
+
* - Useful when you have a dynamic key and want to ensure it's valid for a specific object
|
|
11
|
+
* - The object type remains unchanged
|
|
12
|
+
*
|
|
13
|
+
* **Implementation:** Uses `Object.hasOwn()` to check for own properties (not inherited).
|
|
14
|
+
*
|
|
15
|
+
* @template K - The type of the key to check, must extend PropertyKey (string | number | symbol)
|
|
16
|
+
* @template R - The type of the record (object), must extend UnknownRecord
|
|
17
|
+
* @param key - The key to check for
|
|
18
|
+
* @param obj - The object to check within
|
|
19
|
+
* @returns `true` if `key` is an own property of `obj`, `false` otherwise.
|
|
20
|
+
* When `true`, TypeScript narrows the key type to be a valid key of the object.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* Basic usage with known object structure:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
26
|
+
* const userInput: string = getUserInput(); // Could be any string
|
|
27
|
+
*
|
|
28
|
+
* if (keyIsIn(userInput, obj)) {
|
|
29
|
+
* // userInput is now narrowed to 'a' | 'b' | 'c'
|
|
30
|
+
* const value = obj[userInput]; // Type-safe access, value is number
|
|
31
|
+
* console.log(`Value for ${userInput}:`, value);
|
|
32
|
+
* } else {
|
|
33
|
+
* console.log(`Key '${userInput}' not found in object`);
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* Dynamic key validation for safe property access:
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const config = {
|
|
41
|
+
* apiUrl: 'https://api.example.com',
|
|
42
|
+
* timeout: 5000,
|
|
43
|
+
* retries: 3
|
|
44
|
+
* } as const;
|
|
45
|
+
*
|
|
46
|
+
* function getConfigValue(key: string): unknown {
|
|
47
|
+
* if (keyIsIn(key, config)) {
|
|
48
|
+
* // key is now narrowed to 'apiUrl' | 'timeout' | 'retries'
|
|
49
|
+
* return config[key]; // Safe access with proper typing
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* throw new Error(`Invalid config key: ${key}`);
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // Usage
|
|
56
|
+
* const apiUrl = getConfigValue('apiUrl'); // Returns string
|
|
57
|
+
* const timeout = getConfigValue('timeout'); // Returns number
|
|
58
|
+
* // getConfigValue('invalid') would throw an error
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* Form field validation:
|
|
63
|
+
* ```typescript
|
|
64
|
+
* interface FormData {
|
|
65
|
+
* name: string;
|
|
66
|
+
* email: string;
|
|
67
|
+
* age: number;
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* const formData: FormData = getFormData();
|
|
71
|
+
* const requiredFields: readonly string[] = ['name', 'email'] as const;
|
|
72
|
+
*
|
|
73
|
+
* function validateRequiredFields(data: FormData): string[] {
|
|
74
|
+
* const errors: string[] = [];
|
|
75
|
+
*
|
|
76
|
+
* for (const field of requiredFields) {
|
|
77
|
+
* if (keyIsIn(field, data)) {
|
|
78
|
+
* // field is now narrowed to keyof FormData
|
|
79
|
+
* const value = data[field];
|
|
80
|
+
*
|
|
81
|
+
* if (typeof value === 'string' && value.trim() === '') {
|
|
82
|
+
* errors.push(`${field} is required`);
|
|
83
|
+
* }
|
|
84
|
+
* }
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* return errors;
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* Safe object property iteration:
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const userPreferences = {
|
|
95
|
+
* theme: 'dark',
|
|
96
|
+
* language: 'en',
|
|
97
|
+
* notifications: true
|
|
98
|
+
* };
|
|
99
|
+
*
|
|
100
|
+
* const settingsToUpdate: string[] = getSettingsFromUser();
|
|
101
|
+
*
|
|
102
|
+
* function updatePreferences(updates: Record<string, unknown>) {
|
|
103
|
+
* const validUpdates: Partial<typeof userPreferences> = {};
|
|
104
|
+
*
|
|
105
|
+
* for (const [key, value] of Object.entries(updates)) {
|
|
106
|
+
* if (keyIsIn(key, userPreferences)) {
|
|
107
|
+
* // key is now narrowed to valid preference keys
|
|
108
|
+
* validUpdates[key] = value as typeof userPreferences[typeof key];
|
|
109
|
+
* } else {
|
|
110
|
+
* console.warn(`Unknown preference key: ${key}`);
|
|
111
|
+
* }
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* return { ...userPreferences, ...validUpdates };
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* Comparison with hasKey() - different narrowing behavior:
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const obj = { x: 10, y: 20 };
|
|
122
|
+
* const key: string = 'x';
|
|
123
|
+
*
|
|
124
|
+
* // Using keyIsIn - narrows the key type
|
|
125
|
+
* if (keyIsIn(key, obj)) {
|
|
126
|
+
* // key is now 'x' | 'y'
|
|
127
|
+
* const value = obj[key]; // Safe access
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* // Using hasKey - narrows the object type
|
|
131
|
+
* if (hasKey(obj, key)) {
|
|
132
|
+
* // obj type is narrowed to guarantee the key exists
|
|
133
|
+
* const value = obj.x; // Direct access
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* Working with union types:
|
|
139
|
+
* ```typescript
|
|
140
|
+
* type Config =
|
|
141
|
+
* | { type: 'database'; host: string; port: number }
|
|
142
|
+
* | { type: 'file'; path: string }
|
|
143
|
+
* | { type: 'memory'; maxSize: number };
|
|
144
|
+
*
|
|
145
|
+
* function getConfigProperty(config: Config, propName: string): unknown {
|
|
146
|
+
* if (keyIsIn(propName, config)) {
|
|
147
|
+
* // propName is narrowed to valid keys for the specific config type
|
|
148
|
+
* return config[propName];
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
151
|
+
* return undefined;
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @see {@link hasKey} - Similar function that narrows the object type instead of the key type
|
|
156
|
+
*/
|
|
157
|
+
const keyIsIn = (key, obj) => Object.hasOwn(obj, key);
|
|
158
|
+
|
|
159
|
+
export { keyIsIn };
|
|
160
|
+
//# sourceMappingURL=key-is-in.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-is-in.mjs","sources":["../../src/guard/key-is-in.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2JG;AACI,MAAM,OAAO,GAAG,CAIrB,GAAM,EACN,GAAM,KAC0B,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG;;;;"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './array/index.mjs';
|
|
2
|
+
export * from './collections/index.mjs';
|
|
3
|
+
export * from './expect-type.mjs';
|
|
4
|
+
export * from './functional/index.mjs';
|
|
5
|
+
export * from './guard/index.mjs';
|
|
6
|
+
export * from './iterator/index.mjs';
|
|
7
|
+
export * from './json/index.mjs';
|
|
8
|
+
export * from './number/index.mjs';
|
|
9
|
+
export * from './object/index.mjs';
|
|
10
|
+
export * from './others/index.mjs';
|
|
11
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export { Arr } from './array/array-utils.mjs';
|
|
2
|
+
export { Tpl } from './array/tuple-utils.mjs';
|
|
3
|
+
export { IMapMapped } from './collections/imap-mapped.mjs';
|
|
4
|
+
export { IMap } from './collections/imap.mjs';
|
|
5
|
+
export { ISetMapped } from './collections/iset-mapped.mjs';
|
|
6
|
+
export { ISet } from './collections/iset.mjs';
|
|
7
|
+
export { createQueue } from './collections/queue.mjs';
|
|
8
|
+
export { createStack } from './collections/stack.mjs';
|
|
9
|
+
export { expectType } from './expect-type.mjs';
|
|
10
|
+
export { match } from './functional/match.mjs';
|
|
11
|
+
export { Optional } from './functional/optional.mjs';
|
|
12
|
+
export { pipe } from './functional/pipe.mjs';
|
|
13
|
+
export { Result } from './functional/result.mjs';
|
|
14
|
+
export { hasKey } from './guard/has-key.mjs';
|
|
15
|
+
export { isNonEmptyString } from './guard/is-non-empty-string.mjs';
|
|
16
|
+
export { isNonNullObject } from './guard/is-non-null-object.mjs';
|
|
17
|
+
export { isPrimitive } from './guard/is-primitive.mjs';
|
|
18
|
+
export { isRecord } from './guard/is-record.mjs';
|
|
19
|
+
export { isBigint, isBoolean, isNonNullish, isNotBigint, isNotBoolean, isNotNull, isNotNumber, isNotString, isNotSymbol, isNotUndefined, isNull, isNullish, isNumber, isString, isSymbol, isUndefined } from './guard/is-type.mjs';
|
|
20
|
+
export { keyIsIn } from './guard/key-is-in.mjs';
|
|
21
|
+
export { range } from './iterator/range.mjs';
|
|
22
|
+
export { Json } from './json/json.mjs';
|
|
23
|
+
export { FiniteNumber, asFiniteNumber, isFiniteNumber } from './number/branded-types/finite-number.mjs';
|
|
24
|
+
export { Int, asInt, isInt } from './number/branded-types/int.mjs';
|
|
25
|
+
export { Int16, asInt16, isInt16 } from './number/branded-types/int16.mjs';
|
|
26
|
+
export { Int32, asInt32, isInt32 } from './number/branded-types/int32.mjs';
|
|
27
|
+
export { NonNegativeFiniteNumber, asNonNegativeFiniteNumber, isNonNegativeFiniteNumber } from './number/branded-types/non-negative-finite-number.mjs';
|
|
28
|
+
export { NonNegativeInt16, asNonNegativeInt16, isNonNegativeInt16 } from './number/branded-types/non-negative-int16.mjs';
|
|
29
|
+
export { NonNegativeInt32, asNonNegativeInt32, isNonNegativeInt32 } from './number/branded-types/non-negative-int32.mjs';
|
|
30
|
+
export { NonZeroFiniteNumber, asNonZeroFiniteNumber, isNonZeroFiniteNumber } from './number/branded-types/non-zero-finite-number.mjs';
|
|
31
|
+
export { NonZeroInt, asNonZeroInt, isNonZeroInt } from './number/branded-types/non-zero-int.mjs';
|
|
32
|
+
export { NonZeroInt16, asNonZeroInt16, isNonZeroInt16 } from './number/branded-types/non-zero-int16.mjs';
|
|
33
|
+
export { NonZeroInt32, asNonZeroInt32, isNonZeroInt32 } from './number/branded-types/non-zero-int32.mjs';
|
|
34
|
+
export { NonZeroSafeInt, asNonZeroSafeInt, isNonZeroSafeInt } from './number/branded-types/non-zero-safe-int.mjs';
|
|
35
|
+
export { NonZeroUint16, asNonZeroUint16, isNonZeroUint16 } from './number/branded-types/non-zero-uint16.mjs';
|
|
36
|
+
export { NonZeroUint32, asNonZeroUint32, isNonZeroUint32 } from './number/branded-types/non-zero-uint32.mjs';
|
|
37
|
+
export { PositiveFiniteNumber, asPositiveFiniteNumber, isPositiveFiniteNumber } from './number/branded-types/positive-finite-number.mjs';
|
|
38
|
+
export { PositiveInt, asPositiveInt, isPositiveInt } from './number/branded-types/positive-int.mjs';
|
|
39
|
+
export { PositiveInt16, asPositiveInt16, isPositiveInt16 } from './number/branded-types/positive-int16.mjs';
|
|
40
|
+
export { PositiveInt32, asPositiveInt32, isPositiveInt32 } from './number/branded-types/positive-int32.mjs';
|
|
41
|
+
export { PositiveSafeInt, asPositiveSafeInt, isPositiveSafeInt } from './number/branded-types/positive-safe-int.mjs';
|
|
42
|
+
export { PositiveUint16, asPositiveUint16, isPositiveUint16 } from './number/branded-types/positive-uint16.mjs';
|
|
43
|
+
export { PositiveUint32, asPositiveUint32, isPositiveUint32 } from './number/branded-types/positive-uint32.mjs';
|
|
44
|
+
export { SafeInt, asSafeInt, isSafeInt } from './number/branded-types/safe-int.mjs';
|
|
45
|
+
export { SafeUint, asSafeUint, isSafeUint } from './number/branded-types/safe-uint.mjs';
|
|
46
|
+
export { Uint, asUint, isUint } from './number/branded-types/uint.mjs';
|
|
47
|
+
export { Uint16, asUint16, isUint16 } from './number/branded-types/uint16.mjs';
|
|
48
|
+
export { Uint32, asUint32, isUint32 } from './number/branded-types/uint32.mjs';
|
|
49
|
+
export { Int8, asInt8, isInt8 } from './number/enum/int8.mjs';
|
|
50
|
+
export { Uint8, asUint8, isUint8 } from './number/enum/uint8.mjs';
|
|
51
|
+
export { Num } from './number/num.mjs';
|
|
52
|
+
export { TsVerifiedInternals } from './number/refined-number-utils.mjs';
|
|
53
|
+
export { Obj } from './object/object.mjs';
|
|
54
|
+
export { castDeepMutable, castMutable } from './others/cast-mutable.mjs';
|
|
55
|
+
export { castDeepReadonly, castReadonly } from './others/cast-readonly.mjs';
|
|
56
|
+
export { ifThen } from './others/if-then.mjs';
|
|
57
|
+
export { mapNullable } from './others/map-nullable.mjs';
|
|
58
|
+
export { memoizeFunction } from './others/memoize-function.mjs';
|
|
59
|
+
export { tp } from './others/tuple.mjs';
|
|
60
|
+
export { unknownToString } from './others/unknown-to-string.mjs';
|
|
61
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/iterator/index.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a sequence of numbers within a specified range using a generator function.
|
|
3
|
+
*
|
|
4
|
+
* This function creates a generator that yields numbers from `start` (inclusive) to `end` (exclusive)
|
|
5
|
+
* with the specified `step` increment/decrement. The function implements the JavaScript iterator protocol,
|
|
6
|
+
* making it compatible with for-of loops, spread operator, Array.from(), and other iterator consumers.
|
|
7
|
+
*
|
|
8
|
+
* The function has two overloaded signatures:
|
|
9
|
+
* 1. For non-negative ranges: accepts SafeUint parameters and optional positive step
|
|
10
|
+
* 2. For general ranges: accepts SafeInt parameters and optional non-zero step
|
|
11
|
+
*
|
|
12
|
+
* **Generator Behavior:**
|
|
13
|
+
* - Yields values lazily (computed on-demand)
|
|
14
|
+
* - Returns `void` when iteration completes
|
|
15
|
+
* - Does not accept sent values (next parameter is ignored)
|
|
16
|
+
* - Automatically handles direction based on step sign and start/end relationship
|
|
17
|
+
*
|
|
18
|
+
* **Step Parameter Behavior:**
|
|
19
|
+
* - Positive step: iterates from start toward end (start < end expected)
|
|
20
|
+
* - Negative step: iterates from start toward end (start > end expected)
|
|
21
|
+
* - Zero step: not allowed (NonZeroSafeInt constraint)
|
|
22
|
+
* - Default step: 1 (positive direction)
|
|
23
|
+
*
|
|
24
|
+
* **Edge Cases:**
|
|
25
|
+
* - When start equals end: yields no values (empty sequence)
|
|
26
|
+
* - When step direction conflicts with start/end relationship: yields no values
|
|
27
|
+
* - All parameters must be safe integers (within JavaScript's safe integer range)
|
|
28
|
+
*
|
|
29
|
+
* @template T - The specific SafeInt or SafeUint type being generated
|
|
30
|
+
* @param start - The starting number of the sequence (inclusive). Must be a safe integer.
|
|
31
|
+
* @param end - The end number of the sequence (exclusive). Must be a safe integer.
|
|
32
|
+
* @param step - The increment or decrement value. Defaults to 1. Must be non-zero safe integer.
|
|
33
|
+
* @returns A Generator object that yields safe integers in the specified range.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Basic ascending range
|
|
38
|
+
* for (const n of range(0, 5)) {
|
|
39
|
+
* console.log(n); // Outputs: 0, 1, 2, 3, 4
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // Range with custom step
|
|
43
|
+
* for (const n of range(0, 10, 2)) {
|
|
44
|
+
* console.log(n); // Outputs: 0, 2, 4, 6, 8
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* // Descending range with negative step
|
|
48
|
+
* for (const n of range(10, 0, -1)) {
|
|
49
|
+
* console.log(n); // Outputs: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* // Negative numbers with negative step
|
|
53
|
+
* for (const n of range(0, -10, -1)) {
|
|
54
|
+
* console.log(n); // Outputs: 0, -1, -2, -3, -4, -5, -6, -7, -8, -9
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* // Convert generator to array
|
|
58
|
+
* const numbers = Array.from(range(1, 4)); // [1, 2, 3]
|
|
59
|
+
* const evens = [...range(0, 11, 2)]; // [0, 2, 4, 6, 8, 10]
|
|
60
|
+
*
|
|
61
|
+
* // Empty ranges
|
|
62
|
+
* Array.from(range(5, 5)); // [] (start equals end)
|
|
63
|
+
* Array.from(range(5, 3)); // [] (positive step, start > end)
|
|
64
|
+
* Array.from(range(3, 5, -1)); // [] (negative step, start < end)
|
|
65
|
+
*
|
|
66
|
+
* // Using with iterator protocol manually
|
|
67
|
+
* const gen = range(1, 4);
|
|
68
|
+
* console.log(gen.next()); // { value: 1, done: false }
|
|
69
|
+
* console.log(gen.next()); // { value: 2, done: false }
|
|
70
|
+
* console.log(gen.next()); // { value: 3, done: false }
|
|
71
|
+
* console.log(gen.next()); // { value: undefined, done: true }
|
|
72
|
+
*
|
|
73
|
+
* // Practical usage patterns
|
|
74
|
+
* // Create index sequences
|
|
75
|
+
* const indices = Array.from(range(0, items.length));
|
|
76
|
+
*
|
|
77
|
+
* // Generate test data
|
|
78
|
+
* const testIds = [...range(1, 101)]; // [1, 2, ..., 100]
|
|
79
|
+
*
|
|
80
|
+
* // Iterate with step intervals
|
|
81
|
+
* for (const minute of range(0, 60, 5)) {
|
|
82
|
+
* scheduleTask(minute); // Every 5 minutes
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* // Countdown sequences
|
|
86
|
+
* for (const count of range(10, 0, -1)) {
|
|
87
|
+
* console.log(`T-minus ${count}`);
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare const range: RangeFnOverload;
|
|
92
|
+
type RangeFnOverload = {
|
|
93
|
+
(start: SafeUintWithSmallInt, end: SafeUintWithSmallInt, step?: PositiveSafeIntWithSmallInt): Generator<SafeUint, void, unknown>;
|
|
94
|
+
(start: SafeIntWithSmallInt, end: SafeIntWithSmallInt, step?: NonZeroSafeIntWithSmallInt): Generator<SafeInt, void, unknown>;
|
|
95
|
+
};
|
|
96
|
+
export {};
|
|
97
|
+
//# sourceMappingURL=range.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.d.mts","sourceRoot":"","sources":["../../src/iterator/range.mts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyFG;AACH,eAAO,MAAM,KAAK,EAAE,eAYA,CAAC;AAErB,KAAK,eAAe,GAAG;IACrB,CACE,KAAK,EAAE,oBAAoB,EAC3B,GAAG,EAAE,oBAAoB,EACzB,IAAI,CAAC,EAAE,2BAA2B,GACjC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IAEtC,CACE,KAAK,EAAE,mBAAmB,EAC1B,GAAG,EAAE,mBAAmB,EACxB,IAAI,CAAC,EAAE,0BAA0B,GAChC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;CACtC,CAAC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import '../number/branded-types/finite-number.mjs';
|
|
2
|
+
import '../number/branded-types/int.mjs';
|
|
3
|
+
import '../number/branded-types/int16.mjs';
|
|
4
|
+
import '../number/branded-types/int32.mjs';
|
|
5
|
+
import '../number/branded-types/non-negative-finite-number.mjs';
|
|
6
|
+
import '../number/branded-types/non-negative-int16.mjs';
|
|
7
|
+
import '../number/branded-types/non-negative-int32.mjs';
|
|
8
|
+
import '../number/branded-types/non-zero-finite-number.mjs';
|
|
9
|
+
import '../number/branded-types/non-zero-int.mjs';
|
|
10
|
+
import '../number/branded-types/non-zero-int16.mjs';
|
|
11
|
+
import '../number/branded-types/non-zero-int32.mjs';
|
|
12
|
+
import '../number/branded-types/non-zero-safe-int.mjs';
|
|
13
|
+
import '../number/branded-types/non-zero-uint16.mjs';
|
|
14
|
+
import '../number/branded-types/non-zero-uint32.mjs';
|
|
15
|
+
import '../number/branded-types/positive-finite-number.mjs';
|
|
16
|
+
import '../number/branded-types/positive-int.mjs';
|
|
17
|
+
import '../number/branded-types/positive-int16.mjs';
|
|
18
|
+
import '../number/branded-types/positive-int32.mjs';
|
|
19
|
+
import '../number/branded-types/positive-safe-int.mjs';
|
|
20
|
+
import '../number/branded-types/positive-uint16.mjs';
|
|
21
|
+
import '../number/branded-types/positive-uint32.mjs';
|
|
22
|
+
import { asSafeInt, SafeInt } from '../number/branded-types/safe-int.mjs';
|
|
23
|
+
import '../number/branded-types/safe-uint.mjs';
|
|
24
|
+
import '../number/branded-types/uint.mjs';
|
|
25
|
+
import '../number/branded-types/uint16.mjs';
|
|
26
|
+
import '../number/branded-types/uint32.mjs';
|
|
27
|
+
import '../number/enum/int8.mjs';
|
|
28
|
+
import '../number/enum/uint8.mjs';
|
|
29
|
+
import '../number/num.mjs';
|
|
30
|
+
import '../number/refined-number-utils.mjs';
|
|
31
|
+
|
|
32
|
+
/* eslint-disable @typescript-eslint/no-unsafe-type-assertion, func-names */
|
|
33
|
+
/**
|
|
34
|
+
* Generates a sequence of numbers within a specified range using a generator function.
|
|
35
|
+
*
|
|
36
|
+
* This function creates a generator that yields numbers from `start` (inclusive) to `end` (exclusive)
|
|
37
|
+
* with the specified `step` increment/decrement. The function implements the JavaScript iterator protocol,
|
|
38
|
+
* making it compatible with for-of loops, spread operator, Array.from(), and other iterator consumers.
|
|
39
|
+
*
|
|
40
|
+
* The function has two overloaded signatures:
|
|
41
|
+
* 1. For non-negative ranges: accepts SafeUint parameters and optional positive step
|
|
42
|
+
* 2. For general ranges: accepts SafeInt parameters and optional non-zero step
|
|
43
|
+
*
|
|
44
|
+
* **Generator Behavior:**
|
|
45
|
+
* - Yields values lazily (computed on-demand)
|
|
46
|
+
* - Returns `void` when iteration completes
|
|
47
|
+
* - Does not accept sent values (next parameter is ignored)
|
|
48
|
+
* - Automatically handles direction based on step sign and start/end relationship
|
|
49
|
+
*
|
|
50
|
+
* **Step Parameter Behavior:**
|
|
51
|
+
* - Positive step: iterates from start toward end (start < end expected)
|
|
52
|
+
* - Negative step: iterates from start toward end (start > end expected)
|
|
53
|
+
* - Zero step: not allowed (NonZeroSafeInt constraint)
|
|
54
|
+
* - Default step: 1 (positive direction)
|
|
55
|
+
*
|
|
56
|
+
* **Edge Cases:**
|
|
57
|
+
* - When start equals end: yields no values (empty sequence)
|
|
58
|
+
* - When step direction conflicts with start/end relationship: yields no values
|
|
59
|
+
* - All parameters must be safe integers (within JavaScript's safe integer range)
|
|
60
|
+
*
|
|
61
|
+
* @template T - The specific SafeInt or SafeUint type being generated
|
|
62
|
+
* @param start - The starting number of the sequence (inclusive). Must be a safe integer.
|
|
63
|
+
* @param end - The end number of the sequence (exclusive). Must be a safe integer.
|
|
64
|
+
* @param step - The increment or decrement value. Defaults to 1. Must be non-zero safe integer.
|
|
65
|
+
* @returns A Generator object that yields safe integers in the specified range.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* // Basic ascending range
|
|
70
|
+
* for (const n of range(0, 5)) {
|
|
71
|
+
* console.log(n); // Outputs: 0, 1, 2, 3, 4
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* // Range with custom step
|
|
75
|
+
* for (const n of range(0, 10, 2)) {
|
|
76
|
+
* console.log(n); // Outputs: 0, 2, 4, 6, 8
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* // Descending range with negative step
|
|
80
|
+
* for (const n of range(10, 0, -1)) {
|
|
81
|
+
* console.log(n); // Outputs: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* // Negative numbers with negative step
|
|
85
|
+
* for (const n of range(0, -10, -1)) {
|
|
86
|
+
* console.log(n); // Outputs: 0, -1, -2, -3, -4, -5, -6, -7, -8, -9
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* // Convert generator to array
|
|
90
|
+
* const numbers = Array.from(range(1, 4)); // [1, 2, 3]
|
|
91
|
+
* const evens = [...range(0, 11, 2)]; // [0, 2, 4, 6, 8, 10]
|
|
92
|
+
*
|
|
93
|
+
* // Empty ranges
|
|
94
|
+
* Array.from(range(5, 5)); // [] (start equals end)
|
|
95
|
+
* Array.from(range(5, 3)); // [] (positive step, start > end)
|
|
96
|
+
* Array.from(range(3, 5, -1)); // [] (negative step, start < end)
|
|
97
|
+
*
|
|
98
|
+
* // Using with iterator protocol manually
|
|
99
|
+
* const gen = range(1, 4);
|
|
100
|
+
* console.log(gen.next()); // { value: 1, done: false }
|
|
101
|
+
* console.log(gen.next()); // { value: 2, done: false }
|
|
102
|
+
* console.log(gen.next()); // { value: 3, done: false }
|
|
103
|
+
* console.log(gen.next()); // { value: undefined, done: true }
|
|
104
|
+
*
|
|
105
|
+
* // Practical usage patterns
|
|
106
|
+
* // Create index sequences
|
|
107
|
+
* const indices = Array.from(range(0, items.length));
|
|
108
|
+
*
|
|
109
|
+
* // Generate test data
|
|
110
|
+
* const testIds = [...range(1, 101)]; // [1, 2, ..., 100]
|
|
111
|
+
*
|
|
112
|
+
* // Iterate with step intervals
|
|
113
|
+
* for (const minute of range(0, 60, 5)) {
|
|
114
|
+
* scheduleTask(minute); // Every 5 minutes
|
|
115
|
+
* }
|
|
116
|
+
*
|
|
117
|
+
* // Countdown sequences
|
|
118
|
+
* for (const count of range(10, 0, -1)) {
|
|
119
|
+
* console.log(`T-minus ${count}`);
|
|
120
|
+
* }
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
const range = function* (start, end, step = 1) {
|
|
124
|
+
for (let mut_i = asSafeInt(start); step > 0 ? mut_i < end : mut_i > end; mut_i = SafeInt.add(mut_i, step)) {
|
|
125
|
+
yield mut_i;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export { range };
|
|
130
|
+
//# sourceMappingURL=range.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.mjs","sources":["../../src/iterator/range.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFG;AACI,MAAM,KAAK,GAAoB,WACpC,KAA0B,EAC1B,GAAwB,EACxB,IAAA,GAAmC,CAAC,EAAA;AAEpC,IAAA,KACE,IAAI,KAAK,GAAY,SAAS,CAAC,KAAK,CAAC,EACrC,IAAI,GAAG,CAAC,GAAG,KAAK,GAAG,GAAG,GAAG,KAAK,GAAG,GAAG,EACpC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,EAChC;AACA,QAAA,MAAM,KAAK;;AAEf;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/json/index.mts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|