@carbonteq/fp 0.9.1 → 0.11.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 +326 -97
- package/dist/errors.cjs +2 -3
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +2 -5
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +2 -5
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +1 -2
- package/dist/errors.mjs.map +1 -1
- package/dist/flow-experimental.cjs +332 -0
- package/dist/flow-experimental.cjs.map +1 -0
- package/dist/flow-experimental.d.cts +69 -0
- package/dist/flow-experimental.d.cts.map +1 -0
- package/dist/flow-experimental.d.mts +69 -0
- package/dist/flow-experimental.d.mts.map +1 -0
- package/dist/flow-experimental.mjs +330 -0
- package/dist/flow-experimental.mjs.map +1 -0
- package/dist/flow.cjs +340 -0
- package/dist/flow.cjs.map +1 -0
- package/dist/flow.d.cts +77 -0
- package/dist/flow.d.cts.map +1 -0
- package/dist/flow.d.mts +77 -0
- package/dist/flow.d.mts.map +1 -0
- package/dist/flow.mjs +338 -0
- package/dist/flow.mjs.map +1 -0
- package/dist/index.cjs +29 -14
- package/dist/index.d.cts +10 -5
- package/dist/index.d.mts +10 -5
- package/dist/index.mjs +10 -5
- package/dist/match.cjs +376 -3
- package/dist/match.cjs.map +1 -1
- package/dist/match.d.cts +467 -8
- package/dist/match.d.cts.map +1 -1
- package/dist/match.d.mts +467 -8
- package/dist/match.d.mts.map +1 -1
- package/dist/match.mjs +371 -3
- package/dist/match.mjs.map +1 -1
- package/dist/option-experimental.cjs +1547 -0
- package/dist/option-experimental.cjs.map +1 -0
- package/dist/option-experimental.d.cts +1380 -0
- package/dist/option-experimental.d.cts.map +1 -0
- package/dist/option-experimental.d.mts +1380 -0
- package/dist/option-experimental.d.mts.map +1 -0
- package/dist/option-experimental.mjs +1545 -0
- package/dist/option-experimental.mjs.map +1 -0
- package/dist/option.cjs +650 -71
- package/dist/option.cjs.map +1 -1
- package/dist/option.d.cts +528 -39
- package/dist/option.d.cts.map +1 -1
- package/dist/option.d.mts +528 -39
- package/dist/option.d.mts.map +1 -1
- package/dist/option.mjs +648 -70
- package/dist/option.mjs.map +1 -1
- package/dist/result-experimental.cjs +1346 -0
- package/dist/result-experimental.cjs.map +1 -0
- package/dist/result-experimental.d.cts +1127 -0
- package/dist/result-experimental.d.cts.map +1 -0
- package/dist/result-experimental.d.mts +1127 -0
- package/dist/result-experimental.d.mts.map +1 -0
- package/dist/result-experimental.mjs +1345 -0
- package/dist/result-experimental.mjs.map +1 -0
- package/dist/result.cjs +746 -81
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +799 -104
- package/dist/result.d.cts.map +1 -1
- package/dist/result.d.mts +799 -104
- package/dist/result.d.mts.map +1 -1
- package/dist/result.mjs +742 -73
- package/dist/result.mjs.map +1 -1
- package/dist/tuple.cjs +5 -4
- package/dist/tuple.cjs.map +1 -1
- package/dist/tuple.d.cts +9 -7
- package/dist/tuple.d.cts.map +1 -1
- package/dist/tuple.d.mts +9 -7
- package/dist/tuple.d.mts.map +1 -1
- package/dist/tuple.mjs +4 -3
- package/dist/tuple.mjs.map +1 -1
- package/dist/unit.cjs +3 -3
- package/dist/unit.cjs.map +1 -1
- package/dist/unit.d.cts +4 -5
- package/dist/unit.d.cts.map +1 -1
- package/dist/unit.d.mts +4 -5
- package/dist/unit.d.mts.map +1 -1
- package/dist/unit.mjs +2 -2
- package/dist/unit.mjs.map +1 -1
- package/dist/utils.cjs +50 -3
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +14 -4
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +14 -4
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +46 -3
- package/dist/utils.mjs.map +1 -1
- package/examples/README.md +60 -0
- package/examples/flow/01-basics.ts +74 -0
- package/examples/flow/02-async.ts +73 -0
- package/examples/flow/03-recovery.ts +60 -0
- package/examples/flow/04-complex-backend.ts +240 -0
- package/examples/flow/05-complex-backend-asyncGen.ts +224 -0
- package/examples/flow/index.ts +13 -0
- package/examples/flow-experimental/01-basics.ts +78 -0
- package/examples/flow-experimental/02-async.ts +77 -0
- package/examples/flow-experimental/03-recovery.ts +93 -0
- package/examples/flow-experimental/04-complex-backend.ts +244 -0
- package/examples/flow-experimental/05-complex-backend-asyncGen.ts +229 -0
- package/examples/flow-experimental/index.ts +14 -0
- package/examples/match/01-stable-match.ts +154 -0
- package/examples/match/index.ts +19 -0
- package/examples/match-experimental/01-experimental-match.ts +157 -0
- package/examples/match-experimental/index.ts +18 -0
- package/examples/option/01-basics.ts +16 -0
- package/examples/option/02-map-flatMap.ts +36 -0
- package/examples/option/03-zip-flatZip.ts +23 -0
- package/examples/option/04-constructors.ts +21 -0
- package/examples/option/05-transform.ts +15 -0
- package/examples/option/06-aggregation.ts +14 -0
- package/examples/option/index.ts +49 -0
- package/examples/option-experimental/01-map.ts +109 -0
- package/examples/option-experimental/02-flatMap.ts +234 -0
- package/examples/option-experimental/03-zip.ts +197 -0
- package/examples/option-experimental/04-flatZip.ts +251 -0
- package/examples/option-experimental/05-gen.ts +360 -0
- package/examples/option-experimental/06-genAdapter.ts +463 -0
- package/examples/option-experimental/07-asyncGen.ts +353 -0
- package/examples/option-experimental/08-asyncGenAdapter.ts +514 -0
- package/examples/option-experimental/09-workflow-simple-sync.ts +321 -0
- package/examples/option-experimental/10-workflow-async.ts +378 -0
- package/examples/option-experimental/index.ts +341 -0
- package/examples/option-experimental/shared-domain.ts +325 -0
- package/examples/result/01-basics.ts +16 -0
- package/examples/result/02-map-flatMap.ts +34 -0
- package/examples/result/03-zip-flatZip.ts +23 -0
- package/examples/result/04-validation.ts +44 -0
- package/examples/result/05-constructors.ts +30 -0
- package/examples/result/06-utilities.ts +36 -0
- package/examples/result/index.ts +53 -0
- package/examples/result-experimental/01-map.ts +60 -0
- package/examples/result-experimental/02-flatMap.ts +169 -0
- package/examples/result-experimental/03-zip.ts +145 -0
- package/examples/result-experimental/04-flatZip.ts +252 -0
- package/examples/result-experimental/05-gen.ts +293 -0
- package/examples/result-experimental/06-genAdapter.ts +396 -0
- package/examples/result-experimental/07-asyncGen.ts +379 -0
- package/examples/result-experimental/08-asyncGenAdapter.ts +553 -0
- package/examples/result-experimental/09-workflow-simple-sync.ts +304 -0
- package/examples/result-experimental/10-workflow-async.ts +356 -0
- package/examples/result-experimental/index.ts +312 -0
- package/examples/result-experimental/shared-domain.ts +263 -0
- package/package.json +54 -31
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ExperimentalResult Examples Index
|
|
3
|
+
*
|
|
4
|
+
* This file lists all available examples and demonstrates different
|
|
5
|
+
* patterns for working with ExperimentalResult types.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: These examples are experimental-only and may change.
|
|
8
|
+
* For stable Result examples, see README.md.
|
|
9
|
+
*
|
|
10
|
+
* Run examples with:
|
|
11
|
+
* bun run examples/result-experimental/01-map.ts
|
|
12
|
+
* bun run examples/result-experimental/02-flatMap.ts
|
|
13
|
+
* etc.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// BASIC OPERATIONS (files 01-04)
|
|
18
|
+
// ============================================================================
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 01-map.ts - Transform success values
|
|
22
|
+
*
|
|
23
|
+
* Topics covered:
|
|
24
|
+
* - Basic map usage
|
|
25
|
+
* - Map on Err (mapper not called)
|
|
26
|
+
* - Chaining multiple maps
|
|
27
|
+
* - Map with object transformation
|
|
28
|
+
* - Map with async functions (returns Result<Promise<T>, E>)
|
|
29
|
+
* - Map with array operations
|
|
30
|
+
* - Type transformations
|
|
31
|
+
*
|
|
32
|
+
* Run: bun run examples/result-experimental/01-map.ts
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 02-flatMap.ts - Chain Result-returning functions
|
|
37
|
+
*
|
|
38
|
+
* Topics covered:
|
|
39
|
+
* - Basic flatMap chains
|
|
40
|
+
* - Short-circuit on errors
|
|
41
|
+
* - Chaining multiple flatMaps
|
|
42
|
+
* - Dependent lookups (user → posts)
|
|
43
|
+
* - Error type unions
|
|
44
|
+
* - Async flatMap
|
|
45
|
+
* - Validation pipelines
|
|
46
|
+
* - flatMap vs map comparison
|
|
47
|
+
* - Real-world user registration flow
|
|
48
|
+
*
|
|
49
|
+
* Run: bun run examples/result-experimental/02-flatMap.ts
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 03-zip.ts - Pair original with derived value
|
|
54
|
+
*
|
|
55
|
+
* Topics covered:
|
|
56
|
+
* - Basic zip usage
|
|
57
|
+
* - Zip on Err
|
|
58
|
+
* - Async zip
|
|
59
|
+
* - Validation with context
|
|
60
|
+
* - Multiple zips
|
|
61
|
+
* - Audit trail patterns
|
|
62
|
+
* - Before/after comparisons
|
|
63
|
+
* - Error context preservation
|
|
64
|
+
* - Transformation history
|
|
65
|
+
* - Async API call patterns
|
|
66
|
+
*
|
|
67
|
+
* Run: bun run examples/result-experimental/03-zip.ts
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 04-flatZip.ts - Pair original with Result-returning function
|
|
72
|
+
*
|
|
73
|
+
* Topics covered:
|
|
74
|
+
* - Basic flatZip usage
|
|
75
|
+
* - Short-circuit on errors
|
|
76
|
+
* - Chaining flatZips
|
|
77
|
+
* - Async flatZip
|
|
78
|
+
* - User and posts pattern
|
|
79
|
+
* - Dependent lookups
|
|
80
|
+
* - Validation with context
|
|
81
|
+
* - Complex object building
|
|
82
|
+
* - Sequential API calls
|
|
83
|
+
* - flatZip vs flatMap comparison
|
|
84
|
+
* - Audit log patterns
|
|
85
|
+
* - Context preservation
|
|
86
|
+
* - Multi-step validation
|
|
87
|
+
*
|
|
88
|
+
* Run: bun run examples/result-experimental/04-flatZip.ts
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
// ============================================================================
|
|
92
|
+
// GENERATOR-BASED SYNTAX (files 05-08)
|
|
93
|
+
// ============================================================================
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 05-gen.ts - Imperative-style sync Result composition
|
|
97
|
+
*
|
|
98
|
+
* Topics covered:
|
|
99
|
+
* - Simple gen with single/multiple yields
|
|
100
|
+
* - Short-circuit on errors
|
|
101
|
+
* - Function calls in gen
|
|
102
|
+
* - Validation pipelines
|
|
103
|
+
* - gen vs flatMap readability
|
|
104
|
+
* - Complex object building
|
|
105
|
+
* - Intermediate processing
|
|
106
|
+
* - Array operations
|
|
107
|
+
* - Dependent operations
|
|
108
|
+
* - No yields case
|
|
109
|
+
* - Error context collection
|
|
110
|
+
* - Stack overflow prevention (many yields)
|
|
111
|
+
* - Conditional logic
|
|
112
|
+
* - Type information preservation
|
|
113
|
+
* - Error recovery with orElse
|
|
114
|
+
* - Sequential API patterns
|
|
115
|
+
* - Accumulator patterns
|
|
116
|
+
*
|
|
117
|
+
* Run: bun run examples/result-experimental/05-gen.ts
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 06-genAdapter.ts - Generator with adapter for better type inference
|
|
122
|
+
*
|
|
123
|
+
* Topics covered:
|
|
124
|
+
* - Adapter function ($) usage
|
|
125
|
+
* - Improved type inference with different error types
|
|
126
|
+
* - Rich validation error types
|
|
127
|
+
* - Method calls on objects
|
|
128
|
+
* - API composition
|
|
129
|
+
* - Conditional logic
|
|
130
|
+
* - Parallel-like operations (sequential but clean)
|
|
131
|
+
* - Error type narrowing
|
|
132
|
+
* - Complex state building
|
|
133
|
+
* - Try-catch patterns (safeParseInt, safeDivide)
|
|
134
|
+
* - Array iteration
|
|
135
|
+
* - Multi-step validation
|
|
136
|
+
* - Nested genAdapter calls
|
|
137
|
+
* - Accumulator patterns
|
|
138
|
+
* - Transaction-like operations
|
|
139
|
+
* - gen vs genAdapter comparison
|
|
140
|
+
*
|
|
141
|
+
* Run: bun run examples/result-experimental/06-genAdapter.ts
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 07-asyncGen.ts - Imperative-style async Result composition
|
|
146
|
+
*
|
|
147
|
+
* Topics covered:
|
|
148
|
+
* - Simple asyncGen with sync/async Results
|
|
149
|
+
* - Multiple yields with mixed sync/async
|
|
150
|
+
* - Short-circuit on errors
|
|
151
|
+
* - Async operations with delays
|
|
152
|
+
* - Fetching data patterns (user → posts)
|
|
153
|
+
* - Auto-awaiting Result<Promise<T>, E>
|
|
154
|
+
* - Async validation pipelines
|
|
155
|
+
* - Error recovery
|
|
156
|
+
* - Sequential API calls
|
|
157
|
+
* - Async map chains
|
|
158
|
+
* - Complex workflows (order → payment → receipt)
|
|
159
|
+
* - toPromise conversion
|
|
160
|
+
* - Retry-like patterns
|
|
161
|
+
* - Array processing
|
|
162
|
+
* - Conditional async operations
|
|
163
|
+
* - Batch operations
|
|
164
|
+
*
|
|
165
|
+
* Run: bun run examples/result-experimental/07-asyncGen.ts
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 08-asyncGenAdapter.ts - Async generator with adapter
|
|
170
|
+
*
|
|
171
|
+
* Topics covered:
|
|
172
|
+
* - Adapter function ($) in async context
|
|
173
|
+
* - Promise<Result<T,E>> handling
|
|
174
|
+
* - Improved type inference
|
|
175
|
+
* - API composition
|
|
176
|
+
* - Auto-awaiting Result<Promise<T>, E>
|
|
177
|
+
* - Complex async validation
|
|
178
|
+
* - Transaction workflows
|
|
179
|
+
* - Retry logic with fallbacks
|
|
180
|
+
* - Data processing pipelines
|
|
181
|
+
* - Array operations
|
|
182
|
+
* - Conditional logic
|
|
183
|
+
* - Batch operations
|
|
184
|
+
* - Parallel-like patterns (sequential)
|
|
185
|
+
* - toPromise usage
|
|
186
|
+
* - Complex workflows (order → payment → shipment → invoice)
|
|
187
|
+
* - Error aggregation
|
|
188
|
+
* - Nested operations
|
|
189
|
+
* - Streaming-like patterns
|
|
190
|
+
*
|
|
191
|
+
* Run: bun run examples/result-experimental/08-asyncGenAdapter.ts
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
// ============================================================================
|
|
195
|
+
// WORKFLOW EXAMPLES (files 09-10)
|
|
196
|
+
// ============================================================================
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 09-workflow-simple-sync.ts - Real-world sync workflow
|
|
200
|
+
*
|
|
201
|
+
* Domain: Grocery list management
|
|
202
|
+
*
|
|
203
|
+
* Comparisons shown:
|
|
204
|
+
* - flatMap chain style
|
|
205
|
+
* - Result.gen style
|
|
206
|
+
* - Result.genAdapter style
|
|
207
|
+
*
|
|
208
|
+
* Workflow steps:
|
|
209
|
+
* - Ensure ownership
|
|
210
|
+
* - Validate list is active
|
|
211
|
+
* - Validate items
|
|
212
|
+
* - Calculate stats
|
|
213
|
+
* - Serialize data
|
|
214
|
+
*
|
|
215
|
+
* Also covers:
|
|
216
|
+
* - Multi-step validation comparison
|
|
217
|
+
* - Fetching and chaining
|
|
218
|
+
* - Readability comparison
|
|
219
|
+
*
|
|
220
|
+
* Run: bun run examples/result-experimental/09-workflow-simple-sync.ts
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 10-workflow-async.ts - Real-world async workflow
|
|
225
|
+
*
|
|
226
|
+
* Domain: Grocery list management with async operations
|
|
227
|
+
*
|
|
228
|
+
* Styles shown:
|
|
229
|
+
* - Result.asyncGen
|
|
230
|
+
* - Result.asyncGenAdapter
|
|
231
|
+
* - Auto-awaiting Result<Promise<T>, E>
|
|
232
|
+
* - Complex multi-step async validation
|
|
233
|
+
*
|
|
234
|
+
* Workflow steps:
|
|
235
|
+
* - Fetch list and owner (async)
|
|
236
|
+
* - Ensure ownership
|
|
237
|
+
* - Async permission validation
|
|
238
|
+
* - Validate list is active
|
|
239
|
+
* - Fetch items (async)
|
|
240
|
+
* - Validate items
|
|
241
|
+
* - Serialize (async)
|
|
242
|
+
* - Calculate stats (async)
|
|
243
|
+
*
|
|
244
|
+
* Also covers:
|
|
245
|
+
* - Error handling comparison
|
|
246
|
+
* - Sequential fetch patterns
|
|
247
|
+
* - toPromise conversion
|
|
248
|
+
* - Readability comparison
|
|
249
|
+
*
|
|
250
|
+
* Run: bun run examples/result-experimental/10-workflow-async.ts
|
|
251
|
+
*/
|
|
252
|
+
|
|
253
|
+
// ============================================================================
|
|
254
|
+
// SHARED DOMAIN (for workflow examples)
|
|
255
|
+
// ============================================================================
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* shared-domain.ts - Domain models used in workflow examples
|
|
259
|
+
*
|
|
260
|
+
* Contains:
|
|
261
|
+
* - UserEntity, ItemEntity, GroceryListEntity classes
|
|
262
|
+
* - Error types (GroceryListOwnershipError, ValidationError)
|
|
263
|
+
* - Helper functions (calculateDetailedStats)
|
|
264
|
+
* - Mock data
|
|
265
|
+
* - Repository methods (sync and async)
|
|
266
|
+
*
|
|
267
|
+
* Imported by: 09-workflow-simple-sync.ts, 10-workflow-async.ts
|
|
268
|
+
*/
|
|
269
|
+
|
|
270
|
+
// ============================================================================
|
|
271
|
+
// QUICK REFERENCE
|
|
272
|
+
// ============================================================================
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* WHEN TO USE EACH METHOD:
|
|
276
|
+
*
|
|
277
|
+
* map():
|
|
278
|
+
* - Transform success value
|
|
279
|
+
* - Error bypasses mapper
|
|
280
|
+
*
|
|
281
|
+
* flatMap():
|
|
282
|
+
* - Chain Result-returning functions
|
|
283
|
+
* - Stop on first error
|
|
284
|
+
*
|
|
285
|
+
* zip():
|
|
286
|
+
* - Keep original value along with transformed value
|
|
287
|
+
* - Returns [original, derived] tuple
|
|
288
|
+
*
|
|
289
|
+
* flatZip():
|
|
290
|
+
* - Like zip, but function returns Result
|
|
291
|
+
* - Stop on first error
|
|
292
|
+
*
|
|
293
|
+
* gen():
|
|
294
|
+
* - Imperative-style sync code
|
|
295
|
+
* - Multiple yields with automatic threading
|
|
296
|
+
* - Short-circuits on errors
|
|
297
|
+
*
|
|
298
|
+
* genAdapter():
|
|
299
|
+
* - Same as gen, with adapter function ($)
|
|
300
|
+
* - Better type inference for complex errors
|
|
301
|
+
*
|
|
302
|
+
* asyncGen():
|
|
303
|
+
* - Imperative-style async code
|
|
304
|
+
* - Handles Promise<Result<T,E>> and Result<Promise<T>,E>
|
|
305
|
+
* - Auto-awaits inner promises
|
|
306
|
+
*
|
|
307
|
+
* asyncGenAdapter():
|
|
308
|
+
* - Same as asyncGen, with adapter ($)
|
|
309
|
+
* - Best type inference for async workflows
|
|
310
|
+
*/
|
|
311
|
+
|
|
312
|
+
export {}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared domain models for workflow examples
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ExperimentalResult as Result } from "../../dist/result-experimental.mjs"
|
|
6
|
+
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// DOMAIN MODELS
|
|
9
|
+
// ============================================================================
|
|
10
|
+
|
|
11
|
+
type UserId = string
|
|
12
|
+
type GroceryListId = string
|
|
13
|
+
type ItemId = string
|
|
14
|
+
|
|
15
|
+
// User Entity
|
|
16
|
+
export class UserEntity {
|
|
17
|
+
constructor(
|
|
18
|
+
readonly id: UserId,
|
|
19
|
+
readonly name: string,
|
|
20
|
+
readonly email: string,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
serialize() {
|
|
24
|
+
return {
|
|
25
|
+
id: this.id,
|
|
26
|
+
name: this.name,
|
|
27
|
+
email: this.email,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Item Entity
|
|
33
|
+
export class ItemEntity {
|
|
34
|
+
constructor(
|
|
35
|
+
readonly id: ItemId,
|
|
36
|
+
readonly name: string,
|
|
37
|
+
readonly quantity: number,
|
|
38
|
+
readonly isBought: boolean,
|
|
39
|
+
) {}
|
|
40
|
+
|
|
41
|
+
serialize() {
|
|
42
|
+
return {
|
|
43
|
+
id: this.id,
|
|
44
|
+
name: this.name,
|
|
45
|
+
quantity: this.quantity,
|
|
46
|
+
isBought: this.isBought,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Grocery List Entity
|
|
52
|
+
export class GroceryListEntity {
|
|
53
|
+
constructor(
|
|
54
|
+
readonly id: GroceryListId,
|
|
55
|
+
readonly name: string,
|
|
56
|
+
readonly description: string,
|
|
57
|
+
readonly active: boolean,
|
|
58
|
+
readonly ownerId: UserId,
|
|
59
|
+
) {}
|
|
60
|
+
|
|
61
|
+
serialize() {
|
|
62
|
+
return {
|
|
63
|
+
id: this.id,
|
|
64
|
+
name: this.name,
|
|
65
|
+
description: this.description,
|
|
66
|
+
active: this.active,
|
|
67
|
+
ownerId: this.ownerId,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ensureIsOwner(user: UserEntity): Result<this, GroceryListOwnershipError> {
|
|
72
|
+
if (this.ownerId !== user.id) {
|
|
73
|
+
return Result.Err(new GroceryListOwnershipError(this.id))
|
|
74
|
+
}
|
|
75
|
+
return Result.Ok(this)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Stats
|
|
80
|
+
export interface GroceryListStats {
|
|
81
|
+
totalItems: number
|
|
82
|
+
pendingItems: number
|
|
83
|
+
completedItems: number
|
|
84
|
+
completionPercentage: number
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Details output
|
|
88
|
+
export interface GroceryListDetails {
|
|
89
|
+
id: GroceryListId
|
|
90
|
+
name: string
|
|
91
|
+
description: string
|
|
92
|
+
active: boolean
|
|
93
|
+
owner: ReturnType<UserEntity["serialize"]>
|
|
94
|
+
items: ReturnType<ItemEntity["serialize"]>[]
|
|
95
|
+
stats: GroceryListStats
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Error types
|
|
99
|
+
export class GroceryListOwnershipError {
|
|
100
|
+
readonly _tag = "GroceryListOwnershipError"
|
|
101
|
+
constructor(readonly listId: GroceryListId) {}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export class ValidationError {
|
|
105
|
+
readonly _tag = "ValidationError"
|
|
106
|
+
constructor(readonly message: string) {}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export type GroceryListError = GroceryListOwnershipError | ValidationError
|
|
110
|
+
|
|
111
|
+
// ============================================================================
|
|
112
|
+
// HELPER FUNCTIONS
|
|
113
|
+
// ============================================================================
|
|
114
|
+
|
|
115
|
+
export function calculateDetailedStats(items: ItemEntity[]): GroceryListStats {
|
|
116
|
+
const totalItems = items.length
|
|
117
|
+
const pendingItems = items.filter((item) => !item.isBought).length
|
|
118
|
+
const completedItems = items.filter((item) => item.isBought).length
|
|
119
|
+
|
|
120
|
+
const completionPercentage =
|
|
121
|
+
totalItems > 0 ? (completedItems / totalItems) * 100 : 0
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
totalItems,
|
|
125
|
+
pendingItems,
|
|
126
|
+
completedItems,
|
|
127
|
+
completionPercentage: Math.round(completionPercentage * 100) / 100,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ============================================================================
|
|
132
|
+
// MOCK DATA
|
|
133
|
+
// ============================================================================
|
|
134
|
+
|
|
135
|
+
export const mockUser = new UserEntity("user-123", "Alice", "alice@example.com")
|
|
136
|
+
|
|
137
|
+
export const mockDifferentUser = new UserEntity(
|
|
138
|
+
"user-999",
|
|
139
|
+
"Bob",
|
|
140
|
+
"bob@example.com",
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
export const mockGroceryList = new GroceryListEntity(
|
|
144
|
+
"list-456",
|
|
145
|
+
"Weekly Shopping",
|
|
146
|
+
"Weekly grocery list",
|
|
147
|
+
true,
|
|
148
|
+
"user-123",
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
export const mockItems: ItemEntity[] = [
|
|
152
|
+
new ItemEntity("item-1", "Milk", 2, true),
|
|
153
|
+
new ItemEntity("item-2", "Eggs", 12, false),
|
|
154
|
+
new ItemEntity("item-3", "Bread", 1, false),
|
|
155
|
+
]
|
|
156
|
+
|
|
157
|
+
// ============================================================================
|
|
158
|
+
// REPOSITORY/WORKFLOW METHODS
|
|
159
|
+
// ============================================================================
|
|
160
|
+
|
|
161
|
+
// SYNC methods returning Result
|
|
162
|
+
export function fetchUserById(id: UserId): Result<UserEntity, ValidationError> {
|
|
163
|
+
if (id === "user-123") {
|
|
164
|
+
return Result.Ok(mockUser)
|
|
165
|
+
}
|
|
166
|
+
return Result.Err(new ValidationError(`User not found: ${id}`))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function ensureListIsActive(
|
|
170
|
+
list: GroceryListEntity,
|
|
171
|
+
): Result<GroceryListEntity, ValidationError> {
|
|
172
|
+
if (list.active) {
|
|
173
|
+
return Result.Ok(list)
|
|
174
|
+
}
|
|
175
|
+
return Result.Err(new ValidationError("List is not active"))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function validateItems(
|
|
179
|
+
items: ItemEntity[],
|
|
180
|
+
): Result<ItemEntity[], ValidationError> {
|
|
181
|
+
if (items.length === 0) {
|
|
182
|
+
return Result.Err(new ValidationError("Items cannot be empty"))
|
|
183
|
+
}
|
|
184
|
+
const hasInvalidQuantity = items.some((item) => item.quantity <= 0)
|
|
185
|
+
if (hasInvalidQuantity) {
|
|
186
|
+
return Result.Err(
|
|
187
|
+
new ValidationError("All items must have positive quantity"),
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
return Result.Ok(items)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function filterActiveItems(
|
|
194
|
+
items: ItemEntity[],
|
|
195
|
+
): Result<ItemEntity[], ValidationError> {
|
|
196
|
+
const filtered = items.filter((item) => !item.isBought)
|
|
197
|
+
if (filtered.length === 0) {
|
|
198
|
+
return Result.Err(new ValidationError("No active items found"))
|
|
199
|
+
}
|
|
200
|
+
return Result.Ok(filtered)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ASYNC methods returning Promise<Result>
|
|
204
|
+
export async function fetchUserByIdAsync(
|
|
205
|
+
id: UserId,
|
|
206
|
+
): Promise<Result<UserEntity, ValidationError>> {
|
|
207
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
208
|
+
return fetchUserById(id)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export async function fetchItemsForListAsync(
|
|
212
|
+
listId: GroceryListId,
|
|
213
|
+
): Promise<Result<ItemEntity[], ValidationError>> {
|
|
214
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
215
|
+
if (listId === "list-456") {
|
|
216
|
+
return Result.Ok(mockItems)
|
|
217
|
+
}
|
|
218
|
+
return Result.Err(new ValidationError(`Items not found for list: ${listId}`))
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ASYNC methods returning Promise (for use as mappers)
|
|
222
|
+
export async function serializeItemsAsync(
|
|
223
|
+
items: ItemEntity[],
|
|
224
|
+
): Promise<ReturnType<ItemEntity["serialize"]>[]> {
|
|
225
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
226
|
+
return items.map((item) => item.serialize())
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export async function calculateDetailedStatsAsync(
|
|
230
|
+
items: ItemEntity[],
|
|
231
|
+
): Promise<GroceryListStats> {
|
|
232
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
233
|
+
return calculateDetailedStats(items)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function validateUserPermissionsAsync(
|
|
237
|
+
user: UserEntity,
|
|
238
|
+
list: GroceryListEntity,
|
|
239
|
+
): Promise<Result<UserEntity, ValidationError>> {
|
|
240
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
241
|
+
if (user.id === list.ownerId) {
|
|
242
|
+
return Result.Ok(user)
|
|
243
|
+
}
|
|
244
|
+
return Result.Err(new ValidationError("User lacks permission"))
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export async function fetchListWithOwnerAsync(
|
|
248
|
+
listId: GroceryListId,
|
|
249
|
+
userId: UserId,
|
|
250
|
+
): Promise<
|
|
251
|
+
Result<{ list: GroceryListEntity; owner: UserEntity }, ValidationError>
|
|
252
|
+
> {
|
|
253
|
+
const owner = await fetchUserByIdAsync(userId)
|
|
254
|
+
if (owner.isErr()) {
|
|
255
|
+
return Result.Err(owner.unwrapErr())
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (listId !== "list-456") {
|
|
259
|
+
return Result.Err(new ValidationError(`List not found: ${listId}`))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return Result.Ok({ list: mockGroceryList, owner: owner.unwrap() })
|
|
263
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carbonteq/fp",
|
|
3
3
|
"description": "FP utils",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.11.1",
|
|
5
5
|
"author": "Arslan <rslnkrmt2552@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scope": "@carbonteq",
|
|
@@ -13,10 +13,7 @@
|
|
|
13
13
|
"url": "https://github.com/carbonteq/fp/issues"
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
|
-
"files": [
|
|
17
|
-
"/dist",
|
|
18
|
-
"dist"
|
|
19
|
-
],
|
|
16
|
+
"files": ["dist", "README.md", "examples"],
|
|
20
17
|
"main": "./dist/index.cjs",
|
|
21
18
|
"module": "./dist/index.js",
|
|
22
19
|
"types": "./dist/index.d.ts",
|
|
@@ -70,6 +67,36 @@
|
|
|
70
67
|
"types": "./dist/errors.d.mts",
|
|
71
68
|
"default": "./dist/errors.mjs"
|
|
72
69
|
}
|
|
70
|
+
},
|
|
71
|
+
"./flow": {
|
|
72
|
+
"require": {
|
|
73
|
+
"types": "./dist/flow.d.cts",
|
|
74
|
+
"default": "./dist/flow.cjs"
|
|
75
|
+
},
|
|
76
|
+
"import": {
|
|
77
|
+
"types": "./dist/flow.d.mts",
|
|
78
|
+
"default": "./dist/flow.mjs"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"./result-experimental": {
|
|
82
|
+
"require": {
|
|
83
|
+
"types": "./dist/result-experimental.d.cts",
|
|
84
|
+
"default": "./dist/result-experimental.cjs"
|
|
85
|
+
},
|
|
86
|
+
"import": {
|
|
87
|
+
"types": "./dist/result-experimental.d.mts",
|
|
88
|
+
"default": "./dist/result-experimental.mjs"
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"./option-experimental": {
|
|
92
|
+
"require": {
|
|
93
|
+
"types": "./dist/option-experimental.d.cts",
|
|
94
|
+
"default": "./dist/option-experimental.cjs"
|
|
95
|
+
},
|
|
96
|
+
"import": {
|
|
97
|
+
"types": "./dist/option-experimental.d.mts",
|
|
98
|
+
"default": "./dist/option-experimental.mjs"
|
|
99
|
+
}
|
|
73
100
|
}
|
|
74
101
|
},
|
|
75
102
|
"homepage": "https://github.com/carbonteq/fp#readme",
|
|
@@ -80,40 +107,36 @@
|
|
|
80
107
|
"registry": "https://registry.npmjs.org/"
|
|
81
108
|
},
|
|
82
109
|
"scripts": {
|
|
83
|
-
"lint": "
|
|
84
|
-
"
|
|
85
|
-
"fmt
|
|
86
|
-
"
|
|
87
|
-
"
|
|
110
|
+
"lint": "oxlint --type-aware src tests",
|
|
111
|
+
"fmt:dry": "oxfmt --check .",
|
|
112
|
+
"fmt": "oxfmt .",
|
|
113
|
+
"tc": "bun check",
|
|
114
|
+
"typecheck": "tsrs --noEmit --pretty",
|
|
88
115
|
"build": "NODE_ENV=production tsdown",
|
|
89
116
|
"dev": "tsdown --watch",
|
|
90
117
|
"test": "bun test",
|
|
91
|
-
"test:node": "tsx --test tests/*.test.ts",
|
|
92
118
|
"test:dbg": "bun test --inspect",
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"knip": "knip"
|
|
119
|
+
"ver": "changeset version && node sync_versions.mjs && oxfmt deno.jsonc",
|
|
120
|
+
"release": "changeset git-tag"
|
|
96
121
|
},
|
|
97
122
|
"devDependencies": {
|
|
98
|
-
"@
|
|
99
|
-
"@
|
|
100
|
-
"@
|
|
101
|
-
"@
|
|
102
|
-
"@tsconfig/
|
|
103
|
-
"@
|
|
104
|
-
"@types/bun": "^1.3.5",
|
|
123
|
+
"@changesets/cli": "^3.0.3",
|
|
124
|
+
"@microsoft/api-extractor": "^7.60.1",
|
|
125
|
+
"@swc/core": "^1.16.22",
|
|
126
|
+
"@tsconfig/bun": "^1.0.12",
|
|
127
|
+
"@tsconfig/node22": "^22.0.7",
|
|
128
|
+
"@types/bun": "^1.4.5",
|
|
105
129
|
"@types/node": "^24.10.1",
|
|
106
|
-
"
|
|
107
|
-
"bunup": "^0.16.17",
|
|
130
|
+
"bunup": "^0.16.32",
|
|
108
131
|
"changesets": "^1.0.2",
|
|
109
|
-
"jsr": "^0.
|
|
110
|
-
"
|
|
111
|
-
"
|
|
112
|
-
"oxlint": "^
|
|
113
|
-
"
|
|
114
|
-
"
|
|
115
|
-
"tsx": "^4.
|
|
116
|
-
"typescript": "^
|
|
132
|
+
"jsr": "^0.14.3",
|
|
133
|
+
"oxfmt": "^0.79.0",
|
|
134
|
+
"oxlint": "^1.94.0",
|
|
135
|
+
"oxlint-tsgolint": "^7.0.2008",
|
|
136
|
+
"tsdown": "^0.23.7",
|
|
137
|
+
"tsrs": "^7.1.0-beta.tsrs4",
|
|
138
|
+
"tsx": "^4.23.15",
|
|
139
|
+
"typescript": "^7.0.2"
|
|
117
140
|
},
|
|
118
141
|
"engines": {
|
|
119
142
|
"node": ">=22.0.0"
|