functype 0.8.67 → 0.8.69

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.
Files changed (48) hide show
  1. package/README.md +136 -6
  2. package/dist/Either-BfXNbTHo.d.ts +533 -0
  3. package/dist/Map-vivbm5n0.d.ts +65 -0
  4. package/dist/{Tuple-DfdXAbL_.d.ts → Valuable-CtuVEKTZ.d.ts} +17 -10
  5. package/dist/chunk-5DWCHDSA.mjs +39 -0
  6. package/dist/chunk-5DWCHDSA.mjs.map +1 -0
  7. package/dist/chunk-7PQA3W7W.mjs +2 -0
  8. package/dist/chunk-7PQA3W7W.mjs.map +1 -0
  9. package/dist/either/index.d.ts +2 -3
  10. package/dist/either/index.mjs +1 -1
  11. package/dist/fpromise/index.d.ts +373 -3
  12. package/dist/fpromise/index.mjs +1 -1
  13. package/dist/index.d.ts +533 -2
  14. package/dist/index.mjs +1 -1
  15. package/dist/list/index.d.ts +2 -3
  16. package/dist/list/index.mjs +1 -1
  17. package/dist/map/index.d.ts +4 -3
  18. package/dist/map/index.mjs +1 -1
  19. package/dist/option/index.d.ts +2 -987
  20. package/dist/option/index.mjs +1 -1
  21. package/dist/set/index.d.ts +2 -3
  22. package/dist/set/index.mjs +1 -1
  23. package/dist/try/index.d.ts +59 -3
  24. package/dist/try/index.mjs +1 -1
  25. package/dist/tuple/index.d.ts +12 -1
  26. package/dist/tuple/index.mjs +1 -1
  27. package/package.json +17 -16
  28. package/readme/BUNDLE_OPTIMIZATION.md +74 -0
  29. package/readme/FPromise-Assessment.md +43 -0
  30. package/readme/HKT.md +110 -0
  31. package/readme/ROADMAP.md +113 -0
  32. package/readme/TASK-IMPLEMENTATION.md +290 -0
  33. package/readme/TASK-TODO.md +40 -0
  34. package/readme/TASK-UPDATES.md +64 -0
  35. package/readme/TUPLE-EXAMPLES.md +79 -0
  36. package/readme/TaskMigration.md +129 -0
  37. package/readme/ai-guide.md +406 -0
  38. package/readme/examples.md +2093 -0
  39. package/readme/quick-reference.md +514 -0
  40. package/readme/task-cancellation-progress.md +258 -0
  41. package/readme/task-error-handling.md +128 -0
  42. package/readme/task-quick-reference.md +157 -0
  43. package/readme/tasks.md +205 -0
  44. package/readme/type-index.md +238 -0
  45. package/dist/chunk-NTL4HYMA.mjs +0 -18
  46. package/dist/chunk-NTL4HYMA.mjs.map +0 -1
  47. package/dist/chunk-PXFJPCM7.mjs +0 -2
  48. package/dist/chunk-PXFJPCM7.mjs.map +0 -1
package/README.md CHANGED
@@ -25,8 +25,9 @@ Functype is a lightweight functional programming library for TypeScript, drawing
25
25
  - **Task**: Handle synchronous and asynchronous operations with error handling
26
26
  - **Tuple**: Type-safe fixed-length arrays
27
27
  - **Typeable**: Runtime type identification with compile-time safety
28
- - **Branded Types**: Nominal typing in TypeScript's structural type system
28
+ - **Branded Types**: Nominal typing in TypeScript's structural type system
29
29
  - **FPromise**: Enhanced Promise functionality with built-in error handling
30
+ - **Error Formatting**: Utilities for improved error visualization and logging
30
31
 
31
32
  ## Installation
32
33
 
@@ -215,7 +216,9 @@ const Email = (email: string): Email => {
215
216
  }
216
217
 
217
218
  // Type safety in action
218
- function getUserByEmail(email: Email): User { /* ... */ }
219
+ function getUserByEmail(email: Email): User {
220
+ /* ... */
221
+ }
219
222
 
220
223
  // These calls are type-safe
221
224
  const userId = UserId("U123456")
@@ -238,21 +241,21 @@ import { Option, Either, Try, List } from "functype"
238
241
  const opt = Option(5)
239
242
  const optResult = opt.fold(
240
243
  () => "None",
241
- (value) => `Some(${value})`
244
+ (value) => `Some(${value})`,
242
245
  ) // "Some(5)"
243
246
 
244
247
  // Either fold
245
248
  const either = Right<string, number>(42)
246
249
  const eitherResult = either.fold(
247
250
  (left) => `Left(${left})`,
248
- (right) => `Right(${right})`
251
+ (right) => `Right(${right})`,
249
252
  ) // "Right(42)"
250
253
 
251
254
  // Try fold
252
255
  const tryValue = Try(() => 10)
253
256
  const tryResult = tryValue.fold(
254
257
  (error) => `Error: ${error.message}`,
255
- (value) => `Success: ${value}`
258
+ (value) => `Success: ${value}`,
256
259
  ) // "Success: 10"
257
260
 
258
261
  // List fold
@@ -260,6 +263,85 @@ const list = List([1, 2, 3])
260
263
  const listResult = list.foldLeft(0)((acc, num) => acc + num) // 6
261
264
  ```
262
265
 
266
+ ## Foldable
267
+
268
+ New in v0.8.67, Functype includes a proper `Foldable` type class that data structures can implement:
269
+
270
+ ```typescript
271
+ import { FoldableUtils, Option, List, Try } from "functype"
272
+
273
+ // All data structures implement the Foldable interface
274
+ const option = Option(5)
275
+ const list = List([1, 2, 3, 4, 5])
276
+ const tryVal = Try(() => 10)
277
+
278
+ // Use fold to pattern-match on data structures
279
+ option.fold(
280
+ () => console.log("Empty option"),
281
+ (value) => console.log(`Option value: ${value}`),
282
+ )
283
+
284
+ // Use foldLeft for left-associative operations
285
+ const sum = list.foldLeft(0)((acc, value) => acc + value) // 15
286
+
287
+ // Use foldRight for right-associative operations
288
+ const product = list.foldRight(1)((value, acc) => value * acc) // 120
289
+
290
+ // Use FoldableUtils to work with any Foldable
291
+ const isEmpty = FoldableUtils.isEmpty(option) // false
292
+ const size = FoldableUtils.size(list) // 5
293
+ const convertedToList = FoldableUtils.toList(option) // List([5])
294
+ const convertedToEither = FoldableUtils.toEither(tryVal, "Error") // Right(10)
295
+ ```
296
+
297
+ ## Matchable
298
+
299
+ New in v0.8.68, Functype now includes a `Matchable` type class for enhanced pattern matching:
300
+
301
+ ```typescript
302
+ import { Option, Either, Try, List, MatchableUtils } from "functype"
303
+
304
+ // Pattern matching on Option
305
+ const opt = Option(42)
306
+ const optResult = opt.match({
307
+ Some: (value) => `Found: ${value}`,
308
+ None: () => "Not found",
309
+ }) // "Found: 42"
310
+
311
+ // Pattern matching on Either
312
+ const either = Either.fromNullable(null, "Missing value")
313
+ const eitherResult = either.match({
314
+ Left: (error) => `Error: ${error}`,
315
+ Right: (value) => `Value: ${value}`,
316
+ }) // "Error: Missing value"
317
+
318
+ // Pattern matching on Try
319
+ const tryVal = Try(() => JSON.parse('{"name":"John"}'))
320
+ const tryResult = tryVal.match({
321
+ Success: (data) => `Name: ${data.name}`,
322
+ Failure: (error) => `Parse error: ${error.message}`,
323
+ }) // "Name: John"
324
+
325
+ // Pattern matching on List
326
+ const list = List([1, 2, 3])
327
+ const listResult = list.match({
328
+ NonEmpty: (values) => `Values: ${values.join(", ")}`,
329
+ Empty: () => "No values",
330
+ }) // "Values: 1, 2, 3"
331
+
332
+ // Using MatchableUtils for advanced pattern matching
333
+ const isPositive = MatchableUtils.when(
334
+ (n: number) => n > 0,
335
+ (n) => `Positive: ${n}`,
336
+ )
337
+
338
+ const defaultCase = MatchableUtils.default((n: number) => `Default: ${n}`)
339
+
340
+ // Using pattern guards in custom matching logic
341
+ const num = 42
342
+ const result = isPositive(num) ?? defaultCase(num) // "Positive: 42"
343
+ ```
344
+
263
345
  ## Type Safety
264
346
 
265
347
  Functype leverages TypeScript's advanced type system to provide compile-time safety for functional patterns, ensuring that your code is both robust and maintainable.
@@ -272,6 +354,51 @@ const mappedValue = option.map((x) => x.toString())
272
354
  // Inferred as string
273
355
  ```
274
356
 
357
+ ## Error Formatting
358
+
359
+ Functype provides utilities for improved error visualization and logging:
360
+
361
+ ```typescript
362
+ import { formatError, createErrorSerializer } from "functype/error"
363
+
364
+ // Create a nested task error
365
+ const innerTask = Task({ name: "DbQuery" }).Sync(() => {
366
+ throw new Error("Database connection failed")
367
+ })
368
+
369
+ const outerTask = Task({ name: "UserFetch" }).Sync(() => {
370
+ return innerTask.value
371
+ })
372
+
373
+ // Format the error for console display
374
+ console.error(
375
+ formatError(outerTask.value as Error, {
376
+ includeTasks: true,
377
+ includeStackTrace: true,
378
+ colors: true,
379
+ }),
380
+ )
381
+
382
+ // Create a serializer for structured logging libraries like Pino
383
+ const errorSerializer = createErrorSerializer()
384
+
385
+ // Use with Pino
386
+ const logger = pino({
387
+ serializers: { err: errorSerializer },
388
+ })
389
+
390
+ // Log the error with full context
391
+ logger.error(
392
+ {
393
+ err: outerTask.value,
394
+ requestId: "req-123",
395
+ },
396
+ "Failed to fetch user data",
397
+ )
398
+ ```
399
+
400
+ For more details, see the [Error Formatting Guide](docs/error-formatting.md).
401
+
275
402
  ## Roadmap / TODO
276
403
 
277
404
  ### Missing Functionality
@@ -281,6 +408,9 @@ const mappedValue = option.map((x) => x.toString())
281
408
  - [ ] Add Reader/State/IO monads for more functional patterns
282
409
  - [ ] Implement lens/optics for immutable updates
283
410
  - [ ] Expand concurrent execution utilities beyond FPromise.all
411
+ - [x] Add a proper Foldable type class interface
412
+ - [x] Implement Matchable type class for pattern matching
413
+ - [ ] Implement Applicative and other functional type classes
284
414
 
285
415
  ### Performance Optimizations
286
416
 
@@ -346,4 +476,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
346
476
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
347
477
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
348
478
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
349
- SOFTWARE.
479
+ SOFTWARE.