@nlozgachev/pipekit 0.1.6
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 +182 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/src/Composition/compose.js +3 -0
- package/esm/src/Composition/curry.js +42 -0
- package/esm/src/Composition/flip.js +20 -0
- package/esm/src/Composition/flow.js +8 -0
- package/esm/src/Composition/fn.js +85 -0
- package/esm/src/Composition/index.js +10 -0
- package/esm/src/Composition/memoize.js +66 -0
- package/esm/src/Composition/not.js +25 -0
- package/esm/src/Composition/pipe.js +3 -0
- package/esm/src/Composition/tap.js +33 -0
- package/esm/src/Composition/uncurry.js +32 -0
- package/esm/src/Core/Arr.js +461 -0
- package/esm/src/Core/InternalTypes.js +1 -0
- package/esm/src/Core/Option.js +195 -0
- package/esm/src/Core/Rec.js +167 -0
- package/esm/src/Core/RemoteData.js +210 -0
- package/esm/src/Core/Result.js +173 -0
- package/esm/src/Core/Task.js +108 -0
- package/esm/src/Core/TaskResult.js +63 -0
- package/esm/src/Core/Validation.js +215 -0
- package/esm/src/Core/index.js +8 -0
- package/esm/src/Types/NonEmptyList.js +14 -0
- package/esm/src/Types/index.js +1 -0
- package/package.json +60 -0
- package/script/mod.js +19 -0
- package/script/package.json +3 -0
- package/script/src/Composition/compose.js +6 -0
- package/script/src/Composition/curry.js +48 -0
- package/script/src/Composition/flip.js +24 -0
- package/script/src/Composition/flow.js +11 -0
- package/script/src/Composition/fn.js +98 -0
- package/script/src/Composition/index.js +26 -0
- package/script/src/Composition/memoize.js +71 -0
- package/script/src/Composition/not.js +29 -0
- package/script/src/Composition/pipe.js +6 -0
- package/script/src/Composition/tap.js +37 -0
- package/script/src/Composition/uncurry.js +38 -0
- package/script/src/Core/Arr.js +464 -0
- package/script/src/Core/InternalTypes.js +2 -0
- package/script/src/Core/Option.js +198 -0
- package/script/src/Core/Rec.js +170 -0
- package/script/src/Core/RemoteData.js +213 -0
- package/script/src/Core/Result.js +176 -0
- package/script/src/Core/Task.js +111 -0
- package/script/src/Core/TaskResult.js +66 -0
- package/script/src/Core/Validation.js +218 -0
- package/script/src/Core/index.js +24 -0
- package/script/src/Types/NonEmptyList.js +18 -0
- package/script/src/Types/index.js +17 -0
- package/types/mod.d.ts +4 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/Composition/compose.d.ts +33 -0
- package/types/src/Composition/compose.d.ts.map +1 -0
- package/types/src/Composition/curry.d.ts +43 -0
- package/types/src/Composition/curry.d.ts.map +1 -0
- package/types/src/Composition/flip.d.ts +21 -0
- package/types/src/Composition/flip.d.ts.map +1 -0
- package/types/src/Composition/flow.d.ts +56 -0
- package/types/src/Composition/flow.d.ts.map +1 -0
- package/types/src/Composition/fn.d.ts +76 -0
- package/types/src/Composition/fn.d.ts.map +1 -0
- package/types/src/Composition/index.d.ts +11 -0
- package/types/src/Composition/index.d.ts.map +1 -0
- package/types/src/Composition/memoize.d.ts +46 -0
- package/types/src/Composition/memoize.d.ts.map +1 -0
- package/types/src/Composition/not.d.ts +26 -0
- package/types/src/Composition/not.d.ts.map +1 -0
- package/types/src/Composition/pipe.d.ts +56 -0
- package/types/src/Composition/pipe.d.ts.map +1 -0
- package/types/src/Composition/tap.d.ts +31 -0
- package/types/src/Composition/tap.d.ts.map +1 -0
- package/types/src/Composition/uncurry.d.ts +54 -0
- package/types/src/Composition/uncurry.d.ts.map +1 -0
- package/types/src/Core/Arr.d.ts +355 -0
- package/types/src/Core/Arr.d.ts.map +1 -0
- package/types/src/Core/InternalTypes.d.ts +14 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -0
- package/types/src/Core/Option.d.ts +214 -0
- package/types/src/Core/Option.d.ts.map +1 -0
- package/types/src/Core/Rec.d.ts +121 -0
- package/types/src/Core/Rec.d.ts.map +1 -0
- package/types/src/Core/RemoteData.d.ts +196 -0
- package/types/src/Core/RemoteData.d.ts.map +1 -0
- package/types/src/Core/Result.d.ts +185 -0
- package/types/src/Core/Result.d.ts.map +1 -0
- package/types/src/Core/Task.d.ts +125 -0
- package/types/src/Core/Task.d.ts.map +1 -0
- package/types/src/Core/TaskResult.d.ts +78 -0
- package/types/src/Core/TaskResult.d.ts.map +1 -0
- package/types/src/Core/Validation.d.ts +217 -0
- package/types/src/Core/Validation.d.ts.map +1 -0
- package/types/src/Core/index.d.ts +9 -0
- package/types/src/Core/index.d.ts.map +1 -0
- package/types/src/Types/NonEmptyList.d.ts +29 -0
- package/types/src/Types/NonEmptyList.d.ts.map +1 -0
- package/types/src/Types/index.d.ts +2 -0
- package/types/src/Types/index.d.ts.map +1 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.not = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Negates a predicate function.
|
|
6
|
+
* Returns a new predicate that returns true when the original returns false, and vice versa.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const isEven = (n: number) => n % 2 === 0;
|
|
11
|
+
* const isOdd = not(isEven);
|
|
12
|
+
*
|
|
13
|
+
* isOdd(3); // true
|
|
14
|
+
* isOdd(4); // false
|
|
15
|
+
*
|
|
16
|
+
* // With array methods
|
|
17
|
+
* const numbers = [1, 2, 3, 4, 5];
|
|
18
|
+
* numbers.filter(not(isEven)); // [1, 3, 5]
|
|
19
|
+
*
|
|
20
|
+
* // In pipelines
|
|
21
|
+
* pipe(
|
|
22
|
+
* users,
|
|
23
|
+
* Array.filter(not(isAdmin)),
|
|
24
|
+
* Array.map(u => u.name)
|
|
25
|
+
* );
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const not = (predicate) => (...args) => !predicate(...args);
|
|
29
|
+
exports.not = not;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tap = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Executes a side effect function and returns the original value unchanged.
|
|
6
|
+
* Useful for logging, debugging, or other side effects within a pipeline.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // Debugging a pipeline
|
|
11
|
+
* pipe(
|
|
12
|
+
* Option.of(5),
|
|
13
|
+
* tap(x => console.log("Before map:", x)),
|
|
14
|
+
* Option.map(n => n * 2),
|
|
15
|
+
* tap(x => console.log("After map:", x)),
|
|
16
|
+
* Option.getOrElse(0)
|
|
17
|
+
* );
|
|
18
|
+
* // logs: "Before map: { kind: 'Some', value: 5 }"
|
|
19
|
+
* // logs: "After map: { kind: 'Some', value: 10 }"
|
|
20
|
+
* // returns: 10
|
|
21
|
+
*
|
|
22
|
+
* // Collecting intermediate values
|
|
23
|
+
* const values: number[] = [];
|
|
24
|
+
* pipe(
|
|
25
|
+
* [1, 2, 3],
|
|
26
|
+
* arr => arr.map(n => n * 2),
|
|
27
|
+
* tap(arr => values.push(...arr))
|
|
28
|
+
* );
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link Option.tap} for Option-specific tap that only runs on Some
|
|
32
|
+
*/
|
|
33
|
+
const tap = (f) => (a) => {
|
|
34
|
+
f(a);
|
|
35
|
+
return a;
|
|
36
|
+
};
|
|
37
|
+
exports.tap = tap;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uncurry4 = exports.uncurry3 = void 0;
|
|
4
|
+
exports.uncurry = uncurry;
|
|
5
|
+
// deno-lint-ignore no-explicit-any
|
|
6
|
+
function uncurry(f) {
|
|
7
|
+
// f.length determines the outer arity; inner.length determines the inner arity.
|
|
8
|
+
// The typed overloads guarantee these are 0, 1, or 2 total args.
|
|
9
|
+
// deno-lint-ignore no-explicit-any
|
|
10
|
+
return (...args) => {
|
|
11
|
+
const inner = f(...args.slice(0, f.length));
|
|
12
|
+
return inner.length === 0 ? inner() : inner(...args.slice(f.length));
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts a curried 3-argument function into a multi-argument function.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const curriedAdd3 = (a: number) => (b: number) => (c: number) => a + b + c;
|
|
21
|
+
* const add3 = uncurry3(curriedAdd3);
|
|
22
|
+
* add3(1, 2, 3); // 6
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
const uncurry3 = (f) => (a, b, c) => f(a)(b)(c);
|
|
26
|
+
exports.uncurry3 = uncurry3;
|
|
27
|
+
/**
|
|
28
|
+
* Converts a curried 4-argument function into a multi-argument function.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const curriedAdd4 = (a: number) => (b: number) => (c: number) => (d: number) => a + b + c + d;
|
|
33
|
+
* const add4 = uncurry4(curriedAdd4);
|
|
34
|
+
* add4(1, 2, 3, 4); // 10
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
const uncurry4 = (f) => (a, b, c, d) => f(a)(b)(c)(d);
|
|
38
|
+
exports.uncurry4 = uncurry4;
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Arr = void 0;
|
|
4
|
+
const Option_js_1 = require("./Option.js");
|
|
5
|
+
const Result_js_1 = require("./Result.js");
|
|
6
|
+
const NonEmptyList_js_1 = require("../Types/NonEmptyList.js");
|
|
7
|
+
/**
|
|
8
|
+
* Functional array utilities that compose well with pipe.
|
|
9
|
+
* All functions are data-last and curried where applicable.
|
|
10
|
+
* Safe access functions return Option instead of throwing or returning undefined.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* pipe(
|
|
15
|
+
* [1, 2, 3, 4, 5],
|
|
16
|
+
* Arr.filter(n => n > 2),
|
|
17
|
+
* Arr.map(n => n * 10),
|
|
18
|
+
* Arr.head
|
|
19
|
+
* ); // Some(30)
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
var Arr;
|
|
23
|
+
(function (Arr) {
|
|
24
|
+
// --- Safe access ---
|
|
25
|
+
/**
|
|
26
|
+
* Returns the first element of an array, or None if the array is empty.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* Arr.head([1, 2, 3]); // Some(1)
|
|
31
|
+
* Arr.head([]); // None
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
Arr.head = (data) => data.length > 0 ? Option_js_1.Option.toSome(data[0]) : Option_js_1.Option.toNone();
|
|
35
|
+
/**
|
|
36
|
+
* Returns the last element of an array, or None if the array is empty.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* Arr.last([1, 2, 3]); // Some(3)
|
|
41
|
+
* Arr.last([]); // None
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
Arr.last = (data) => data.length > 0 ? Option_js_1.Option.toSome(data[data.length - 1]) : Option_js_1.Option.toNone();
|
|
45
|
+
/**
|
|
46
|
+
* Returns all elements except the first, or None if the array is empty.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* Arr.tail([1, 2, 3]); // Some([2, 3])
|
|
51
|
+
* Arr.tail([]); // None
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
Arr.tail = (data) => data.length > 0 ? Option_js_1.Option.toSome(data.slice(1)) : Option_js_1.Option.toNone();
|
|
55
|
+
/**
|
|
56
|
+
* Returns all elements except the last, or None if the array is empty.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* Arr.init([1, 2, 3]); // Some([1, 2])
|
|
61
|
+
* Arr.init([]); // None
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
Arr.init = (data) => data.length > 0 ? Option_js_1.Option.toSome(data.slice(0, -1)) : Option_js_1.Option.toNone();
|
|
65
|
+
// --- Search ---
|
|
66
|
+
/**
|
|
67
|
+
* Returns the first element matching the predicate, or None.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* pipe([1, 2, 3, 4], Arr.findFirst(n => n > 2)); // Some(3)
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
Arr.findFirst = (predicate) => (data) => {
|
|
75
|
+
const idx = data.findIndex(predicate);
|
|
76
|
+
return idx >= 0 ? Option_js_1.Option.toSome(data[idx]) : Option_js_1.Option.toNone();
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Returns the last element matching the predicate, or None.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* pipe([1, 2, 3, 4], Arr.findLast(n => n > 2)); // Some(4)
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
Arr.findLast = (predicate) => (data) => {
|
|
87
|
+
for (let i = data.length - 1; i >= 0; i--) {
|
|
88
|
+
if (predicate(data[i]))
|
|
89
|
+
return Option_js_1.Option.toSome(data[i]);
|
|
90
|
+
}
|
|
91
|
+
return Option_js_1.Option.toNone();
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Returns the index of the first element matching the predicate, or None.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* pipe([1, 2, 3, 4], Arr.findIndex(n => n > 2)); // Some(2)
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
Arr.findIndex = (predicate) => (data) => {
|
|
102
|
+
const idx = data.findIndex(predicate);
|
|
103
|
+
return idx >= 0 ? Option_js_1.Option.toSome(idx) : Option_js_1.Option.toNone();
|
|
104
|
+
};
|
|
105
|
+
// --- Transform ---
|
|
106
|
+
/**
|
|
107
|
+
* Transforms each element of an array.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* pipe([1, 2, 3], Arr.map(n => n * 2)); // [2, 4, 6]
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
Arr.map = (f) => (data) => data.map(f);
|
|
115
|
+
/**
|
|
116
|
+
* Filters elements that satisfy the predicate.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* pipe([1, 2, 3, 4], Arr.filter(n => n % 2 === 0)); // [2, 4]
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
Arr.filter = (predicate) => (data) => data.filter(predicate);
|
|
124
|
+
/**
|
|
125
|
+
* Splits an array into two groups based on a predicate.
|
|
126
|
+
* First group contains elements that satisfy the predicate,
|
|
127
|
+
* second group contains the rest.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* pipe([1, 2, 3, 4], Arr.partition(n => n % 2 === 0)); // [[2, 4], [1, 3]]
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
Arr.partition = (predicate) => (data) => {
|
|
135
|
+
const pass = [];
|
|
136
|
+
const fail = [];
|
|
137
|
+
for (const a of data) {
|
|
138
|
+
(predicate(a) ? pass : fail).push(a);
|
|
139
|
+
}
|
|
140
|
+
return [pass, fail];
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Groups elements by a key function.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* pipe(
|
|
148
|
+
* ["apple", "avocado", "banana"],
|
|
149
|
+
* Arr.groupBy(s => s[0])
|
|
150
|
+
* ); // { a: ["apple", "avocado"], b: ["banana"] }
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
Arr.groupBy = (f) => (data) => {
|
|
154
|
+
const result = {};
|
|
155
|
+
for (const a of data) {
|
|
156
|
+
const key = f(a);
|
|
157
|
+
if (!result[key])
|
|
158
|
+
result[key] = [];
|
|
159
|
+
result[key].push(a);
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Removes duplicate elements using strict equality.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* Arr.uniq([1, 2, 2, 3, 1]); // [1, 2, 3]
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
Arr.uniq = (data) => [
|
|
172
|
+
...new Set(data),
|
|
173
|
+
];
|
|
174
|
+
/**
|
|
175
|
+
* Removes duplicate elements by comparing the result of a key function.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* pipe(
|
|
180
|
+
* [{id: 1, name: "a"}, {id: 1, name: "b"}, {id: 2, name: "c"}],
|
|
181
|
+
* Arr.uniqBy(x => x.id)
|
|
182
|
+
* ); // [{id: 1, name: "a"}, {id: 2, name: "c"}]
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
Arr.uniqBy = (f) => (data) => {
|
|
186
|
+
const seen = new Set();
|
|
187
|
+
const result = [];
|
|
188
|
+
for (const a of data) {
|
|
189
|
+
const key = f(a);
|
|
190
|
+
if (!seen.has(key)) {
|
|
191
|
+
seen.add(key);
|
|
192
|
+
result.push(a);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Sorts an array using a comparison function. Returns a new array.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* pipe([3, 1, 2], Arr.sortBy((a, b) => a - b)); // [1, 2, 3]
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
Arr.sortBy = (compare) => (data) => [...data].sort(compare);
|
|
206
|
+
// --- Combine ---
|
|
207
|
+
/**
|
|
208
|
+
* Pairs up elements from two arrays. Stops at the shorter array.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* pipe([1, 2, 3], Arr.zip(["a", "b"])); // [[1, "a"], [2, "b"]]
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
Arr.zip = (other) => (data) => {
|
|
216
|
+
const len = Math.min(data.length, other.length);
|
|
217
|
+
const result = [];
|
|
218
|
+
for (let i = 0; i < len; i++) {
|
|
219
|
+
result.push([data[i], other[i]]);
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Combines elements from two arrays using a function. Stops at the shorter array.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* pipe([1, 2], Arr.zipWith((a, b) => a + b, ["a", "b"])); // ["1a", "2b"]
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
Arr.zipWith = (f, other) => (data) => {
|
|
232
|
+
const len = Math.min(data.length, other.length);
|
|
233
|
+
const result = [];
|
|
234
|
+
for (let i = 0; i < len; i++) {
|
|
235
|
+
result.push(f(data[i], other[i]));
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Inserts a separator between every element.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* pipe([1, 2, 3], Arr.intersperse(0)); // [1, 0, 2, 0, 3]
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
Arr.intersperse = (sep) => (data) => {
|
|
248
|
+
if (data.length <= 1)
|
|
249
|
+
return data;
|
|
250
|
+
const result = [data[0]];
|
|
251
|
+
for (let i = 1; i < data.length; i++) {
|
|
252
|
+
result.push(sep, data[i]);
|
|
253
|
+
}
|
|
254
|
+
return result;
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* Splits an array into chunks of the given size.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* pipe([1, 2, 3, 4, 5], Arr.chunksOf(2)); // [[1, 2], [3, 4], [5]]
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
Arr.chunksOf = (n) => (data) => {
|
|
265
|
+
if (n <= 0)
|
|
266
|
+
return [];
|
|
267
|
+
const result = [];
|
|
268
|
+
for (let i = 0; i < data.length; i += n) {
|
|
269
|
+
result.push(data.slice(i, i + n));
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* Flattens a nested array by one level.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* Arr.flatten([[1, 2], [3], [4, 5]]); // [1, 2, 3, 4, 5]
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
Arr.flatten = (data) => [].concat(...data);
|
|
282
|
+
/**
|
|
283
|
+
* Maps each element to an array and flattens the result.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* pipe([1, 2, 3], Arr.flatMap(n => [n, n * 10])); // [1, 10, 2, 20, 3, 30]
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
Arr.flatMap = (f) => (data) => [].concat(...data.map(f));
|
|
291
|
+
/**
|
|
292
|
+
* Reduces an array from the left.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* pipe([1, 2, 3], Arr.reduce(0, (acc, n) => acc + n)); // 6
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
Arr.reduce = (initial, f) => (data) => data.reduce(f, initial);
|
|
300
|
+
// --- Traverse / Sequence ---
|
|
301
|
+
/**
|
|
302
|
+
* Maps each element to an Option and collects the results.
|
|
303
|
+
* Returns None if any mapping returns None.
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* const parseNum = (s: string): Option<number> => {
|
|
308
|
+
* const n = Number(s);
|
|
309
|
+
* return isNaN(n) ? Option.toNone() : Option.of(n);
|
|
310
|
+
* };
|
|
311
|
+
*
|
|
312
|
+
* pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
|
|
313
|
+
* pipe(["1", "x", "3"], Arr.traverse(parseNum)); // None
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
Arr.traverse = (f) => (data) => {
|
|
317
|
+
const result = [];
|
|
318
|
+
for (const a of data) {
|
|
319
|
+
const mapped = f(a);
|
|
320
|
+
if (Option_js_1.Option.isNone(mapped))
|
|
321
|
+
return Option_js_1.Option.toNone();
|
|
322
|
+
result.push(mapped.value);
|
|
323
|
+
}
|
|
324
|
+
return Option_js_1.Option.toSome(result);
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Maps each element to a Result and collects the results.
|
|
328
|
+
* Returns the first Err if any mapping fails.
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* ```ts
|
|
332
|
+
* pipe(
|
|
333
|
+
* [1, 2, 3],
|
|
334
|
+
* Arr.traverseResult(n => n > 0 ? Result.toOk(n) : Result.toErr("negative"))
|
|
335
|
+
* ); // Ok([1, 2, 3])
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
Arr.traverseResult = (f) => (data) => {
|
|
339
|
+
const result = [];
|
|
340
|
+
for (const a of data) {
|
|
341
|
+
const mapped = f(a);
|
|
342
|
+
if (Result_js_1.Result.isErr(mapped))
|
|
343
|
+
return mapped;
|
|
344
|
+
result.push(mapped.value);
|
|
345
|
+
}
|
|
346
|
+
return Result_js_1.Result.toOk(result);
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Maps each element to a Task and runs all in parallel.
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```ts
|
|
353
|
+
* pipe(
|
|
354
|
+
* [1, 2, 3],
|
|
355
|
+
* Arr.traverseTask(n => Task.of(n * 2))
|
|
356
|
+
* )(); // Promise<[2, 4, 6]>
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
Arr.traverseTask = (f) => (data) => () => Promise.all(data.map((a) => f(a)()));
|
|
360
|
+
/**
|
|
361
|
+
* Collects an array of Options into an Option of array.
|
|
362
|
+
* Returns None if any element is None.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* Arr.sequence([Option.of(1), Option.of(2)]); // Some([1, 2])
|
|
367
|
+
* Arr.sequence([Option.of(1), Option.toNone()]); // None
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
Arr.sequence = (data) => Arr.traverse((a) => a)(data);
|
|
371
|
+
/**
|
|
372
|
+
* Collects an array of Results into a Result of array.
|
|
373
|
+
* Returns the first Err if any element is Err.
|
|
374
|
+
*/
|
|
375
|
+
Arr.sequenceResult = (data) => Arr.traverseResult((a) => a)(data);
|
|
376
|
+
/**
|
|
377
|
+
* Collects an array of Tasks into a Task of array. Runs in parallel.
|
|
378
|
+
*/
|
|
379
|
+
Arr.sequenceTask = (data) => Arr.traverseTask((a) => a)(data);
|
|
380
|
+
/**
|
|
381
|
+
* Returns true if the array is non-empty (type guard).
|
|
382
|
+
*/
|
|
383
|
+
Arr.isNonEmpty = (data) => (0, NonEmptyList_js_1.isNonEmptyList)(data);
|
|
384
|
+
/**
|
|
385
|
+
* Returns the length of an array.
|
|
386
|
+
*/
|
|
387
|
+
Arr.size = (data) => data.length;
|
|
388
|
+
/**
|
|
389
|
+
* Returns true if any element satisfies the predicate.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```ts
|
|
393
|
+
* pipe([1, 2, 3], Arr.some(n => n > 2)); // true
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
Arr.some = (predicate) => (data) => data.some(predicate);
|
|
397
|
+
/**
|
|
398
|
+
* Returns true if all elements satisfy the predicate.
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* pipe([1, 2, 3], Arr.every(n => n > 0)); // true
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
Arr.every = (predicate) => (data) => data.every(predicate);
|
|
406
|
+
/**
|
|
407
|
+
* Reverses an array. Returns a new array.
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* ```ts
|
|
411
|
+
* Arr.reverse([1, 2, 3]); // [3, 2, 1]
|
|
412
|
+
* ```
|
|
413
|
+
*/
|
|
414
|
+
Arr.reverse = (data) => [...data].reverse();
|
|
415
|
+
/**
|
|
416
|
+
* Takes the first n elements from an array.
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```ts
|
|
420
|
+
* pipe([1, 2, 3, 4], Arr.take(2)); // [1, 2]
|
|
421
|
+
* ```
|
|
422
|
+
*/
|
|
423
|
+
Arr.take = (n) => (data) => n <= 0 ? [] : data.slice(0, n);
|
|
424
|
+
/**
|
|
425
|
+
* Drops the first n elements from an array.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* ```ts
|
|
429
|
+
* pipe([1, 2, 3, 4], Arr.drop(2)); // [3, 4]
|
|
430
|
+
* ```
|
|
431
|
+
*/
|
|
432
|
+
Arr.drop = (n) => (data) => data.slice(n);
|
|
433
|
+
/**
|
|
434
|
+
* Takes elements from the start while the predicate holds.
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* ```ts
|
|
438
|
+
* pipe([1, 2, 3, 1], Arr.takeWhile(n => n < 3)); // [1, 2]
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
Arr.takeWhile = (predicate) => (data) => {
|
|
442
|
+
const result = [];
|
|
443
|
+
for (const a of data) {
|
|
444
|
+
if (!predicate(a))
|
|
445
|
+
break;
|
|
446
|
+
result.push(a);
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* Drops elements from the start while the predicate holds.
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```ts
|
|
455
|
+
* pipe([1, 2, 3, 1], Arr.dropWhile(n => n < 3)); // [3, 1]
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
Arr.dropWhile = (predicate) => (data) => {
|
|
459
|
+
let i = 0;
|
|
460
|
+
while (i < data.length && predicate(data[i]))
|
|
461
|
+
i++;
|
|
462
|
+
return data.slice(i);
|
|
463
|
+
};
|
|
464
|
+
})(Arr || (exports.Arr = Arr = {}));
|