functype 0.8.80 → 0.8.82
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 +43 -47
- package/dist/{Either-CfG7OVB-.d.ts → Either-CncND8cm.d.ts} +393 -217
- package/dist/Tuple-ZYh96cGE.d.ts +41 -0
- package/dist/Typeable-BBXrKPTY.d.ts +14 -0
- package/dist/chunk-B6FR572T.mjs +2 -0
- package/dist/{chunk-7PQA3W7W.mjs.map → chunk-B6FR572T.mjs.map} +1 -1
- package/dist/chunk-JMCOLAJY.mjs +42 -0
- package/dist/chunk-JMCOLAJY.mjs.map +1 -0
- package/dist/either/index.d.ts +2 -2
- package/dist/either/index.mjs +1 -1
- package/dist/fpromise/index.d.ts +2 -2
- package/dist/fpromise/index.mjs +1 -1
- package/dist/index.d.ts +69 -11
- package/dist/index.mjs +1 -1
- package/dist/list/index.d.ts +2 -2
- package/dist/list/index.mjs +1 -1
- package/dist/map/index.d.ts +58 -4
- package/dist/map/index.mjs +1 -1
- package/dist/option/index.d.ts +2 -2
- package/dist/option/index.mjs +1 -1
- package/dist/set/index.d.ts +2 -2
- package/dist/set/index.mjs +1 -1
- package/dist/try/index.d.ts +13 -4
- package/dist/try/index.mjs +1 -1
- package/dist/tuple/index.d.ts +2 -12
- package/dist/tuple/index.mjs +1 -1
- package/package.json +3 -3
- package/dist/Map-wkGSJvYa.d.ts +0 -65
- package/dist/Valuable-BI2O7E9Q.d.ts +0 -59
- package/dist/chunk-7PQA3W7W.mjs +0 -2
- package/dist/chunk-JIVKCD74.mjs +0 -42
- package/dist/chunk-JIVKCD74.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -133,9 +133,9 @@ const withFive = numbers.add(5) // List([1, 2, 3, 4, 5])
|
|
|
133
133
|
const without3 = numbers.remove(3) // List([1, 2, 4])
|
|
134
134
|
|
|
135
135
|
// Universal container operations
|
|
136
|
-
const hasEven = numbers.exists(x => x % 2 === 0) // true
|
|
137
|
-
const firstEven = numbers.find(x => x % 2 === 0) // Some(2)
|
|
138
|
-
const evenCount = numbers.count(x => x % 2 === 0) // 2
|
|
136
|
+
const hasEven = numbers.exists((x) => x % 2 === 0) // true
|
|
137
|
+
const firstEven = numbers.find((x) => x % 2 === 0) // Some(2)
|
|
138
|
+
const evenCount = numbers.count((x) => x % 2 === 0) // 2
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
### Try
|
|
@@ -179,13 +179,13 @@ const value1 = expensive.get() // Logs "Computing...", returns number
|
|
|
179
179
|
const value2 = expensive.get() // Returns same number, no log
|
|
180
180
|
|
|
181
181
|
// Transform lazy values
|
|
182
|
-
const doubled = expensive.map(x => x * 2)
|
|
183
|
-
const formatted = doubled.map(x => `Value: ${x}`)
|
|
182
|
+
const doubled = expensive.map((x) => x * 2)
|
|
183
|
+
const formatted = doubled.map((x) => `Value: ${x}`)
|
|
184
184
|
|
|
185
185
|
// Chain computations
|
|
186
186
|
const result = Lazy(() => 10)
|
|
187
|
-
.flatMap(x => Lazy(() => x + 5))
|
|
188
|
-
.map(x => x * 2)
|
|
187
|
+
.flatMap((x) => Lazy(() => x + 5))
|
|
188
|
+
.map((x) => x * 2)
|
|
189
189
|
.get() // 30
|
|
190
190
|
```
|
|
191
191
|
|
|
@@ -275,10 +275,10 @@ import { Cond } from "functype"
|
|
|
275
275
|
|
|
276
276
|
// Replace if-else chains with Cond
|
|
277
277
|
const grade = Cond<number, string>()
|
|
278
|
-
.case(score => score >= 90, "A")
|
|
279
|
-
.case(score => score >= 80, "B")
|
|
280
|
-
.case(score => score >= 70, "C")
|
|
281
|
-
.case(score => score >= 60, "D")
|
|
278
|
+
.case((score) => score >= 90, "A")
|
|
279
|
+
.case((score) => score >= 80, "B")
|
|
280
|
+
.case((score) => score >= 70, "C")
|
|
281
|
+
.case((score) => score >= 60, "D")
|
|
282
282
|
.default("F")
|
|
283
283
|
|
|
284
284
|
console.log(grade(85)) // "B"
|
|
@@ -287,16 +287,16 @@ console.log(grade(55)) // "F"
|
|
|
287
287
|
// With transformation
|
|
288
288
|
const discount = Cond<number, number>()
|
|
289
289
|
.case(
|
|
290
|
-
qty => qty >= 100,
|
|
291
|
-
qty => qty * 0.
|
|
290
|
+
(qty) => qty >= 100,
|
|
291
|
+
(qty) => qty * 0.2, // 20% off for 100+
|
|
292
292
|
)
|
|
293
293
|
.case(
|
|
294
|
-
qty => qty >= 50,
|
|
295
|
-
qty => qty * 0.
|
|
294
|
+
(qty) => qty >= 50,
|
|
295
|
+
(qty) => qty * 0.1, // 10% off for 50+
|
|
296
296
|
)
|
|
297
297
|
.case(
|
|
298
|
-
qty => qty >= 10,
|
|
299
|
-
qty => qty * 0.05
|
|
298
|
+
(qty) => qty >= 10,
|
|
299
|
+
(qty) => qty * 0.05, // 5% off for 10+
|
|
300
300
|
)
|
|
301
301
|
.default(0)
|
|
302
302
|
|
|
@@ -323,12 +323,12 @@ console.log(statusMessage("approved")) // "Your request has been approved!"
|
|
|
323
323
|
// Match with predicates
|
|
324
324
|
const numberType = Match<number, string>()
|
|
325
325
|
.case(0, "zero")
|
|
326
|
-
.case(n => n > 0, "positive")
|
|
327
|
-
.case(n => n < 0, "negative")
|
|
326
|
+
.case((n) => n > 0, "positive")
|
|
327
|
+
.case((n) => n < 0, "negative")
|
|
328
328
|
.exhaustive()
|
|
329
329
|
|
|
330
|
-
console.log(numberType(42))
|
|
331
|
-
console.log(numberType(-5))
|
|
330
|
+
console.log(numberType(42)) // "positive"
|
|
331
|
+
console.log(numberType(-5)) // "negative"
|
|
332
332
|
```
|
|
333
333
|
|
|
334
334
|
## Fold
|
|
@@ -464,32 +464,28 @@ All data structures implement the `Functype` hierarchy:
|
|
|
464
464
|
|
|
465
465
|
```typescript
|
|
466
466
|
// Base interface for all data structures
|
|
467
|
-
interface FunctypeBase<A, Tag>
|
|
468
|
-
AsyncMonad<A>,
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
467
|
+
interface FunctypeBase<A, Tag>
|
|
468
|
+
extends AsyncMonad<A>,
|
|
469
|
+
Traversable<A>,
|
|
470
|
+
Serializable<A>,
|
|
471
|
+
Foldable<A>,
|
|
472
|
+
Typeable<Tag>,
|
|
473
|
+
ContainerOps<A> {
|
|
474
474
|
readonly _tag: Tag
|
|
475
475
|
}
|
|
476
476
|
|
|
477
477
|
// For single-value containers (Option, Either, Try)
|
|
478
|
-
interface Functype<A, Tag> extends
|
|
479
|
-
FunctypeBase<A, Tag>,
|
|
480
|
-
Extractable<A>,
|
|
481
|
-
Pipe<A>,
|
|
482
|
-
Matchable<A, Tag> {
|
|
478
|
+
interface Functype<A, Tag> extends FunctypeBase<A, Tag>, Extractable<A>, Pipe<A>, Matchable<A, Tag> {
|
|
483
479
|
toValue(): { _tag: Tag; value: A }
|
|
484
480
|
}
|
|
485
481
|
|
|
486
482
|
// For collections (List, Set, Map)
|
|
487
|
-
interface FunctypeCollection<A, Tag>
|
|
488
|
-
FunctypeBase<A, Tag>,
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
483
|
+
interface FunctypeCollection<A, Tag>
|
|
484
|
+
extends FunctypeBase<A, Tag>,
|
|
485
|
+
Iterable<A>,
|
|
486
|
+
Pipe<A[]>,
|
|
487
|
+
Collection<A>,
|
|
488
|
+
CollectionOps<A, FunctypeCollection<A, Tag>> {
|
|
493
489
|
toValue(): { _tag: Tag; value: A[] }
|
|
494
490
|
// Collections work with Iterable instead of Monad
|
|
495
491
|
flatMap<B>(f: (value: A) => Iterable<B>): FunctypeCollection<B, Tag>
|
|
@@ -507,17 +503,17 @@ const opt = Option(42)
|
|
|
507
503
|
const list = List([1, 2, 3, 4, 5])
|
|
508
504
|
|
|
509
505
|
// Universal operations work on both single-value and collections
|
|
510
|
-
opt.count(x => x > 40)
|
|
511
|
-
list.count(x => x > 3)
|
|
506
|
+
opt.count((x) => x > 40) // 1
|
|
507
|
+
list.count((x) => x > 3) // 2
|
|
512
508
|
|
|
513
|
-
opt.find(x => x > 40)
|
|
514
|
-
list.find(x => x > 3)
|
|
509
|
+
opt.find((x) => x > 40) // Some(42)
|
|
510
|
+
list.find((x) => x > 3) // Some(4)
|
|
515
511
|
|
|
516
|
-
opt.exists(x => x === 42)
|
|
517
|
-
list.exists(x => x === 3)
|
|
512
|
+
opt.exists((x) => x === 42) // true
|
|
513
|
+
list.exists((x) => x === 3) // true
|
|
518
514
|
|
|
519
|
-
opt.forEach(console.log)
|
|
520
|
-
list.forEach(console.log)
|
|
515
|
+
opt.forEach(console.log) // Logs: 42
|
|
516
|
+
list.forEach(console.log) // Logs: 1, 2, 3, 4, 5
|
|
521
517
|
```
|
|
522
518
|
|
|
523
519
|
## Type Safety
|