typetify 0.1.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/LICENSE +21 -0
- package/README.md +327 -0
- package/dist/async/index.d.mts +152 -0
- package/dist/async/index.d.ts +152 -0
- package/dist/async/index.js +49 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/index.mjs +4 -0
- package/dist/async/index.mjs.map +1 -0
- package/dist/chunk-2LJ6NZ6K.js +108 -0
- package/dist/chunk-2LJ6NZ6K.js.map +1 -0
- package/dist/chunk-44Y5JSGU.js +145 -0
- package/dist/chunk-44Y5JSGU.js.map +1 -0
- package/dist/chunk-4NXETABV.mjs +116 -0
- package/dist/chunk-4NXETABV.mjs.map +1 -0
- package/dist/chunk-6ZBTL74K.js +129 -0
- package/dist/chunk-6ZBTL74K.js.map +1 -0
- package/dist/chunk-CN3GYRJN.mjs +137 -0
- package/dist/chunk-CN3GYRJN.mjs.map +1 -0
- package/dist/chunk-CNTE6ZVH.js +359 -0
- package/dist/chunk-CNTE6ZVH.js.map +1 -0
- package/dist/chunk-DBENOSTA.js +156 -0
- package/dist/chunk-DBENOSTA.js.map +1 -0
- package/dist/chunk-DWIG5GF2.js +135 -0
- package/dist/chunk-DWIG5GF2.js.map +1 -0
- package/dist/chunk-EAUTTWTQ.mjs +231 -0
- package/dist/chunk-EAUTTWTQ.mjs.map +1 -0
- package/dist/chunk-FXWYPHA3.mjs +13 -0
- package/dist/chunk-FXWYPHA3.mjs.map +1 -0
- package/dist/chunk-GS3PP67B.js +200 -0
- package/dist/chunk-GS3PP67B.js.map +1 -0
- package/dist/chunk-J5LGTIGS.mjs +9 -0
- package/dist/chunk-J5LGTIGS.mjs.map +1 -0
- package/dist/chunk-JAOGY4JO.mjs +1007 -0
- package/dist/chunk-JAOGY4JO.mjs.map +1 -0
- package/dist/chunk-JB6UXRKD.mjs +97 -0
- package/dist/chunk-JB6UXRKD.mjs.map +1 -0
- package/dist/chunk-JQHUBZ4M.js +88 -0
- package/dist/chunk-JQHUBZ4M.js.map +1 -0
- package/dist/chunk-L3M7LGKL.mjs +128 -0
- package/dist/chunk-L3M7LGKL.mjs.map +1 -0
- package/dist/chunk-LT7JK7RJ.js +87 -0
- package/dist/chunk-LT7JK7RJ.js.map +1 -0
- package/dist/chunk-OEJK37LO.mjs +328 -0
- package/dist/chunk-OEJK37LO.mjs.map +1 -0
- package/dist/chunk-OPVES6W2.js +16 -0
- package/dist/chunk-OPVES6W2.js.map +1 -0
- package/dist/chunk-OWNUKWXV.js +291 -0
- package/dist/chunk-OWNUKWXV.js.map +1 -0
- package/dist/chunk-PQTXSQ4P.js +369 -0
- package/dist/chunk-PQTXSQ4P.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +11 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-QFR7DVAJ.mjs +63 -0
- package/dist/chunk-QFR7DVAJ.mjs.map +1 -0
- package/dist/chunk-SGQNLTRK.js +73 -0
- package/dist/chunk-SGQNLTRK.js.map +1 -0
- package/dist/chunk-SIA5BSVY.js +1054 -0
- package/dist/chunk-SIA5BSVY.js.map +1 -0
- package/dist/chunk-SRDWUHDY.mjs +188 -0
- package/dist/chunk-SRDWUHDY.mjs.map +1 -0
- package/dist/chunk-TXU7NTT4.js +249 -0
- package/dist/chunk-TXU7NTT4.js.map +1 -0
- package/dist/chunk-TZEWREAC.mjs +277 -0
- package/dist/chunk-TZEWREAC.mjs.map +1 -0
- package/dist/chunk-V6CWFDIJ.mjs +123 -0
- package/dist/chunk-V6CWFDIJ.mjs.map +1 -0
- package/dist/chunk-YBJC5WMX.mjs +341 -0
- package/dist/chunk-YBJC5WMX.mjs.map +1 -0
- package/dist/chunk-YOPAXITF.mjs +75 -0
- package/dist/chunk-YOPAXITF.mjs.map +1 -0
- package/dist/chunk-ZE4FDBRI.mjs +79 -0
- package/dist/chunk-ZE4FDBRI.mjs.map +1 -0
- package/dist/collection/index.d.mts +291 -0
- package/dist/collection/index.d.ts +291 -0
- package/dist/collection/index.js +125 -0
- package/dist/collection/index.js.map +1 -0
- package/dist/collection/index.mjs +4 -0
- package/dist/collection/index.mjs.map +1 -0
- package/dist/core/index.d.mts +85 -0
- package/dist/core/index.d.ts +85 -0
- package/dist/core/index.js +41 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/index.mjs +4 -0
- package/dist/core/index.mjs.map +1 -0
- package/dist/decorator/index.d.mts +165 -0
- package/dist/decorator/index.d.ts +165 -0
- package/dist/decorator/index.js +57 -0
- package/dist/decorator/index.js.map +1 -0
- package/dist/decorator/index.mjs +4 -0
- package/dist/decorator/index.mjs.map +1 -0
- package/dist/dx/index.d.mts +125 -0
- package/dist/dx/index.d.ts +125 -0
- package/dist/dx/index.js +53 -0
- package/dist/dx/index.js.map +1 -0
- package/dist/dx/index.mjs +4 -0
- package/dist/dx/index.mjs.map +1 -0
- package/dist/flow/index.d.mts +165 -0
- package/dist/flow/index.d.ts +165 -0
- package/dist/flow/index.js +50 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/flow/index.mjs +5 -0
- package/dist/flow/index.mjs.map +1 -0
- package/dist/fn/index.d.mts +77 -0
- package/dist/fn/index.d.ts +77 -0
- package/dist/fn/index.js +37 -0
- package/dist/fn/index.js.map +1 -0
- package/dist/fn/index.mjs +4 -0
- package/dist/fn/index.mjs.map +1 -0
- package/dist/guards/index.d.mts +165 -0
- package/dist/guards/index.d.ts +165 -0
- package/dist/guards/index.js +69 -0
- package/dist/guards/index.js.map +1 -0
- package/dist/guards/index.mjs +4 -0
- package/dist/guards/index.mjs.map +1 -0
- package/dist/index.d.mts +228 -0
- package/dist/index.d.ts +228 -0
- package/dist/index.js +775 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/dist/input/index.d.mts +185 -0
- package/dist/input/index.d.ts +185 -0
- package/dist/input/index.js +74 -0
- package/dist/input/index.js.map +1 -0
- package/dist/input/index.mjs +5 -0
- package/dist/input/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +209 -0
- package/dist/iterator/index.d.ts +209 -0
- package/dist/iterator/index.js +522 -0
- package/dist/iterator/index.js.map +1 -0
- package/dist/iterator/index.mjs +502 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/logic/index.d.mts +531 -0
- package/dist/logic/index.d.ts +531 -0
- package/dist/logic/index.js +416 -0
- package/dist/logic/index.js.map +1 -0
- package/dist/logic/index.mjs +367 -0
- package/dist/logic/index.mjs.map +1 -0
- package/dist/math/index.d.mts +86 -0
- package/dist/math/index.d.ts +86 -0
- package/dist/math/index.js +45 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/index.mjs +4 -0
- package/dist/math/index.mjs.map +1 -0
- package/dist/narrowing/index.d.mts +429 -0
- package/dist/narrowing/index.d.ts +429 -0
- package/dist/narrowing/index.js +220 -0
- package/dist/narrowing/index.js.map +1 -0
- package/dist/narrowing/index.mjs +186 -0
- package/dist/narrowing/index.mjs.map +1 -0
- package/dist/object/index.d.mts +327 -0
- package/dist/object/index.d.ts +327 -0
- package/dist/object/index.js +113 -0
- package/dist/object/index.js.map +1 -0
- package/dist/object/index.mjs +4 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/result/index.d.mts +201 -0
- package/dist/result/index.d.ts +201 -0
- package/dist/result/index.js +86 -0
- package/dist/result/index.js.map +1 -0
- package/dist/result/index.mjs +5 -0
- package/dist/result/index.mjs.map +1 -0
- package/dist/schema/index.d.mts +216 -0
- package/dist/schema/index.d.ts +216 -0
- package/dist/schema/index.js +410 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/index.mjs +384 -0
- package/dist/schema/index.mjs.map +1 -0
- package/dist/string/index.d.mts +102 -0
- package/dist/string/index.d.ts +102 -0
- package/dist/string/index.js +49 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/index.mjs +4 -0
- package/dist/string/index.mjs.map +1 -0
- package/dist/typed/index.d.mts +1962 -0
- package/dist/typed/index.d.ts +1962 -0
- package/dist/typed/index.js +193 -0
- package/dist/typed/index.js.map +1 -0
- package/dist/typed/index.mjs +4 -0
- package/dist/typed/index.mjs.map +1 -0
- package/dist/types-Db0vauC3.d.mts +258 -0
- package/dist/types-Db0vauC3.d.ts +258 -0
- package/dist/types-VsDp2t8s.d.mts +30 -0
- package/dist/types-VsDp2t8s.d.ts +30 -0
- package/package.json +157 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executes a function when condition is truthy.
|
|
3
|
+
* Replaces: `condition && doSomething()`
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* when(user.isAdmin, () => showAdminPanel())
|
|
7
|
+
* when(count > 0, () => processItems(count))
|
|
8
|
+
*
|
|
9
|
+
* // Instead of:
|
|
10
|
+
* user.isAdmin && showAdminPanel()
|
|
11
|
+
*/
|
|
12
|
+
declare function when<T>(condition: unknown, fn: () => T): T | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Returns value when condition is truthy, undefined otherwise.
|
|
15
|
+
* Replaces: `condition && value`
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const className = whenValue(isActive, 'active')
|
|
19
|
+
* // Instead of: isActive && 'active'
|
|
20
|
+
*/
|
|
21
|
+
declare function whenValue<T>(condition: unknown, value: T): T | undefined;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Executes a function unless condition is truthy.
|
|
25
|
+
* Replaces: `!condition && doSomething()`
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* unless(user.isBlocked, () => allowAccess())
|
|
29
|
+
*
|
|
30
|
+
* // Instead of:
|
|
31
|
+
* !user.isBlocked && allowAccess()
|
|
32
|
+
*/
|
|
33
|
+
declare function unless<T>(condition: unknown, fn: () => T): T | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Returns value unless condition is truthy, undefined otherwise.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const message = unlessValue(isHidden, 'Hello!')
|
|
39
|
+
*/
|
|
40
|
+
declare function unlessValue<T>(condition: unknown, value: T): T | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns one of two values based on a condition.
|
|
44
|
+
* Replaces: `condition ? valueA : valueB`
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const status = ifElse(isOnline, 'Online', 'Offline')
|
|
48
|
+
* const greeting = ifElse(user.name, `Hello ${user.name}`, 'Hello Guest')
|
|
49
|
+
*
|
|
50
|
+
* // Instead of:
|
|
51
|
+
* const status = isOnline ? 'Online' : 'Offline'
|
|
52
|
+
*/
|
|
53
|
+
declare function ifElse<T, F>(condition: unknown, whenTrue: T, whenFalse: F): T | F;
|
|
54
|
+
/**
|
|
55
|
+
* Lazy version - executes functions instead of evaluating values.
|
|
56
|
+
* Useful when values are expensive to compute.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const result = ifElseLazy(
|
|
60
|
+
* shouldCompute,
|
|
61
|
+
* () => expensiveComputation(),
|
|
62
|
+
* () => cachedValue
|
|
63
|
+
* )
|
|
64
|
+
*/
|
|
65
|
+
declare function ifElseLazy<T, F>(condition: unknown, whenTrue: () => T, whenFalse: () => F): T | F;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Pattern matching with multiple conditions.
|
|
69
|
+
* Replaces nested ternaries or switch statements.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* const message = cond(
|
|
73
|
+
* [score >= 90, 'Excellent'],
|
|
74
|
+
* [score >= 70, 'Good'],
|
|
75
|
+
* [score >= 50, 'Pass'],
|
|
76
|
+
* [true, 'Fail'] // default
|
|
77
|
+
* )
|
|
78
|
+
*
|
|
79
|
+
* // Instead of:
|
|
80
|
+
* const message = score >= 90 ? 'Excellent'
|
|
81
|
+
* : score >= 70 ? 'Good'
|
|
82
|
+
* : score >= 50 ? 'Pass'
|
|
83
|
+
* : 'Fail'
|
|
84
|
+
*/
|
|
85
|
+
declare function cond<T>(...pairs: readonly [condition: unknown, value: T][]): T | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Lazy version - evaluates functions instead of values.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* const result = condLazy(
|
|
91
|
+
* [isAdmin, () => getAdminData()],
|
|
92
|
+
* [isUser, () => getUserData()],
|
|
93
|
+
* [true, () => getGuestData()]
|
|
94
|
+
* )
|
|
95
|
+
*/
|
|
96
|
+
declare function condLazy<T>(...pairs: readonly [condition: unknown, fn: () => T][]): T | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Cond with predicate functions for more complex matching.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const category = condBy(
|
|
102
|
+
* age,
|
|
103
|
+
* [n => n < 13, 'child'],
|
|
104
|
+
* [n => n < 20, 'teen'],
|
|
105
|
+
* [n => n < 60, 'adult'],
|
|
106
|
+
* [() => true, 'senior']
|
|
107
|
+
* )
|
|
108
|
+
*/
|
|
109
|
+
declare function condBy<V, T>(value: V, ...pairs: readonly [predicate: (v: V) => boolean, result: T][]): T | undefined;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Type-safe pattern matching on values.
|
|
113
|
+
* Replaces switch statements with exhaustive checking.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* type Status = 'pending' | 'success' | 'error'
|
|
117
|
+
*
|
|
118
|
+
* const message = matchValue(status, {
|
|
119
|
+
* pending: 'Loading...',
|
|
120
|
+
* success: 'Done!',
|
|
121
|
+
* error: 'Failed!',
|
|
122
|
+
* })
|
|
123
|
+
*
|
|
124
|
+
* // Instead of:
|
|
125
|
+
* let message: string
|
|
126
|
+
* switch (status) {
|
|
127
|
+
* case 'pending': message = 'Loading...'; break
|
|
128
|
+
* case 'success': message = 'Done!'; break
|
|
129
|
+
* case 'error': message = 'Failed!'; break
|
|
130
|
+
* }
|
|
131
|
+
*/
|
|
132
|
+
declare function matchValue<T extends string | number, R>(value: T, cases: Record<T, R>): R;
|
|
133
|
+
/**
|
|
134
|
+
* Pattern matching with a default case.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* const icon = matchWithDefault(status, {
|
|
138
|
+
* success: '✓',
|
|
139
|
+
* error: '✗',
|
|
140
|
+
* }, '?')
|
|
141
|
+
*/
|
|
142
|
+
declare function matchWithDefault<T extends string | number, R, D>(value: T, cases: Partial<Record<T, R>>, defaultValue: D): R | D;
|
|
143
|
+
/**
|
|
144
|
+
* Lazy pattern matching - executes functions.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* const result = matchLazy(action.type, {
|
|
148
|
+
* increment: () => state + 1,
|
|
149
|
+
* decrement: () => state - 1,
|
|
150
|
+
* reset: () => 0,
|
|
151
|
+
* })
|
|
152
|
+
*/
|
|
153
|
+
declare function matchLazy<T extends string | number, R>(value: T, cases: Record<T, () => R>): R;
|
|
154
|
+
/**
|
|
155
|
+
* Pattern matching with type guards.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* const result = matchType(value, [
|
|
159
|
+
* [isString, (s) => s.toUpperCase()],
|
|
160
|
+
* [isNumber, (n) => n * 2],
|
|
161
|
+
* [isArray, (arr) => arr.length],
|
|
162
|
+
* ])
|
|
163
|
+
*/
|
|
164
|
+
declare function matchType<T, R>(value: T, matchers: readonly [guard: (v: T) => boolean, handler: (v: T) => R][]): R | undefined;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Returns the last value if all values are truthy, otherwise the first falsy value.
|
|
168
|
+
* Replaces: `a && b && c`
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* const result = and(user, user.isActive, user.hasPermission)
|
|
172
|
+
* // Instead of: user && user.isActive && user.hasPermission
|
|
173
|
+
*
|
|
174
|
+
* // With callback
|
|
175
|
+
* const data = and(isReady, hasData, () => processData())
|
|
176
|
+
*/
|
|
177
|
+
declare function and<T>(...values: readonly T[]): T | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* Returns true if all values are truthy.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* if (allTrue(isLoggedIn, hasPermission, isActive)) {
|
|
183
|
+
* // proceed
|
|
184
|
+
* }
|
|
185
|
+
*/
|
|
186
|
+
declare function allTrue(...values: readonly unknown[]): boolean;
|
|
187
|
+
/**
|
|
188
|
+
* Returns the result of the callback if all conditions are truthy.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* const result = andThen(
|
|
192
|
+
* [user, user?.isAdmin, user?.canEdit],
|
|
193
|
+
* () => editDocument()
|
|
194
|
+
* )
|
|
195
|
+
*/
|
|
196
|
+
declare function andThen<T>(conditions: readonly unknown[], fn: () => T): T | undefined;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns the first truthy value, or the last value if none are truthy.
|
|
200
|
+
* Replaces: `a || b || c`
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* const name = or(user.nickname, user.firstName, user.email, 'Anonymous')
|
|
204
|
+
* // Instead of: user.nickname || user.firstName || user.email || 'Anonymous'
|
|
205
|
+
*/
|
|
206
|
+
declare function or<T>(...values: readonly T[]): T | undefined;
|
|
207
|
+
/**
|
|
208
|
+
* Returns true if any value is truthy.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* if (anyTrue(isAdmin, isModerator, isOwner)) {
|
|
212
|
+
* // has elevated permissions
|
|
213
|
+
* }
|
|
214
|
+
*/
|
|
215
|
+
declare function anyTrue(...values: readonly unknown[]): boolean;
|
|
216
|
+
/**
|
|
217
|
+
* Returns the result of the callback if any condition is truthy.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* const result = orThen(
|
|
221
|
+
* [isAdmin, isModerator],
|
|
222
|
+
* () => showModTools()
|
|
223
|
+
* )
|
|
224
|
+
*/
|
|
225
|
+
declare function orThen<T>(conditions: readonly unknown[], fn: () => T): T | undefined;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Negates a value.
|
|
229
|
+
* Replaces: `!value`
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* const isInactive = not(user.isActive)
|
|
233
|
+
*
|
|
234
|
+
* // Useful in functional composition
|
|
235
|
+
* users.filter(not(isAdmin))
|
|
236
|
+
*/
|
|
237
|
+
declare function not<T>(value: T): boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Creates a negated predicate function.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* const isNotEmpty = notFn(isEmpty)
|
|
243
|
+
* const validItems = items.filter(isNotEmpty)
|
|
244
|
+
*/
|
|
245
|
+
declare function notFn<T extends readonly unknown[]>(predicate: (...args: T) => boolean): (...args: T) => boolean;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Returns the first non-nullish value (not null or undefined).
|
|
249
|
+
* Replaces: `a ?? b ?? c`
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* const value = coalesce(config.timeout, defaultConfig.timeout, 5000)
|
|
253
|
+
* // Instead of: config.timeout ?? defaultConfig.timeout ?? 5000
|
|
254
|
+
*
|
|
255
|
+
* // Handles 0 and '' correctly (unlike ||)
|
|
256
|
+
* coalesce(0, 10) // 0
|
|
257
|
+
* coalesce('', 'default') // ''
|
|
258
|
+
*/
|
|
259
|
+
declare function coalesce<T>(...values: readonly (T | null | undefined)[]): T | undefined;
|
|
260
|
+
/**
|
|
261
|
+
* Lazy version - evaluates functions only if needed.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* const config = coalesceLazy(
|
|
265
|
+
* () => getUserConfig(),
|
|
266
|
+
* () => getDefaultConfig(),
|
|
267
|
+
* () => ({ timeout: 5000 })
|
|
268
|
+
* )
|
|
269
|
+
*/
|
|
270
|
+
declare function coalesceLazy<T>(...fns: readonly (() => T | null | undefined)[]): T | undefined;
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Returns the value if it's not nullish, otherwise returns the default.
|
|
274
|
+
* Replaces: `value ?? defaultValue`
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* const timeout = defaultTo(config.timeout, 5000)
|
|
278
|
+
* // Instead of: config.timeout ?? 5000
|
|
279
|
+
*/
|
|
280
|
+
declare function defaultTo<T, D>(value: T | null | undefined, defaultValue: D): T | D;
|
|
281
|
+
/**
|
|
282
|
+
* Lazy version - evaluates default only if needed.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* const user = defaultToLazy(cachedUser, () => fetchUser())
|
|
286
|
+
*/
|
|
287
|
+
declare function defaultToLazy<T, D>(value: T | null | undefined, getDefault: () => D): T | D;
|
|
288
|
+
/**
|
|
289
|
+
* Returns the value if it passes the predicate, otherwise returns the default.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* const validAge = defaultToIf(age, n => n >= 0 && n <= 150, 0)
|
|
293
|
+
*/
|
|
294
|
+
declare function defaultToIf<T, D>(value: T, predicate: (v: T) => boolean, defaultValue: D): T | D;
|
|
295
|
+
/**
|
|
296
|
+
* Returns the value if it's not empty (null, undefined, '', [], {}), otherwise returns the default.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* const name = defaultToIfEmpty(user.name, 'Anonymous')
|
|
300
|
+
*/
|
|
301
|
+
declare function defaultToIfEmpty<T, D>(value: T | null | undefined, defaultValue: D): T | D;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Returns the first truthy value from an array.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* const source = firstTruthy([null, '', 0, 'hello', 'world'])
|
|
308
|
+
* // 'hello'
|
|
309
|
+
*/
|
|
310
|
+
declare function firstTruthy<T>(values: readonly T[]): T | undefined;
|
|
311
|
+
/**
|
|
312
|
+
* Returns the first value that passes the predicate.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* const validEmail = firstWhere(
|
|
316
|
+
* [user.email, user.backupEmail, 'default@example.com'],
|
|
317
|
+
* isValidEmail
|
|
318
|
+
* )
|
|
319
|
+
*/
|
|
320
|
+
declare function firstWhere<T>(values: readonly T[], predicate: (value: T) => boolean): T | undefined;
|
|
321
|
+
/**
|
|
322
|
+
* Returns the first non-nullish value from an array.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* const value = firstDefined([null, undefined, 0, '', false])
|
|
326
|
+
* // 0 (first non-nullish value)
|
|
327
|
+
*/
|
|
328
|
+
declare function firstDefined<T>(values: readonly (T | null | undefined)[]): T | undefined;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Returns true if all values are truthy.
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* if (allTruthy([user, user.isActive, user.hasPermission])) {
|
|
335
|
+
* // proceed
|
|
336
|
+
* }
|
|
337
|
+
*/
|
|
338
|
+
declare function allTruthy(values: readonly unknown[]): boolean;
|
|
339
|
+
/**
|
|
340
|
+
* Returns true if all values pass the predicate.
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* const allValid = allWhere([email1, email2, email3], isValidEmail)
|
|
344
|
+
*/
|
|
345
|
+
declare function allWhere<T>(values: readonly T[], predicate: (value: T) => boolean): boolean;
|
|
346
|
+
/**
|
|
347
|
+
* Returns true if all values are defined (not null or undefined).
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* if (allDefined([user, user.profile, user.profile.settings])) {
|
|
351
|
+
* // safe to access user.profile.settings
|
|
352
|
+
* }
|
|
353
|
+
*/
|
|
354
|
+
declare function allDefined(values: readonly unknown[]): boolean;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Safely gets a nested property value using a path.
|
|
358
|
+
* Replaces: `obj?.a?.b?.c`
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* const city = get(user, 'address', 'city')
|
|
362
|
+
* // Instead of: user?.address?.city
|
|
363
|
+
*
|
|
364
|
+
* const firstItem = get(data, 'items', 0, 'name')
|
|
365
|
+
* // Instead of: data?.items?.[0]?.name
|
|
366
|
+
*/
|
|
367
|
+
declare function get<T>(obj: unknown, ...path: readonly PropertyKey[]): T | undefined;
|
|
368
|
+
/**
|
|
369
|
+
* Type-safe get for known object structures.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* interface User { profile: { name: string } }
|
|
373
|
+
* const name = getTyped<User, string>(user, ['profile', 'name'])
|
|
374
|
+
*/
|
|
375
|
+
declare function getTyped<T, R>(obj: T, path: readonly PropertyKey[]): R | undefined;
|
|
376
|
+
/**
|
|
377
|
+
* Gets a value using a dot-notation path string.
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* const city = getPath(user, 'address.city')
|
|
381
|
+
* const item = getPath(data, 'items.0.name')
|
|
382
|
+
*/
|
|
383
|
+
declare function getPath<T>(obj: unknown, path: string): T | undefined;
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Safely gets a nested property value, returning a default if not found.
|
|
387
|
+
* Replaces: `obj?.a?.b?.c ?? defaultValue`
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* const city = getOr(user, 'Unknown', 'address', 'city')
|
|
391
|
+
* // Instead of: user?.address?.city ?? 'Unknown'
|
|
392
|
+
*/
|
|
393
|
+
declare function getOr<T>(obj: unknown, defaultValue: T, ...path: readonly PropertyKey[]): T;
|
|
394
|
+
/**
|
|
395
|
+
* Gets a value using dot-notation path, with default.
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* const city = getPathOr(user, 'Unknown', 'address.city')
|
|
399
|
+
*/
|
|
400
|
+
declare function getPathOr<T>(obj: unknown, defaultValue: T, path: string): T;
|
|
401
|
+
/**
|
|
402
|
+
* Lazy version - computes default only if needed.
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* const config = getOrLazy(settings, () => computeDefault(), 'theme', 'colors')
|
|
406
|
+
*/
|
|
407
|
+
declare function getOrLazy<T>(obj: unknown, getDefault: () => T, ...path: readonly PropertyKey[]): T;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Tries to get a value, catching any errors.
|
|
411
|
+
* Useful for accessing properties that might throw.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* const value = tryGet(() => JSON.parse(str).data.value)
|
|
415
|
+
* // Instead of: try { JSON.parse(str).data.value } catch { undefined }
|
|
416
|
+
*/
|
|
417
|
+
declare function tryGet<T>(fn: () => T): T | undefined;
|
|
418
|
+
/**
|
|
419
|
+
* Tries to get a value, returning default on error.
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* const config = tryGetOr(() => JSON.parse(str), defaultConfig)
|
|
423
|
+
*/
|
|
424
|
+
declare function tryGetOr<T>(fn: () => T, defaultValue: T): T;
|
|
425
|
+
/**
|
|
426
|
+
* Tries to get a value, returning Result type.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* const result = tryGetResult(() => riskyOperation())
|
|
430
|
+
* if (result.ok) {
|
|
431
|
+
* console.log(result.value)
|
|
432
|
+
* } else {
|
|
433
|
+
* console.error(result.error)
|
|
434
|
+
* }
|
|
435
|
+
*/
|
|
436
|
+
declare function tryGetResult<T>(fn: () => T): {
|
|
437
|
+
ok: true;
|
|
438
|
+
value: T;
|
|
439
|
+
} | {
|
|
440
|
+
ok: false;
|
|
441
|
+
error: unknown;
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Wraps an object to make all property access safe (returns undefined instead of throwing).
|
|
446
|
+
* Creates a proxy that handles undefined/null gracefully.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* const safeUser = safe(user)
|
|
450
|
+
* const city = safeUser.address.city.name // undefined if any part is missing
|
|
451
|
+
*
|
|
452
|
+
* // Instead of:
|
|
453
|
+
* const city = user?.address?.city?.name
|
|
454
|
+
*/
|
|
455
|
+
declare function safe<T extends object>(obj: T | null | undefined): SafeProxy<T>;
|
|
456
|
+
/**
|
|
457
|
+
* Unwraps a safe proxy to get the actual value (or undefined).
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* const safeUser = safe(user)
|
|
461
|
+
* const city = unwrap(safeUser.address.city) // string | undefined
|
|
462
|
+
*/
|
|
463
|
+
declare function unwrap<T>(value: T): T | undefined;
|
|
464
|
+
type SafeProxy<T> = T extends object ? {
|
|
465
|
+
[K in keyof T]: SafeProxy<T[K]>;
|
|
466
|
+
} : T | undefined;
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Chains operations on a value, stopping if any returns null/undefined.
|
|
470
|
+
* Replaces: `a?.method1()?.method2()?.method3()`
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* const result = chain(user)
|
|
474
|
+
* .map(u => u.profile)
|
|
475
|
+
* .map(p => p.settings)
|
|
476
|
+
* .map(s => s.theme)
|
|
477
|
+
* .value()
|
|
478
|
+
*
|
|
479
|
+
* // Instead of:
|
|
480
|
+
* const result = user?.profile?.settings?.theme
|
|
481
|
+
*/
|
|
482
|
+
declare function chain<T>(value: T | null | undefined): Chain<T>;
|
|
483
|
+
interface Chain<T> {
|
|
484
|
+
map<U>(fn: (v: T) => U | null | undefined): Chain<U>;
|
|
485
|
+
flatMap<U>(fn: (v: T) => Chain<U>): Chain<U>;
|
|
486
|
+
filter(predicate: (v: T) => boolean): Chain<T>;
|
|
487
|
+
tap(fn: (v: T) => void): Chain<T>;
|
|
488
|
+
value(): T | undefined;
|
|
489
|
+
valueOr<D>(defaultValue: D): T | D;
|
|
490
|
+
valueOrThrow(message?: string): T;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Calls a method on an object only if the object exists.
|
|
495
|
+
* Replaces: `obj?.method(args)`
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* optional(user, u => u.getName())
|
|
499
|
+
* optional(array, a => a.map(x => x * 2))
|
|
500
|
+
*
|
|
501
|
+
* // Instead of:
|
|
502
|
+
* user?.getName()
|
|
503
|
+
* array?.map(x => x * 2)
|
|
504
|
+
*/
|
|
505
|
+
declare function optional<T, R>(value: T | null | undefined, fn: (v: T) => R): R | undefined;
|
|
506
|
+
/**
|
|
507
|
+
* Calls a method with a default value if the object doesn't exist.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* const length = optionalOr(str, s => s.length, 0)
|
|
511
|
+
* // Instead of: str?.length ?? 0
|
|
512
|
+
*/
|
|
513
|
+
declare function optionalOr<T, R, D>(value: T | null | undefined, fn: (v: T) => R, defaultValue: D): R | D;
|
|
514
|
+
/**
|
|
515
|
+
* Applies multiple optional operations in sequence.
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* const result = optionalChain(
|
|
519
|
+
* user,
|
|
520
|
+
* u => u.profile,
|
|
521
|
+
* p => p.settings,
|
|
522
|
+
* s => s.theme
|
|
523
|
+
* )
|
|
524
|
+
* // Instead of: user?.profile?.settings?.theme
|
|
525
|
+
*/
|
|
526
|
+
declare function optionalChain<T, A>(value: T | null | undefined, fn1: (v: T) => A | null | undefined): A | undefined;
|
|
527
|
+
declare function optionalChain<T, A, B>(value: T | null | undefined, fn1: (v: T) => A | null | undefined, fn2: (v: A) => B | null | undefined): B | undefined;
|
|
528
|
+
declare function optionalChain<T, A, B, C>(value: T | null | undefined, fn1: (v: T) => A | null | undefined, fn2: (v: A) => B | null | undefined, fn3: (v: B) => C | null | undefined): C | undefined;
|
|
529
|
+
declare function optionalChain<T, A, B, C, D>(value: T | null | undefined, fn1: (v: T) => A | null | undefined, fn2: (v: A) => B | null | undefined, fn3: (v: B) => C | null | undefined, fn4: (v: C) => D | null | undefined): D | undefined;
|
|
530
|
+
|
|
531
|
+
export { type Chain, allDefined, allTrue, allTruthy, allWhere, and, andThen, anyTrue, chain, coalesce, coalesceLazy, cond, condBy, condLazy, defaultTo, defaultToIf, defaultToIfEmpty, defaultToLazy, firstDefined, firstTruthy, firstWhere, get, getOr, getOrLazy, getPath, getPathOr, getTyped, ifElse, ifElseLazy, matchLazy, matchType, matchValue, matchWithDefault, not, notFn, optional, optionalChain, optionalOr, or, orThen, safe, tryGet, tryGetOr, tryGetResult, unless, unlessValue, unwrap, when, whenValue };
|