@nlozgachev/pipelined 0.16.0 → 0.18.0
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 +39 -21
- package/dist/{Task-Bd3gXPRQ.d.mts → Task-DBW4nOZR.d.mts} +79 -79
- package/dist/{Task-BjAkkD6t.d.ts → Task-DUdIQm-Q.d.ts} +79 -79
- package/dist/{chunk-KXAWFKQQ.mjs → chunk-5HMYR4XB.mjs} +0 -2
- package/dist/{chunk-FAZN3IWZ.mjs → chunk-B3YNH6GZ.mjs} +39 -19
- package/dist/{chunk-QPTGO5AS.mjs → chunk-EAR4TIGH.mjs} +26 -26
- package/dist/{chunk-2XKWSZEU.mjs → chunk-UWGFO7BH.mjs} +40 -29
- package/dist/composition.d.mts +18 -18
- package/dist/composition.d.ts +18 -18
- package/dist/composition.js +0 -2
- package/dist/composition.mjs +1 -1
- package/dist/core.d.mts +83 -53
- package/dist/core.d.ts +83 -53
- package/dist/core.js +66 -55
- package/dist/core.mjs +6 -6
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +103 -74
- package/dist/index.mjs +8 -8
- package/dist/utils.d.mts +65 -41
- package/dist/utils.d.ts +65 -41
- package/dist/utils.js +62 -42
- package/dist/utils.mjs +2 -2
- package/package.json +1 -1
package/dist/utils.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { M as Maybe, R as Result, T as Task } from './Task-DBW4nOZR.mjs';
|
|
2
2
|
import { N as NonEmptyList } from './NonEmptyList-BlGFjor5.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Functional array utilities that compose well with pipe.
|
|
6
6
|
* All functions are data-last and curried where applicable.
|
|
7
|
-
* Safe access functions return
|
|
7
|
+
* Safe access functions return Maybe instead of throwing or returning undefined.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```ts
|
|
@@ -26,7 +26,7 @@ declare namespace Arr {
|
|
|
26
26
|
* Arr.head([]); // None
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
const head: <A>(data: readonly A[]) =>
|
|
29
|
+
const head: <A>(data: readonly A[]) => Maybe<A>;
|
|
30
30
|
/**
|
|
31
31
|
* Returns the last element of an array, or None if the array is empty.
|
|
32
32
|
*
|
|
@@ -36,7 +36,7 @@ declare namespace Arr {
|
|
|
36
36
|
* Arr.last([]); // None
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
|
-
const last: <A>(data: readonly A[]) =>
|
|
39
|
+
const last: <A>(data: readonly A[]) => Maybe<A>;
|
|
40
40
|
/**
|
|
41
41
|
* Returns all elements except the first, or None if the array is empty.
|
|
42
42
|
*
|
|
@@ -46,7 +46,7 @@ declare namespace Arr {
|
|
|
46
46
|
* Arr.tail([]); // None
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
const tail: <A>(data: readonly A[]) =>
|
|
49
|
+
const tail: <A>(data: readonly A[]) => Maybe<readonly A[]>;
|
|
50
50
|
/**
|
|
51
51
|
* Returns all elements except the last, or None if the array is empty.
|
|
52
52
|
*
|
|
@@ -56,7 +56,7 @@ declare namespace Arr {
|
|
|
56
56
|
* Arr.init([]); // None
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
|
-
const init: <A>(data: readonly A[]) =>
|
|
59
|
+
const init: <A>(data: readonly A[]) => Maybe<readonly A[]>;
|
|
60
60
|
/**
|
|
61
61
|
* Returns the first element matching the predicate, or None.
|
|
62
62
|
*
|
|
@@ -65,7 +65,7 @@ declare namespace Arr {
|
|
|
65
65
|
* pipe([1, 2, 3, 4], Arr.findFirst(n => n > 2)); // Some(3)
|
|
66
66
|
* ```
|
|
67
67
|
*/
|
|
68
|
-
const findFirst: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) =>
|
|
68
|
+
const findFirst: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) => Maybe<A>;
|
|
69
69
|
/**
|
|
70
70
|
* Returns the last element matching the predicate, or None.
|
|
71
71
|
*
|
|
@@ -74,7 +74,7 @@ declare namespace Arr {
|
|
|
74
74
|
* pipe([1, 2, 3, 4], Arr.findLast(n => n > 2)); // Some(4)
|
|
75
75
|
* ```
|
|
76
76
|
*/
|
|
77
|
-
const findLast: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) =>
|
|
77
|
+
const findLast: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) => Maybe<A>;
|
|
78
78
|
/**
|
|
79
79
|
* Returns the index of the first element matching the predicate, or None.
|
|
80
80
|
*
|
|
@@ -83,7 +83,7 @@ declare namespace Arr {
|
|
|
83
83
|
* pipe([1, 2, 3, 4], Arr.findIndex(n => n > 2)); // Some(2)
|
|
84
84
|
* ```
|
|
85
85
|
*/
|
|
86
|
-
const findIndex: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) =>
|
|
86
|
+
const findIndex: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) => Maybe<number>;
|
|
87
87
|
/**
|
|
88
88
|
* Transforms each element of an array.
|
|
89
89
|
*
|
|
@@ -219,21 +219,21 @@ declare namespace Arr {
|
|
|
219
219
|
*/
|
|
220
220
|
const reduce: <A, B>(initial: B, f: (acc: B, a: A) => B) => (data: readonly A[]) => B;
|
|
221
221
|
/**
|
|
222
|
-
* Maps each element to an
|
|
222
|
+
* Maps each element to an Maybe and collects the results.
|
|
223
223
|
* Returns None if any mapping returns None.
|
|
224
224
|
*
|
|
225
225
|
* @example
|
|
226
226
|
* ```ts
|
|
227
|
-
* const parseNum = (s: string):
|
|
227
|
+
* const parseNum = (s: string): Maybe<number> => {
|
|
228
228
|
* const n = Number(s);
|
|
229
|
-
* return isNaN(n) ?
|
|
229
|
+
* return isNaN(n) ? Maybe.none() : Maybe.some(n);
|
|
230
230
|
* };
|
|
231
231
|
*
|
|
232
232
|
* pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
|
|
233
233
|
* pipe(["1", "x", "3"], Arr.traverse(parseNum)); // None
|
|
234
234
|
* ```
|
|
235
235
|
*/
|
|
236
|
-
const traverse: <A, B>(f: (a: A) =>
|
|
236
|
+
const traverse: <A, B>(f: (a: A) => Maybe<B>) => (data: readonly A[]) => Maybe<readonly B[]>;
|
|
237
237
|
/**
|
|
238
238
|
* Maps each element to a Result and collects the results.
|
|
239
239
|
* Returns the first Err if any mapping fails.
|
|
@@ -260,16 +260,16 @@ declare namespace Arr {
|
|
|
260
260
|
*/
|
|
261
261
|
const traverseTask: <A, B>(f: (a: A) => Task<B>) => (data: readonly A[]) => Task<readonly B[]>;
|
|
262
262
|
/**
|
|
263
|
-
* Collects an array of Options into an
|
|
263
|
+
* Collects an array of Options into an Maybe of array.
|
|
264
264
|
* Returns None if any element is None.
|
|
265
265
|
*
|
|
266
266
|
* @example
|
|
267
267
|
* ```ts
|
|
268
|
-
* Arr.sequence([
|
|
269
|
-
* Arr.sequence([
|
|
268
|
+
* Arr.sequence([Maybe.some(1), Maybe.some(2)]); // Some([1, 2])
|
|
269
|
+
* Arr.sequence([Maybe.some(1), Maybe.none()]); // None
|
|
270
270
|
* ```
|
|
271
271
|
*/
|
|
272
|
-
const sequence: <A>(data: readonly
|
|
272
|
+
const sequence: <A>(data: readonly Maybe<A>[]) => Maybe<readonly A[]>;
|
|
273
273
|
/**
|
|
274
274
|
* Collects an array of Results into a Result of array.
|
|
275
275
|
* Returns the first Err if any element is Err.
|
|
@@ -340,6 +340,30 @@ declare namespace Arr {
|
|
|
340
340
|
* ```
|
|
341
341
|
*/
|
|
342
342
|
const reverse: <A>(data: readonly A[]) => readonly A[];
|
|
343
|
+
/**
|
|
344
|
+
* Returns a new array with `item` inserted before the element at `index`.
|
|
345
|
+
* Negative indices are clamped to 0; indices beyond the array length append to the end.
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```ts
|
|
349
|
+
* pipe([1, 2, 3], Arr.insertAt(1, 99)); // [1, 99, 2, 3]
|
|
350
|
+
* pipe([1, 2, 3], Arr.insertAt(0, 99)); // [99, 1, 2, 3]
|
|
351
|
+
* pipe([1, 2, 3], Arr.insertAt(3, 99)); // [1, 2, 3, 99]
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
const insertAt: <A>(index: number, item: A) => (data: readonly A[]) => readonly A[];
|
|
355
|
+
/**
|
|
356
|
+
* Returns a new array with the element at `index` removed.
|
|
357
|
+
* Returns the original array unchanged if `index` is out of bounds.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* pipe([1, 2, 3], Arr.removeAt(1)); // [1, 3]
|
|
362
|
+
* pipe([1, 2, 3], Arr.removeAt(0)); // [2, 3]
|
|
363
|
+
* pipe([1, 2, 3], Arr.removeAt(5)); // [1, 2, 3]
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
const removeAt: (index: number) => <A>(data: readonly A[]) => readonly A[];
|
|
343
367
|
/**
|
|
344
368
|
* Takes the first n elements from an array.
|
|
345
369
|
*
|
|
@@ -405,7 +429,7 @@ declare namespace Arr {
|
|
|
405
429
|
* and data-last — they compose naturally with `pipe`.
|
|
406
430
|
*
|
|
407
431
|
* Unlike plain objects (`Rec`), dictionaries support any key type, preserve insertion order, and
|
|
408
|
-
* make membership checks explicit via `lookup` returning `
|
|
432
|
+
* make membership checks explicit via `lookup` returning `Maybe`.
|
|
409
433
|
*
|
|
410
434
|
* @example
|
|
411
435
|
* ```ts
|
|
@@ -491,7 +515,7 @@ declare namespace Dict {
|
|
|
491
515
|
* pipe(Dict.fromEntries([["a", 1]]), Dict.lookup("b")); // None
|
|
492
516
|
* ```
|
|
493
517
|
*/
|
|
494
|
-
const lookup: <K>(key: K) => <V>(m: ReadonlyMap<K, V>) =>
|
|
518
|
+
const lookup: <K>(key: K) => <V>(m: ReadonlyMap<K, V>) => Maybe<V>;
|
|
495
519
|
/**
|
|
496
520
|
* Returns the number of entries in the dictionary.
|
|
497
521
|
*
|
|
@@ -567,14 +591,14 @@ declare namespace Dict {
|
|
|
567
591
|
*
|
|
568
592
|
* @example
|
|
569
593
|
* ```ts
|
|
570
|
-
* import {
|
|
594
|
+
* import { Maybe } from "@nlozgachev/pipelined/core";
|
|
571
595
|
*
|
|
572
|
-
* const increment = (opt:
|
|
596
|
+
* const increment = (opt: Maybe<number>) => Maybe.getOrElse(() => 0)(opt) + 1;
|
|
573
597
|
* pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("views", increment)); // { views: 6 }
|
|
574
598
|
* pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("likes", increment)); // { views: 5, likes: 1 }
|
|
575
599
|
* ```
|
|
576
600
|
*/
|
|
577
|
-
const upsert: <K, V>(key: K, f: (existing:
|
|
601
|
+
const upsert: <K, V>(key: K, f: (existing: Maybe<V>) => V) => (m: ReadonlyMap<K, V>) => ReadonlyMap<K, V>;
|
|
578
602
|
/**
|
|
579
603
|
* Transforms each value in the dictionary.
|
|
580
604
|
*
|
|
@@ -617,22 +641,22 @@ declare namespace Dict {
|
|
|
617
641
|
*/
|
|
618
642
|
const filterWithKey: <K, A>(predicate: (key: K, a: A) => boolean) => (m: ReadonlyMap<K, A>) => ReadonlyMap<K, A>;
|
|
619
643
|
/**
|
|
620
|
-
* Removes all `None` values from a `ReadonlyMap<K,
|
|
644
|
+
* Removes all `None` values from a `ReadonlyMap<K, Maybe<A>>`, returning a plain
|
|
621
645
|
* `ReadonlyMap<K, A>`. Useful when building dictionaries from fallible lookups.
|
|
622
646
|
*
|
|
623
647
|
* @example
|
|
624
648
|
* ```ts
|
|
625
|
-
* import {
|
|
649
|
+
* import { Maybe } from "@nlozgachev/pipelined/core";
|
|
626
650
|
*
|
|
627
651
|
* Dict.compact(Dict.fromEntries([
|
|
628
|
-
* ["a",
|
|
629
|
-
* ["b",
|
|
630
|
-
* ["c",
|
|
652
|
+
* ["a", Maybe.some(1)],
|
|
653
|
+
* ["b", Maybe.none()],
|
|
654
|
+
* ["c", Maybe.some(3)],
|
|
631
655
|
* ]));
|
|
632
656
|
* // ReadonlyMap { "a" => 1, "c" => 3 }
|
|
633
657
|
* ```
|
|
634
658
|
*/
|
|
635
|
-
const compact: <K, A>(m: ReadonlyMap<K,
|
|
659
|
+
const compact: <K, A>(m: ReadonlyMap<K, Maybe<A>>) => ReadonlyMap<K, A>;
|
|
636
660
|
/**
|
|
637
661
|
* Merges two dictionaries. When both contain the same key, the value from `other` takes
|
|
638
662
|
* precedence.
|
|
@@ -775,7 +799,7 @@ declare namespace Num {
|
|
|
775
799
|
* Num.parse(""); // None
|
|
776
800
|
* ```
|
|
777
801
|
*/
|
|
778
|
-
const parse: (s: string) =>
|
|
802
|
+
const parse: (s: string) => Maybe<number>;
|
|
779
803
|
/**
|
|
780
804
|
* Adds `b` to a number. Data-last: use in `pipe` or `Arr.map`.
|
|
781
805
|
*
|
|
@@ -871,7 +895,7 @@ declare namespace Rec {
|
|
|
871
895
|
*/
|
|
872
896
|
const filterWithKey: <A>(predicate: (key: string, a: A) => boolean) => (data: Readonly<Record<string, A>>) => Readonly<Record<string, A>>;
|
|
873
897
|
/**
|
|
874
|
-
* Looks up a value by key, returning
|
|
898
|
+
* Looks up a value by key, returning Maybe.
|
|
875
899
|
*
|
|
876
900
|
* @example
|
|
877
901
|
* ```ts
|
|
@@ -879,19 +903,19 @@ declare namespace Rec {
|
|
|
879
903
|
* pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
|
|
880
904
|
* ```
|
|
881
905
|
*/
|
|
882
|
-
const lookup: (key:
|
|
906
|
+
const lookup: <K extends string>(key: K) => <V>(data: Record<string, V>) => Maybe<V>;
|
|
883
907
|
/**
|
|
884
908
|
* Returns all keys of a record.
|
|
885
909
|
*/
|
|
886
|
-
const keys: <
|
|
910
|
+
const keys: <T extends Record<string, unknown>>(data: T) => readonly (keyof T & string)[];
|
|
887
911
|
/**
|
|
888
912
|
* Returns all values of a record.
|
|
889
913
|
*/
|
|
890
|
-
const values: <
|
|
914
|
+
const values: <T extends Record<string, unknown>>(data: T) => readonly T[keyof T & string][];
|
|
891
915
|
/**
|
|
892
916
|
* Returns all key-value pairs of a record.
|
|
893
917
|
*/
|
|
894
|
-
const entries: <
|
|
918
|
+
const entries: <T extends Record<string, unknown>>(data: T) => readonly (readonly [keyof T, T[keyof T]])[];
|
|
895
919
|
/**
|
|
896
920
|
* Creates a record from key-value pairs.
|
|
897
921
|
*
|
|
@@ -964,21 +988,21 @@ declare namespace Rec {
|
|
|
964
988
|
*/
|
|
965
989
|
const mapKeys: (f: (key: string) => string) => <A>(data: Readonly<Record<string, A>>) => Readonly<Record<string, A>>;
|
|
966
990
|
/**
|
|
967
|
-
* Removes all `None` values from a `Record<string,
|
|
991
|
+
* Removes all `None` values from a `Record<string, Maybe<A>>`, returning a plain `Record<string, A>`.
|
|
968
992
|
* Useful when building records from fallible lookups.
|
|
969
993
|
*
|
|
970
994
|
* @example
|
|
971
995
|
* ```ts
|
|
972
|
-
* Rec.compact({ a:
|
|
996
|
+
* Rec.compact({ a: Maybe.some(1), b: Maybe.none(), c: Maybe.some(3) });
|
|
973
997
|
* // { a: 1, c: 3 }
|
|
974
998
|
* ```
|
|
975
999
|
*/
|
|
976
|
-
const compact: <A>(data: Readonly<Record<string,
|
|
1000
|
+
const compact: <A>(data: Readonly<Record<string, Maybe<A>>>) => Readonly<Record<string, A>>;
|
|
977
1001
|
}
|
|
978
1002
|
|
|
979
1003
|
/**
|
|
980
1004
|
* String utilities. All transformation functions are data-last and curried so they
|
|
981
|
-
* compose naturally with `pipe`. Safe parsers return `
|
|
1005
|
+
* compose naturally with `pipe`. Safe parsers return `Maybe` instead of `NaN`.
|
|
982
1006
|
*
|
|
983
1007
|
* @example
|
|
984
1008
|
* ```ts
|
|
@@ -1075,7 +1099,7 @@ declare namespace Str {
|
|
|
1075
1099
|
*/
|
|
1076
1100
|
const words: (s: string) => readonly string[];
|
|
1077
1101
|
/**
|
|
1078
|
-
* Safe number parsers that return `
|
|
1102
|
+
* Safe number parsers that return `Maybe` instead of `NaN`.
|
|
1079
1103
|
*/
|
|
1080
1104
|
const parse: {
|
|
1081
1105
|
/**
|
|
@@ -1088,7 +1112,7 @@ declare namespace Str {
|
|
|
1088
1112
|
* Str.parse.int("abc"); // None
|
|
1089
1113
|
* ```
|
|
1090
1114
|
*/
|
|
1091
|
-
int: (s: string) =>
|
|
1115
|
+
int: (s: string) => Maybe<number>;
|
|
1092
1116
|
/**
|
|
1093
1117
|
* Parses a string as a floating-point number. Returns `None` if the result is `NaN`.
|
|
1094
1118
|
*
|
|
@@ -1099,7 +1123,7 @@ declare namespace Str {
|
|
|
1099
1123
|
* Str.parse.float("abc"); // None
|
|
1100
1124
|
* ```
|
|
1101
1125
|
*/
|
|
1102
|
-
float: (s: string) =>
|
|
1126
|
+
float: (s: string) => Maybe<number>;
|
|
1103
1127
|
};
|
|
1104
1128
|
}
|
|
1105
1129
|
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { M as Maybe, R as Result, T as Task } from './Task-DUdIQm-Q.js';
|
|
2
2
|
import { N as NonEmptyList } from './NonEmptyList-BlGFjor5.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Functional array utilities that compose well with pipe.
|
|
6
6
|
* All functions are data-last and curried where applicable.
|
|
7
|
-
* Safe access functions return
|
|
7
|
+
* Safe access functions return Maybe instead of throwing or returning undefined.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```ts
|
|
@@ -26,7 +26,7 @@ declare namespace Arr {
|
|
|
26
26
|
* Arr.head([]); // None
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
const head: <A>(data: readonly A[]) =>
|
|
29
|
+
const head: <A>(data: readonly A[]) => Maybe<A>;
|
|
30
30
|
/**
|
|
31
31
|
* Returns the last element of an array, or None if the array is empty.
|
|
32
32
|
*
|
|
@@ -36,7 +36,7 @@ declare namespace Arr {
|
|
|
36
36
|
* Arr.last([]); // None
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
|
-
const last: <A>(data: readonly A[]) =>
|
|
39
|
+
const last: <A>(data: readonly A[]) => Maybe<A>;
|
|
40
40
|
/**
|
|
41
41
|
* Returns all elements except the first, or None if the array is empty.
|
|
42
42
|
*
|
|
@@ -46,7 +46,7 @@ declare namespace Arr {
|
|
|
46
46
|
* Arr.tail([]); // None
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
const tail: <A>(data: readonly A[]) =>
|
|
49
|
+
const tail: <A>(data: readonly A[]) => Maybe<readonly A[]>;
|
|
50
50
|
/**
|
|
51
51
|
* Returns all elements except the last, or None if the array is empty.
|
|
52
52
|
*
|
|
@@ -56,7 +56,7 @@ declare namespace Arr {
|
|
|
56
56
|
* Arr.init([]); // None
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
|
-
const init: <A>(data: readonly A[]) =>
|
|
59
|
+
const init: <A>(data: readonly A[]) => Maybe<readonly A[]>;
|
|
60
60
|
/**
|
|
61
61
|
* Returns the first element matching the predicate, or None.
|
|
62
62
|
*
|
|
@@ -65,7 +65,7 @@ declare namespace Arr {
|
|
|
65
65
|
* pipe([1, 2, 3, 4], Arr.findFirst(n => n > 2)); // Some(3)
|
|
66
66
|
* ```
|
|
67
67
|
*/
|
|
68
|
-
const findFirst: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) =>
|
|
68
|
+
const findFirst: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) => Maybe<A>;
|
|
69
69
|
/**
|
|
70
70
|
* Returns the last element matching the predicate, or None.
|
|
71
71
|
*
|
|
@@ -74,7 +74,7 @@ declare namespace Arr {
|
|
|
74
74
|
* pipe([1, 2, 3, 4], Arr.findLast(n => n > 2)); // Some(4)
|
|
75
75
|
* ```
|
|
76
76
|
*/
|
|
77
|
-
const findLast: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) =>
|
|
77
|
+
const findLast: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) => Maybe<A>;
|
|
78
78
|
/**
|
|
79
79
|
* Returns the index of the first element matching the predicate, or None.
|
|
80
80
|
*
|
|
@@ -83,7 +83,7 @@ declare namespace Arr {
|
|
|
83
83
|
* pipe([1, 2, 3, 4], Arr.findIndex(n => n > 2)); // Some(2)
|
|
84
84
|
* ```
|
|
85
85
|
*/
|
|
86
|
-
const findIndex: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) =>
|
|
86
|
+
const findIndex: <A>(predicate: (a: A) => boolean) => (data: readonly A[]) => Maybe<number>;
|
|
87
87
|
/**
|
|
88
88
|
* Transforms each element of an array.
|
|
89
89
|
*
|
|
@@ -219,21 +219,21 @@ declare namespace Arr {
|
|
|
219
219
|
*/
|
|
220
220
|
const reduce: <A, B>(initial: B, f: (acc: B, a: A) => B) => (data: readonly A[]) => B;
|
|
221
221
|
/**
|
|
222
|
-
* Maps each element to an
|
|
222
|
+
* Maps each element to an Maybe and collects the results.
|
|
223
223
|
* Returns None if any mapping returns None.
|
|
224
224
|
*
|
|
225
225
|
* @example
|
|
226
226
|
* ```ts
|
|
227
|
-
* const parseNum = (s: string):
|
|
227
|
+
* const parseNum = (s: string): Maybe<number> => {
|
|
228
228
|
* const n = Number(s);
|
|
229
|
-
* return isNaN(n) ?
|
|
229
|
+
* return isNaN(n) ? Maybe.none() : Maybe.some(n);
|
|
230
230
|
* };
|
|
231
231
|
*
|
|
232
232
|
* pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
|
|
233
233
|
* pipe(["1", "x", "3"], Arr.traverse(parseNum)); // None
|
|
234
234
|
* ```
|
|
235
235
|
*/
|
|
236
|
-
const traverse: <A, B>(f: (a: A) =>
|
|
236
|
+
const traverse: <A, B>(f: (a: A) => Maybe<B>) => (data: readonly A[]) => Maybe<readonly B[]>;
|
|
237
237
|
/**
|
|
238
238
|
* Maps each element to a Result and collects the results.
|
|
239
239
|
* Returns the first Err if any mapping fails.
|
|
@@ -260,16 +260,16 @@ declare namespace Arr {
|
|
|
260
260
|
*/
|
|
261
261
|
const traverseTask: <A, B>(f: (a: A) => Task<B>) => (data: readonly A[]) => Task<readonly B[]>;
|
|
262
262
|
/**
|
|
263
|
-
* Collects an array of Options into an
|
|
263
|
+
* Collects an array of Options into an Maybe of array.
|
|
264
264
|
* Returns None if any element is None.
|
|
265
265
|
*
|
|
266
266
|
* @example
|
|
267
267
|
* ```ts
|
|
268
|
-
* Arr.sequence([
|
|
269
|
-
* Arr.sequence([
|
|
268
|
+
* Arr.sequence([Maybe.some(1), Maybe.some(2)]); // Some([1, 2])
|
|
269
|
+
* Arr.sequence([Maybe.some(1), Maybe.none()]); // None
|
|
270
270
|
* ```
|
|
271
271
|
*/
|
|
272
|
-
const sequence: <A>(data: readonly
|
|
272
|
+
const sequence: <A>(data: readonly Maybe<A>[]) => Maybe<readonly A[]>;
|
|
273
273
|
/**
|
|
274
274
|
* Collects an array of Results into a Result of array.
|
|
275
275
|
* Returns the first Err if any element is Err.
|
|
@@ -340,6 +340,30 @@ declare namespace Arr {
|
|
|
340
340
|
* ```
|
|
341
341
|
*/
|
|
342
342
|
const reverse: <A>(data: readonly A[]) => readonly A[];
|
|
343
|
+
/**
|
|
344
|
+
* Returns a new array with `item` inserted before the element at `index`.
|
|
345
|
+
* Negative indices are clamped to 0; indices beyond the array length append to the end.
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```ts
|
|
349
|
+
* pipe([1, 2, 3], Arr.insertAt(1, 99)); // [1, 99, 2, 3]
|
|
350
|
+
* pipe([1, 2, 3], Arr.insertAt(0, 99)); // [99, 1, 2, 3]
|
|
351
|
+
* pipe([1, 2, 3], Arr.insertAt(3, 99)); // [1, 2, 3, 99]
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
const insertAt: <A>(index: number, item: A) => (data: readonly A[]) => readonly A[];
|
|
355
|
+
/**
|
|
356
|
+
* Returns a new array with the element at `index` removed.
|
|
357
|
+
* Returns the original array unchanged if `index` is out of bounds.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* pipe([1, 2, 3], Arr.removeAt(1)); // [1, 3]
|
|
362
|
+
* pipe([1, 2, 3], Arr.removeAt(0)); // [2, 3]
|
|
363
|
+
* pipe([1, 2, 3], Arr.removeAt(5)); // [1, 2, 3]
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
const removeAt: (index: number) => <A>(data: readonly A[]) => readonly A[];
|
|
343
367
|
/**
|
|
344
368
|
* Takes the first n elements from an array.
|
|
345
369
|
*
|
|
@@ -405,7 +429,7 @@ declare namespace Arr {
|
|
|
405
429
|
* and data-last — they compose naturally with `pipe`.
|
|
406
430
|
*
|
|
407
431
|
* Unlike plain objects (`Rec`), dictionaries support any key type, preserve insertion order, and
|
|
408
|
-
* make membership checks explicit via `lookup` returning `
|
|
432
|
+
* make membership checks explicit via `lookup` returning `Maybe`.
|
|
409
433
|
*
|
|
410
434
|
* @example
|
|
411
435
|
* ```ts
|
|
@@ -491,7 +515,7 @@ declare namespace Dict {
|
|
|
491
515
|
* pipe(Dict.fromEntries([["a", 1]]), Dict.lookup("b")); // None
|
|
492
516
|
* ```
|
|
493
517
|
*/
|
|
494
|
-
const lookup: <K>(key: K) => <V>(m: ReadonlyMap<K, V>) =>
|
|
518
|
+
const lookup: <K>(key: K) => <V>(m: ReadonlyMap<K, V>) => Maybe<V>;
|
|
495
519
|
/**
|
|
496
520
|
* Returns the number of entries in the dictionary.
|
|
497
521
|
*
|
|
@@ -567,14 +591,14 @@ declare namespace Dict {
|
|
|
567
591
|
*
|
|
568
592
|
* @example
|
|
569
593
|
* ```ts
|
|
570
|
-
* import {
|
|
594
|
+
* import { Maybe } from "@nlozgachev/pipelined/core";
|
|
571
595
|
*
|
|
572
|
-
* const increment = (opt:
|
|
596
|
+
* const increment = (opt: Maybe<number>) => Maybe.getOrElse(() => 0)(opt) + 1;
|
|
573
597
|
* pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("views", increment)); // { views: 6 }
|
|
574
598
|
* pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("likes", increment)); // { views: 5, likes: 1 }
|
|
575
599
|
* ```
|
|
576
600
|
*/
|
|
577
|
-
const upsert: <K, V>(key: K, f: (existing:
|
|
601
|
+
const upsert: <K, V>(key: K, f: (existing: Maybe<V>) => V) => (m: ReadonlyMap<K, V>) => ReadonlyMap<K, V>;
|
|
578
602
|
/**
|
|
579
603
|
* Transforms each value in the dictionary.
|
|
580
604
|
*
|
|
@@ -617,22 +641,22 @@ declare namespace Dict {
|
|
|
617
641
|
*/
|
|
618
642
|
const filterWithKey: <K, A>(predicate: (key: K, a: A) => boolean) => (m: ReadonlyMap<K, A>) => ReadonlyMap<K, A>;
|
|
619
643
|
/**
|
|
620
|
-
* Removes all `None` values from a `ReadonlyMap<K,
|
|
644
|
+
* Removes all `None` values from a `ReadonlyMap<K, Maybe<A>>`, returning a plain
|
|
621
645
|
* `ReadonlyMap<K, A>`. Useful when building dictionaries from fallible lookups.
|
|
622
646
|
*
|
|
623
647
|
* @example
|
|
624
648
|
* ```ts
|
|
625
|
-
* import {
|
|
649
|
+
* import { Maybe } from "@nlozgachev/pipelined/core";
|
|
626
650
|
*
|
|
627
651
|
* Dict.compact(Dict.fromEntries([
|
|
628
|
-
* ["a",
|
|
629
|
-
* ["b",
|
|
630
|
-
* ["c",
|
|
652
|
+
* ["a", Maybe.some(1)],
|
|
653
|
+
* ["b", Maybe.none()],
|
|
654
|
+
* ["c", Maybe.some(3)],
|
|
631
655
|
* ]));
|
|
632
656
|
* // ReadonlyMap { "a" => 1, "c" => 3 }
|
|
633
657
|
* ```
|
|
634
658
|
*/
|
|
635
|
-
const compact: <K, A>(m: ReadonlyMap<K,
|
|
659
|
+
const compact: <K, A>(m: ReadonlyMap<K, Maybe<A>>) => ReadonlyMap<K, A>;
|
|
636
660
|
/**
|
|
637
661
|
* Merges two dictionaries. When both contain the same key, the value from `other` takes
|
|
638
662
|
* precedence.
|
|
@@ -775,7 +799,7 @@ declare namespace Num {
|
|
|
775
799
|
* Num.parse(""); // None
|
|
776
800
|
* ```
|
|
777
801
|
*/
|
|
778
|
-
const parse: (s: string) =>
|
|
802
|
+
const parse: (s: string) => Maybe<number>;
|
|
779
803
|
/**
|
|
780
804
|
* Adds `b` to a number. Data-last: use in `pipe` or `Arr.map`.
|
|
781
805
|
*
|
|
@@ -871,7 +895,7 @@ declare namespace Rec {
|
|
|
871
895
|
*/
|
|
872
896
|
const filterWithKey: <A>(predicate: (key: string, a: A) => boolean) => (data: Readonly<Record<string, A>>) => Readonly<Record<string, A>>;
|
|
873
897
|
/**
|
|
874
|
-
* Looks up a value by key, returning
|
|
898
|
+
* Looks up a value by key, returning Maybe.
|
|
875
899
|
*
|
|
876
900
|
* @example
|
|
877
901
|
* ```ts
|
|
@@ -879,19 +903,19 @@ declare namespace Rec {
|
|
|
879
903
|
* pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
|
|
880
904
|
* ```
|
|
881
905
|
*/
|
|
882
|
-
const lookup: (key:
|
|
906
|
+
const lookup: <K extends string>(key: K) => <V>(data: Record<string, V>) => Maybe<V>;
|
|
883
907
|
/**
|
|
884
908
|
* Returns all keys of a record.
|
|
885
909
|
*/
|
|
886
|
-
const keys: <
|
|
910
|
+
const keys: <T extends Record<string, unknown>>(data: T) => readonly (keyof T & string)[];
|
|
887
911
|
/**
|
|
888
912
|
* Returns all values of a record.
|
|
889
913
|
*/
|
|
890
|
-
const values: <
|
|
914
|
+
const values: <T extends Record<string, unknown>>(data: T) => readonly T[keyof T & string][];
|
|
891
915
|
/**
|
|
892
916
|
* Returns all key-value pairs of a record.
|
|
893
917
|
*/
|
|
894
|
-
const entries: <
|
|
918
|
+
const entries: <T extends Record<string, unknown>>(data: T) => readonly (readonly [keyof T, T[keyof T]])[];
|
|
895
919
|
/**
|
|
896
920
|
* Creates a record from key-value pairs.
|
|
897
921
|
*
|
|
@@ -964,21 +988,21 @@ declare namespace Rec {
|
|
|
964
988
|
*/
|
|
965
989
|
const mapKeys: (f: (key: string) => string) => <A>(data: Readonly<Record<string, A>>) => Readonly<Record<string, A>>;
|
|
966
990
|
/**
|
|
967
|
-
* Removes all `None` values from a `Record<string,
|
|
991
|
+
* Removes all `None` values from a `Record<string, Maybe<A>>`, returning a plain `Record<string, A>`.
|
|
968
992
|
* Useful when building records from fallible lookups.
|
|
969
993
|
*
|
|
970
994
|
* @example
|
|
971
995
|
* ```ts
|
|
972
|
-
* Rec.compact({ a:
|
|
996
|
+
* Rec.compact({ a: Maybe.some(1), b: Maybe.none(), c: Maybe.some(3) });
|
|
973
997
|
* // { a: 1, c: 3 }
|
|
974
998
|
* ```
|
|
975
999
|
*/
|
|
976
|
-
const compact: <A>(data: Readonly<Record<string,
|
|
1000
|
+
const compact: <A>(data: Readonly<Record<string, Maybe<A>>>) => Readonly<Record<string, A>>;
|
|
977
1001
|
}
|
|
978
1002
|
|
|
979
1003
|
/**
|
|
980
1004
|
* String utilities. All transformation functions are data-last and curried so they
|
|
981
|
-
* compose naturally with `pipe`. Safe parsers return `
|
|
1005
|
+
* compose naturally with `pipe`. Safe parsers return `Maybe` instead of `NaN`.
|
|
982
1006
|
*
|
|
983
1007
|
* @example
|
|
984
1008
|
* ```ts
|
|
@@ -1075,7 +1099,7 @@ declare namespace Str {
|
|
|
1075
1099
|
*/
|
|
1076
1100
|
const words: (s: string) => readonly string[];
|
|
1077
1101
|
/**
|
|
1078
|
-
* Safe number parsers that return `
|
|
1102
|
+
* Safe number parsers that return `Maybe` instead of `NaN`.
|
|
1079
1103
|
*/
|
|
1080
1104
|
const parse: {
|
|
1081
1105
|
/**
|
|
@@ -1088,7 +1112,7 @@ declare namespace Str {
|
|
|
1088
1112
|
* Str.parse.int("abc"); // None
|
|
1089
1113
|
* ```
|
|
1090
1114
|
*/
|
|
1091
|
-
int: (s: string) =>
|
|
1115
|
+
int: (s: string) => Maybe<number>;
|
|
1092
1116
|
/**
|
|
1093
1117
|
* Parses a string as a floating-point number. Returns `None` if the result is `NaN`.
|
|
1094
1118
|
*
|
|
@@ -1099,7 +1123,7 @@ declare namespace Str {
|
|
|
1099
1123
|
* Str.parse.float("abc"); // None
|
|
1100
1124
|
* ```
|
|
1101
1125
|
*/
|
|
1102
|
-
float: (s: string) =>
|
|
1126
|
+
float: (s: string) => Maybe<number>;
|
|
1103
1127
|
};
|
|
1104
1128
|
}
|
|
1105
1129
|
|