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,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements the logical implication (if-then) operator.
|
|
3
|
+
*
|
|
4
|
+
* Returns `true` if the antecedent is `false` or the consequent is `true`.
|
|
5
|
+
* In logical terms: `antecedent → consequent` is equivalent to `¬antecedent ∨ consequent`.
|
|
6
|
+
*
|
|
7
|
+
* **Truth table:**
|
|
8
|
+
* - `true → true` = `true` (valid implication)
|
|
9
|
+
* - `true → false` = `false` (invalid implication)
|
|
10
|
+
* - `false → true` = `true` (vacuously true)
|
|
11
|
+
* - `false → false` = `true` (vacuously true)
|
|
12
|
+
*
|
|
13
|
+
* @param antecedent - The condition (if part)
|
|
14
|
+
* @param consequent - The result that should hold if the condition is true (then part)
|
|
15
|
+
* @returns `true` if the implication holds, `false` otherwise
|
|
16
|
+
*
|
|
17
|
+
* @example Basic truth table demonstration
|
|
18
|
+
* ```typescript
|
|
19
|
+
* ifThen(true, true); // true (if true then true = true)
|
|
20
|
+
* ifThen(true, false); // false (if true then false = false)
|
|
21
|
+
* ifThen(false, true); // true (if false then true = true - vacuously true)
|
|
22
|
+
* ifThen(false, false); // true (if false then false = true - vacuously true)
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Validation logic - "if required then must have value"
|
|
26
|
+
* ```typescript
|
|
27
|
+
* function validateField(value: string, isRequired: boolean): boolean {
|
|
28
|
+
* const hasValue = value.trim().length > 0;
|
|
29
|
+
* return ifThen(isRequired, hasValue);
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* validateField('hello', true); // true (required and has value)
|
|
33
|
+
* validateField('', true); // false (required but no value)
|
|
34
|
+
* validateField('', false); // true (not required, so valid)
|
|
35
|
+
* validateField('hello', false); // true (not required, but has value is fine)
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example Access control - "if admin then has all permissions"
|
|
39
|
+
* ```typescript
|
|
40
|
+
* function checkPermission(user: User, permission: string): boolean {
|
|
41
|
+
* const isAdmin = user.role === 'admin';
|
|
42
|
+
* const hasPermission = user.permissions.includes(permission);
|
|
43
|
+
*
|
|
44
|
+
* // Admin must have all permissions
|
|
45
|
+
* return ifThen(isAdmin, hasPermission);
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* const adminUser = { role: 'admin', permissions: ['read', 'write'] };
|
|
49
|
+
* checkPermission(adminUser, 'delete'); // false (admin without delete permission = invalid)
|
|
50
|
+
*
|
|
51
|
+
* const regularUser = { role: 'user', permissions: ['read'] };
|
|
52
|
+
* checkPermission(regularUser, 'delete'); // true (non-admin without permission is valid)
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example Contract validation - "if premium then features enabled"
|
|
56
|
+
* ```typescript
|
|
57
|
+
* interface Subscription {
|
|
58
|
+
* isPremium: boolean;
|
|
59
|
+
* features: {
|
|
60
|
+
* advancedAnalytics: boolean;
|
|
61
|
+
* unlimitedStorage: boolean;
|
|
62
|
+
* prioritySupport: boolean;
|
|
63
|
+
* };
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* function validateSubscription(sub: Subscription): boolean {
|
|
67
|
+
* // If premium, then all premium features must be enabled
|
|
68
|
+
* return ifThen(sub.isPremium,
|
|
69
|
+
* sub.features.advancedAnalytics &&
|
|
70
|
+
* sub.features.unlimitedStorage &&
|
|
71
|
+
* sub.features.prioritySupport
|
|
72
|
+
* );
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example Chaining multiple implications
|
|
77
|
+
* ```typescript
|
|
78
|
+
* // "If A then B" AND "If B then C"
|
|
79
|
+
* function validateChain(a: boolean, b: boolean, c: boolean): boolean {
|
|
80
|
+
* return ifThen(a, b) && ifThen(b, c);
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* validateChain(true, true, true); // true (valid chain)
|
|
84
|
+
* validateChain(true, false, true); // false (breaks at first implication)
|
|
85
|
+
* validateChain(false, false, false); // true (vacuously true chain)
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @example Negation patterns
|
|
89
|
+
* ```typescript
|
|
90
|
+
* // "If not expired then valid" is equivalent to "expired OR valid"
|
|
91
|
+
* const isExpired = Date.now() > expiryDate;
|
|
92
|
+
* const isValid = checkValidity();
|
|
93
|
+
* const result = ifThen(!isExpired, isValid);
|
|
94
|
+
* // Same as: isExpired || isValid
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare const ifThen: (antecedent: boolean, consequent: boolean) => boolean;
|
|
98
|
+
//# sourceMappingURL=if-then.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"if-then.d.mts","sourceRoot":"","sources":["../../src/others/if-then.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+FG;AACH,eAAO,MAAM,MAAM,GAAI,YAAY,OAAO,EAAE,YAAY,OAAO,KAAG,OACvC,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements the logical implication (if-then) operator.
|
|
3
|
+
*
|
|
4
|
+
* Returns `true` if the antecedent is `false` or the consequent is `true`.
|
|
5
|
+
* In logical terms: `antecedent → consequent` is equivalent to `¬antecedent ∨ consequent`.
|
|
6
|
+
*
|
|
7
|
+
* **Truth table:**
|
|
8
|
+
* - `true → true` = `true` (valid implication)
|
|
9
|
+
* - `true → false` = `false` (invalid implication)
|
|
10
|
+
* - `false → true` = `true` (vacuously true)
|
|
11
|
+
* - `false → false` = `true` (vacuously true)
|
|
12
|
+
*
|
|
13
|
+
* @param antecedent - The condition (if part)
|
|
14
|
+
* @param consequent - The result that should hold if the condition is true (then part)
|
|
15
|
+
* @returns `true` if the implication holds, `false` otherwise
|
|
16
|
+
*
|
|
17
|
+
* @example Basic truth table demonstration
|
|
18
|
+
* ```typescript
|
|
19
|
+
* ifThen(true, true); // true (if true then true = true)
|
|
20
|
+
* ifThen(true, false); // false (if true then false = false)
|
|
21
|
+
* ifThen(false, true); // true (if false then true = true - vacuously true)
|
|
22
|
+
* ifThen(false, false); // true (if false then false = true - vacuously true)
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Validation logic - "if required then must have value"
|
|
26
|
+
* ```typescript
|
|
27
|
+
* function validateField(value: string, isRequired: boolean): boolean {
|
|
28
|
+
* const hasValue = value.trim().length > 0;
|
|
29
|
+
* return ifThen(isRequired, hasValue);
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* validateField('hello', true); // true (required and has value)
|
|
33
|
+
* validateField('', true); // false (required but no value)
|
|
34
|
+
* validateField('', false); // true (not required, so valid)
|
|
35
|
+
* validateField('hello', false); // true (not required, but has value is fine)
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example Access control - "if admin then has all permissions"
|
|
39
|
+
* ```typescript
|
|
40
|
+
* function checkPermission(user: User, permission: string): boolean {
|
|
41
|
+
* const isAdmin = user.role === 'admin';
|
|
42
|
+
* const hasPermission = user.permissions.includes(permission);
|
|
43
|
+
*
|
|
44
|
+
* // Admin must have all permissions
|
|
45
|
+
* return ifThen(isAdmin, hasPermission);
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* const adminUser = { role: 'admin', permissions: ['read', 'write'] };
|
|
49
|
+
* checkPermission(adminUser, 'delete'); // false (admin without delete permission = invalid)
|
|
50
|
+
*
|
|
51
|
+
* const regularUser = { role: 'user', permissions: ['read'] };
|
|
52
|
+
* checkPermission(regularUser, 'delete'); // true (non-admin without permission is valid)
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example Contract validation - "if premium then features enabled"
|
|
56
|
+
* ```typescript
|
|
57
|
+
* interface Subscription {
|
|
58
|
+
* isPremium: boolean;
|
|
59
|
+
* features: {
|
|
60
|
+
* advancedAnalytics: boolean;
|
|
61
|
+
* unlimitedStorage: boolean;
|
|
62
|
+
* prioritySupport: boolean;
|
|
63
|
+
* };
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* function validateSubscription(sub: Subscription): boolean {
|
|
67
|
+
* // If premium, then all premium features must be enabled
|
|
68
|
+
* return ifThen(sub.isPremium,
|
|
69
|
+
* sub.features.advancedAnalytics &&
|
|
70
|
+
* sub.features.unlimitedStorage &&
|
|
71
|
+
* sub.features.prioritySupport
|
|
72
|
+
* );
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example Chaining multiple implications
|
|
77
|
+
* ```typescript
|
|
78
|
+
* // "If A then B" AND "If B then C"
|
|
79
|
+
* function validateChain(a: boolean, b: boolean, c: boolean): boolean {
|
|
80
|
+
* return ifThen(a, b) && ifThen(b, c);
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* validateChain(true, true, true); // true (valid chain)
|
|
84
|
+
* validateChain(true, false, true); // false (breaks at first implication)
|
|
85
|
+
* validateChain(false, false, false); // true (vacuously true chain)
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @example Negation patterns
|
|
89
|
+
* ```typescript
|
|
90
|
+
* // "If not expired then valid" is equivalent to "expired OR valid"
|
|
91
|
+
* const isExpired = Date.now() > expiryDate;
|
|
92
|
+
* const isValid = checkValidity();
|
|
93
|
+
* const result = ifThen(!isExpired, isValid);
|
|
94
|
+
* // Same as: isExpired || isValid
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
const ifThen = (antecedent, consequent) => !antecedent || consequent;
|
|
98
|
+
|
|
99
|
+
export { ifThen };
|
|
100
|
+
//# sourceMappingURL=if-then.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"if-then.mjs","sources":["../../src/others/if-then.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+FG;AACI,MAAM,MAAM,GAAG,CAAC,UAAmB,EAAE,UAAmB,KAC7D,CAAC,UAAU,IAAI;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './cast-mutable.mjs';
|
|
2
|
+
export * from './cast-readonly.mjs';
|
|
3
|
+
export * from './if-then.mjs';
|
|
4
|
+
export * from './map-nullable.mjs';
|
|
5
|
+
export * from './memoize-function.mjs';
|
|
6
|
+
export * from './tuple.mjs';
|
|
7
|
+
export * from './unknown-to-string.mjs';
|
|
8
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/others/index.mts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { castDeepMutable, castMutable } from './cast-mutable.mjs';
|
|
2
|
+
export { castDeepReadonly, castReadonly } from './cast-readonly.mjs';
|
|
3
|
+
export { ifThen } from './if-then.mjs';
|
|
4
|
+
export { mapNullable } from './map-nullable.mjs';
|
|
5
|
+
export { memoizeFunction } from './memoize-function.mjs';
|
|
6
|
+
export { tp } from './tuple.mjs';
|
|
7
|
+
export { unknownToString } from './unknown-to-string.mjs';
|
|
8
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies a function to a value if the value is not `null` or `undefined`.
|
|
3
|
+
* If the value is `null` or `undefined`, it returns `undefined`.
|
|
4
|
+
*
|
|
5
|
+
* This function provides a safe way to transform nullable values without explicit null checks.
|
|
6
|
+
* It's similar to Optional.map() but works directly with TypeScript's nullable types.
|
|
7
|
+
* Supports both regular and curried usage for functional composition.
|
|
8
|
+
*
|
|
9
|
+
* @template A - The type of the input value (excluding null/undefined)
|
|
10
|
+
* @template B - The type of the value returned by the mapping function
|
|
11
|
+
* @param value - The value to potentially transform (can be `A`, `null`, or `undefined`)
|
|
12
|
+
* @param mapFn - A function that transforms a non-nullable value of type `A` to type `B`
|
|
13
|
+
* @returns The result of applying `mapFn` to `value` if value is not null/undefined; otherwise `undefined`
|
|
14
|
+
*
|
|
15
|
+
* @example Basic usage with nullable values
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Safe string transformation
|
|
18
|
+
* mapNullable("hello", s => s.toUpperCase()); // "HELLO"
|
|
19
|
+
* mapNullable(null, s => s.toUpperCase()); // undefined
|
|
20
|
+
* mapNullable(undefined, s => s.toUpperCase()); // undefined
|
|
21
|
+
*
|
|
22
|
+
* // Number operations
|
|
23
|
+
* mapNullable(5, n => n * 2); // 10
|
|
24
|
+
* mapNullable(0, n => n * 2); // 0 (note: 0 is not null/undefined)
|
|
25
|
+
* mapNullable(null as number | null, n => n * 2); // undefined
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Working with optional object properties
|
|
29
|
+
* ```typescript
|
|
30
|
+
* interface User {
|
|
31
|
+
* id: number;
|
|
32
|
+
* name?: string;
|
|
33
|
+
* email?: string;
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* function formatUserDisplay(user: User): string {
|
|
37
|
+
* const displayName = mapNullable(user.name, name => name.toUpperCase()) ?? 'Anonymous';
|
|
38
|
+
* const emailDomain = mapNullable(user.email, email => email.split('@')[1]);
|
|
39
|
+
*
|
|
40
|
+
* return `${displayName} ${emailDomain ? `(${emailDomain})` : ''}`;
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* formatUserDisplay({ id: 1, name: 'John', email: 'john@example.com' }); // "JOHN (example.com)"
|
|
44
|
+
* formatUserDisplay({ id: 2 }); // "Anonymous "
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example Curried usage for functional composition
|
|
48
|
+
* ```typescript
|
|
49
|
+
* // Create reusable transformers
|
|
50
|
+
* const toUpperCase = mapNullable((s: string) => s.toUpperCase());
|
|
51
|
+
* const addPrefix = mapNullable((s: string) => `PREFIX_${s}`);
|
|
52
|
+
* const parseNumber = mapNullable((s: string) => parseInt(s, 10));
|
|
53
|
+
*
|
|
54
|
+
* // Use in different contexts
|
|
55
|
+
* toUpperCase("hello"); // "HELLO"
|
|
56
|
+
* toUpperCase(null); // undefined
|
|
57
|
+
*
|
|
58
|
+
* // Compose transformations
|
|
59
|
+
* const processString = (s: string | null) => {
|
|
60
|
+
* const upper = toUpperCase(s);
|
|
61
|
+
* return addPrefix(upper);
|
|
62
|
+
* };
|
|
63
|
+
*
|
|
64
|
+
* processString("test"); // "PREFIX_TEST"
|
|
65
|
+
* processString(null); // undefined
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example Chaining nullable operations
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // API response handling
|
|
71
|
+
* interface ApiResponse {
|
|
72
|
+
* data?: {
|
|
73
|
+
* user?: {
|
|
74
|
+
* profile?: {
|
|
75
|
+
* displayName?: string;
|
|
76
|
+
* };
|
|
77
|
+
* };
|
|
78
|
+
* };
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* function getDisplayName(response: ApiResponse): string | undefined {
|
|
82
|
+
* return mapNullable(
|
|
83
|
+
* response.data?.user?.profile?.displayName,
|
|
84
|
+
* name => name.trim().toUpperCase()
|
|
85
|
+
* );
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* // Chain multiple transformations
|
|
89
|
+
* function processNullableChain(value: string | null): string | undefined {
|
|
90
|
+
* const step1 = mapNullable(value, v => v.trim());
|
|
91
|
+
* const step2 = mapNullable(step1, v => v.length > 0 ? v : null);
|
|
92
|
+
* const step3 = mapNullable(step2, v => v.toUpperCase());
|
|
93
|
+
* return step3;
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example Integration with array methods
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const nullableNumbers: (number | null | undefined)[] = [1, null, 3, undefined, 5];
|
|
100
|
+
*
|
|
101
|
+
* // Transform and filter in one step
|
|
102
|
+
* const doubled = nullableNumbers
|
|
103
|
+
* .map(n => mapNullable(n, x => x * 2))
|
|
104
|
+
* .filter((n): n is number => n !== undefined);
|
|
105
|
+
* // Result: [2, 6, 10]
|
|
106
|
+
*
|
|
107
|
+
* // Process optional array elements
|
|
108
|
+
* const users: Array<{ name?: string }> = [
|
|
109
|
+
* { name: 'Alice' },
|
|
110
|
+
* { name: undefined },
|
|
111
|
+
* { name: 'Bob' }
|
|
112
|
+
* ];
|
|
113
|
+
*
|
|
114
|
+
* const upperNames = users
|
|
115
|
+
* .map(u => mapNullable(u.name, n => n.toUpperCase()))
|
|
116
|
+
* .filter((n): n is string => n !== undefined);
|
|
117
|
+
* // Result: ['ALICE', 'BOB']
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @example Error handling patterns
|
|
121
|
+
* ```typescript
|
|
122
|
+
* // Safe JSON parsing
|
|
123
|
+
* function parseJsonSafe<T>(json: string | null): T | undefined {
|
|
124
|
+
* return mapNullable(json, j => {
|
|
125
|
+
* try {
|
|
126
|
+
* return JSON.parse(j) as T;
|
|
127
|
+
* } catch {
|
|
128
|
+
* return null;
|
|
129
|
+
* }
|
|
130
|
+
* }) ?? undefined;
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* // Safe property access with computation
|
|
134
|
+
* function calculateAge(birthYear: number | null): string | undefined {
|
|
135
|
+
* return mapNullable(birthYear, year => {
|
|
136
|
+
* const age = new Date().getFullYear() - year;
|
|
137
|
+
* return age >= 0 ? `${age} years old` : null;
|
|
138
|
+
* }) ?? undefined;
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @see Optional - For more complex optional value handling
|
|
143
|
+
* @see Result - For error handling with detailed error information
|
|
144
|
+
*/
|
|
145
|
+
export declare const mapNullable: MapNullableFnOverload;
|
|
146
|
+
type MapNullableFnOverload = {
|
|
147
|
+
<const A, const B>(value: A | null | undefined, mapFn: (v: A) => B): B | undefined;
|
|
148
|
+
<const A, const B>(mapFn: (v: A) => B): (value: A | null | undefined) => B | undefined;
|
|
149
|
+
};
|
|
150
|
+
export {};
|
|
151
|
+
//# sourceMappingURL=map-nullable.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-nullable.d.mts","sourceRoot":"","sources":["../../src/others/map-nullable.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+IG;AACH,eAAO,MAAM,WAAW,EAAE,qBAeC,CAAC;AAE5B,KAAK,qBAAqB,GAAG;IAC3B,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EACf,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAC3B,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GACjB,CAAC,GAAG,SAAS,CAAC;IAGjB,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EACf,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GACjB,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,KAAK,CAAC,GAAG,SAAS,CAAC;CACnD,CAAC"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies a function to a value if the value is not `null` or `undefined`.
|
|
3
|
+
* If the value is `null` or `undefined`, it returns `undefined`.
|
|
4
|
+
*
|
|
5
|
+
* This function provides a safe way to transform nullable values without explicit null checks.
|
|
6
|
+
* It's similar to Optional.map() but works directly with TypeScript's nullable types.
|
|
7
|
+
* Supports both regular and curried usage for functional composition.
|
|
8
|
+
*
|
|
9
|
+
* @template A - The type of the input value (excluding null/undefined)
|
|
10
|
+
* @template B - The type of the value returned by the mapping function
|
|
11
|
+
* @param value - The value to potentially transform (can be `A`, `null`, or `undefined`)
|
|
12
|
+
* @param mapFn - A function that transforms a non-nullable value of type `A` to type `B`
|
|
13
|
+
* @returns The result of applying `mapFn` to `value` if value is not null/undefined; otherwise `undefined`
|
|
14
|
+
*
|
|
15
|
+
* @example Basic usage with nullable values
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Safe string transformation
|
|
18
|
+
* mapNullable("hello", s => s.toUpperCase()); // "HELLO"
|
|
19
|
+
* mapNullable(null, s => s.toUpperCase()); // undefined
|
|
20
|
+
* mapNullable(undefined, s => s.toUpperCase()); // undefined
|
|
21
|
+
*
|
|
22
|
+
* // Number operations
|
|
23
|
+
* mapNullable(5, n => n * 2); // 10
|
|
24
|
+
* mapNullable(0, n => n * 2); // 0 (note: 0 is not null/undefined)
|
|
25
|
+
* mapNullable(null as number | null, n => n * 2); // undefined
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Working with optional object properties
|
|
29
|
+
* ```typescript
|
|
30
|
+
* interface User {
|
|
31
|
+
* id: number;
|
|
32
|
+
* name?: string;
|
|
33
|
+
* email?: string;
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* function formatUserDisplay(user: User): string {
|
|
37
|
+
* const displayName = mapNullable(user.name, name => name.toUpperCase()) ?? 'Anonymous';
|
|
38
|
+
* const emailDomain = mapNullable(user.email, email => email.split('@')[1]);
|
|
39
|
+
*
|
|
40
|
+
* return `${displayName} ${emailDomain ? `(${emailDomain})` : ''}`;
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* formatUserDisplay({ id: 1, name: 'John', email: 'john@example.com' }); // "JOHN (example.com)"
|
|
44
|
+
* formatUserDisplay({ id: 2 }); // "Anonymous "
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example Curried usage for functional composition
|
|
48
|
+
* ```typescript
|
|
49
|
+
* // Create reusable transformers
|
|
50
|
+
* const toUpperCase = mapNullable((s: string) => s.toUpperCase());
|
|
51
|
+
* const addPrefix = mapNullable((s: string) => `PREFIX_${s}`);
|
|
52
|
+
* const parseNumber = mapNullable((s: string) => parseInt(s, 10));
|
|
53
|
+
*
|
|
54
|
+
* // Use in different contexts
|
|
55
|
+
* toUpperCase("hello"); // "HELLO"
|
|
56
|
+
* toUpperCase(null); // undefined
|
|
57
|
+
*
|
|
58
|
+
* // Compose transformations
|
|
59
|
+
* const processString = (s: string | null) => {
|
|
60
|
+
* const upper = toUpperCase(s);
|
|
61
|
+
* return addPrefix(upper);
|
|
62
|
+
* };
|
|
63
|
+
*
|
|
64
|
+
* processString("test"); // "PREFIX_TEST"
|
|
65
|
+
* processString(null); // undefined
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example Chaining nullable operations
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // API response handling
|
|
71
|
+
* interface ApiResponse {
|
|
72
|
+
* data?: {
|
|
73
|
+
* user?: {
|
|
74
|
+
* profile?: {
|
|
75
|
+
* displayName?: string;
|
|
76
|
+
* };
|
|
77
|
+
* };
|
|
78
|
+
* };
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* function getDisplayName(response: ApiResponse): string | undefined {
|
|
82
|
+
* return mapNullable(
|
|
83
|
+
* response.data?.user?.profile?.displayName,
|
|
84
|
+
* name => name.trim().toUpperCase()
|
|
85
|
+
* );
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* // Chain multiple transformations
|
|
89
|
+
* function processNullableChain(value: string | null): string | undefined {
|
|
90
|
+
* const step1 = mapNullable(value, v => v.trim());
|
|
91
|
+
* const step2 = mapNullable(step1, v => v.length > 0 ? v : null);
|
|
92
|
+
* const step3 = mapNullable(step2, v => v.toUpperCase());
|
|
93
|
+
* return step3;
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example Integration with array methods
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const nullableNumbers: (number | null | undefined)[] = [1, null, 3, undefined, 5];
|
|
100
|
+
*
|
|
101
|
+
* // Transform and filter in one step
|
|
102
|
+
* const doubled = nullableNumbers
|
|
103
|
+
* .map(n => mapNullable(n, x => x * 2))
|
|
104
|
+
* .filter((n): n is number => n !== undefined);
|
|
105
|
+
* // Result: [2, 6, 10]
|
|
106
|
+
*
|
|
107
|
+
* // Process optional array elements
|
|
108
|
+
* const users: Array<{ name?: string }> = [
|
|
109
|
+
* { name: 'Alice' },
|
|
110
|
+
* { name: undefined },
|
|
111
|
+
* { name: 'Bob' }
|
|
112
|
+
* ];
|
|
113
|
+
*
|
|
114
|
+
* const upperNames = users
|
|
115
|
+
* .map(u => mapNullable(u.name, n => n.toUpperCase()))
|
|
116
|
+
* .filter((n): n is string => n !== undefined);
|
|
117
|
+
* // Result: ['ALICE', 'BOB']
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @example Error handling patterns
|
|
121
|
+
* ```typescript
|
|
122
|
+
* // Safe JSON parsing
|
|
123
|
+
* function parseJsonSafe<T>(json: string | null): T | undefined {
|
|
124
|
+
* return mapNullable(json, j => {
|
|
125
|
+
* try {
|
|
126
|
+
* return JSON.parse(j) as T;
|
|
127
|
+
* } catch {
|
|
128
|
+
* return null;
|
|
129
|
+
* }
|
|
130
|
+
* }) ?? undefined;
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* // Safe property access with computation
|
|
134
|
+
* function calculateAge(birthYear: number | null): string | undefined {
|
|
135
|
+
* return mapNullable(birthYear, year => {
|
|
136
|
+
* const age = new Date().getFullYear() - year;
|
|
137
|
+
* return age >= 0 ? `${age} years old` : null;
|
|
138
|
+
* }) ?? undefined;
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @see Optional - For more complex optional value handling
|
|
143
|
+
* @see Result - For error handling with detailed error information
|
|
144
|
+
*/
|
|
145
|
+
const mapNullable = ((...args) => {
|
|
146
|
+
switch (args.length) {
|
|
147
|
+
case 2: {
|
|
148
|
+
const [value, mapFn] = args;
|
|
149
|
+
return value == null ? undefined : mapFn(value);
|
|
150
|
+
}
|
|
151
|
+
case 1: {
|
|
152
|
+
const [mapFn] = args;
|
|
153
|
+
return (value) => mapNullable(value, mapFn);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
export { mapNullable };
|
|
159
|
+
//# sourceMappingURL=map-nullable.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-nullable.mjs","sources":["../../src/others/map-nullable.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+IG;MACU,WAAW,IAA2B,CACjD,GAAG,IAE8B,KACqC;AACtE,IAAA,QAAQ,IAAI,CAAC,MAAM;QACjB,KAAK,CAAC,EAAE;AACN,YAAA,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,IAAI;AAC3B,YAAA,OAAO,KAAK,IAAI,IAAI,GAAG,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC;;QAEjD,KAAK,CAAC,EAAE;AACN,YAAA,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI;YACpB,OAAO,CAAC,KAA2B,KAAK,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC;;;AAGvE,CAAC;;;;"}
|