@satoshibits/functional 1.0.2
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 +242 -0
- package/dist/array-utils.d.mts +317 -0
- package/dist/array-utils.d.mts.map +1 -0
- package/dist/array-utils.mjs +370 -0
- package/dist/array-utils.mjs.map +1 -0
- package/dist/composition.d.mts +603 -0
- package/dist/composition.d.mts.map +1 -0
- package/dist/composition.mjs +516 -0
- package/dist/composition.mjs.map +1 -0
- package/dist/object-utils.d.mts +267 -0
- package/dist/object-utils.d.mts.map +1 -0
- package/dist/object-utils.mjs +258 -0
- package/dist/object-utils.mjs.map +1 -0
- package/dist/option.d.mts +622 -0
- package/dist/option.d.mts.map +1 -0
- package/dist/option.mjs +637 -0
- package/dist/option.mjs.map +1 -0
- package/dist/performance.d.mts +265 -0
- package/dist/performance.d.mts.map +1 -0
- package/dist/performance.mjs +453 -0
- package/dist/performance.mjs.map +1 -0
- package/dist/pipeline.d.mts +431 -0
- package/dist/pipeline.d.mts.map +1 -0
- package/dist/pipeline.mjs +460 -0
- package/dist/pipeline.mjs.map +1 -0
- package/dist/predicates.d.mts +722 -0
- package/dist/predicates.d.mts.map +1 -0
- package/dist/predicates.mjs +802 -0
- package/dist/predicates.mjs.map +1 -0
- package/dist/reader-result.d.mts +422 -0
- package/dist/reader-result.d.mts.map +1 -0
- package/dist/reader-result.mjs +758 -0
- package/dist/reader-result.mjs.map +1 -0
- package/dist/result.d.mts +684 -0
- package/dist/result.d.mts.map +1 -0
- package/dist/result.mjs +814 -0
- package/dist/result.mjs.map +1 -0
- package/dist/types.d.mts +439 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +191 -0
- package/dist/types.mjs.map +1 -0
- package/dist/validation.d.mts +622 -0
- package/dist/validation.d.mts.map +1 -0
- package/dist/validation.mjs +852 -0
- package/dist/validation.mjs.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module pipeline
|
|
4
|
+
* @description A fluent, chainable API for function composition that maintains state.
|
|
5
|
+
* Unlike pure functional composition, Pipeline provides an object-oriented
|
|
6
|
+
* interface for building complex data transformations with method chaining.
|
|
7
|
+
* Combines the benefits of functional programming with familiar OOP patterns.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Pipeline } from './pipeline.mts';
|
|
12
|
+
*
|
|
13
|
+
* // basic transformations
|
|
14
|
+
* const result = Pipeline.of(5)
|
|
15
|
+
* .map(x => x * 2)
|
|
16
|
+
* .map(x => x + 1)
|
|
17
|
+
* .value();
|
|
18
|
+
* // => 11
|
|
19
|
+
*
|
|
20
|
+
* // async transformations
|
|
21
|
+
* const userData = await Pipeline.of('user123')
|
|
22
|
+
* .mapAsync(id => fetchUser(id))
|
|
23
|
+
* .then(p => p.map(user => user.email))
|
|
24
|
+
* .then(p => p.value());
|
|
25
|
+
*
|
|
26
|
+
* // error handling
|
|
27
|
+
* const validated = Pipeline.of(input)
|
|
28
|
+
* .filter(x => x > 0, 'Must be positive')
|
|
29
|
+
* .map(result => result.success ? result.data * 2 : result)
|
|
30
|
+
* .value();
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @category Core
|
|
34
|
+
* @since 2025-07-03
|
|
35
|
+
*/
|
|
36
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
37
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
38
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
39
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
40
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
41
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
42
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
46
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
47
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
48
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
49
|
+
function step(op) {
|
|
50
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
51
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
52
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
53
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
54
|
+
switch (op[0]) {
|
|
55
|
+
case 0: case 1: t = op; break;
|
|
56
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
57
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
58
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
59
|
+
default:
|
|
60
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
61
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
62
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
63
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
64
|
+
if (t[2]) _.ops.pop();
|
|
65
|
+
_.trys.pop(); continue;
|
|
66
|
+
}
|
|
67
|
+
op = body.call(thisArg, _);
|
|
68
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
69
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Pipeline builder for fluent function composition.
|
|
74
|
+
* Provides a stateful, chainable interface for applying transformations
|
|
75
|
+
* to a value. Unlike pure functional composition, Pipeline maintains
|
|
76
|
+
* internal state and offers a familiar object-oriented API.
|
|
77
|
+
*
|
|
78
|
+
* @category Core
|
|
79
|
+
* @example
|
|
80
|
+
* // Basic transformation pipeline
|
|
81
|
+
* const result = Pipeline.of(5)
|
|
82
|
+
* .map(x => x * 2)
|
|
83
|
+
* .map(x => x + 1)
|
|
84
|
+
* .map(x => `Result: ${x}`)
|
|
85
|
+
* .value();
|
|
86
|
+
* // => "Result: 11"
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* // Complex data transformation
|
|
90
|
+
* const userPipeline = Pipeline.of({ name: 'john doe', age: 30 })
|
|
91
|
+
* .map(user => ({ ...user, name: user.name.toUpperCase() }))
|
|
92
|
+
* .tap(user => console.log('Processing:', user.name))
|
|
93
|
+
* .map(user => ({ ...user, ageGroup: user.age >= 18 ? 'adult' : 'minor' }))
|
|
94
|
+
* .value();
|
|
95
|
+
* // => { name: 'JOHN DOE', age: 30, ageGroup: 'adult' }
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* // Error handling with filter
|
|
99
|
+
* const processNumber = (n: number) => Pipeline.of(n)
|
|
100
|
+
* .filter(x => x > 0, 'Number must be positive')
|
|
101
|
+
* .map(result => {
|
|
102
|
+
* if (!result.success) return result;
|
|
103
|
+
* return { success: true as const, data: Math.sqrt(result.data) };
|
|
104
|
+
* })
|
|
105
|
+
* .value();
|
|
106
|
+
*
|
|
107
|
+
* processNumber(16); // => { success: true, data: 4 }
|
|
108
|
+
* processNumber(-4); // => { success: false, error: 'Number must be positive' }
|
|
109
|
+
*/
|
|
110
|
+
var Pipeline = /** @class */ (function () {
|
|
111
|
+
/**
|
|
112
|
+
* Creates a new Pipeline instance with the given value.
|
|
113
|
+
* Use Pipeline.of() for a more functional approach.
|
|
114
|
+
*
|
|
115
|
+
* @private
|
|
116
|
+
*/
|
|
117
|
+
function Pipeline(_value) {
|
|
118
|
+
this._value = _value;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Apply a synchronous transformation.
|
|
122
|
+
* @description Transforms the current value using the provided function.
|
|
123
|
+
* Returns a new Pipeline containing the transformed value.
|
|
124
|
+
*
|
|
125
|
+
* @template U - The type of the transformed value
|
|
126
|
+
* @param {function(T): U} fn - Function to transform the value
|
|
127
|
+
* @returns {Pipeline<U>} A new Pipeline with the transformed value
|
|
128
|
+
*
|
|
129
|
+
* @category Transformation
|
|
130
|
+
* @example
|
|
131
|
+
* const doubled = Pipeline.of(5)
|
|
132
|
+
* .map(x => x * 2)
|
|
133
|
+
* .value();
|
|
134
|
+
* // => 10
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* // Chaining transformations
|
|
138
|
+
* const result = Pipeline.of('hello')
|
|
139
|
+
* .map(s => s.toUpperCase())
|
|
140
|
+
* .map(s => s.split(''))
|
|
141
|
+
* .map(arr => arr.reverse())
|
|
142
|
+
* .map(arr => arr.join(''))
|
|
143
|
+
* .value();
|
|
144
|
+
* // => 'OLLEH'
|
|
145
|
+
*
|
|
146
|
+
* @see mapAsync - Transform with async function
|
|
147
|
+
* @see flatMap - Transform and flatten
|
|
148
|
+
* @since 2025-07-03
|
|
149
|
+
*/
|
|
150
|
+
Pipeline.prototype.map = function (fn) {
|
|
151
|
+
return new Pipeline(fn(this._value));
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Apply an asynchronous transformation.
|
|
155
|
+
* @description Transforms the current value using an async function.
|
|
156
|
+
* Returns a Promise that resolves to a new Pipeline with the transformed value.
|
|
157
|
+
*
|
|
158
|
+
* @template U - The type of the transformed value
|
|
159
|
+
* @param {function(T): Promise<U>} fn - Async function to transform the value
|
|
160
|
+
* @returns {Promise<Pipeline<U>>} Promise resolving to a new Pipeline
|
|
161
|
+
*
|
|
162
|
+
* @category Async
|
|
163
|
+
* @example
|
|
164
|
+
* const userData = await Pipeline.of(123)
|
|
165
|
+
* .mapAsync(async (id) => {
|
|
166
|
+
* const response = await fetch(`/api/users/${id}`);
|
|
167
|
+
* return response.json();
|
|
168
|
+
* })
|
|
169
|
+
* .then(p => p.value());
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* // Chaining async operations
|
|
173
|
+
* const result = await Pipeline.of('user@example.com')
|
|
174
|
+
* .mapAsync(email => fetchUserByEmail(email))
|
|
175
|
+
* .then(p => p.mapAsync(user => fetchUserPosts(user.id)))
|
|
176
|
+
* .then(p => p.map(posts => posts.length))
|
|
177
|
+
* .then(p => p.value());
|
|
178
|
+
* // => number of posts
|
|
179
|
+
*/
|
|
180
|
+
Pipeline.prototype.mapAsync = function (fn) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
182
|
+
var result;
|
|
183
|
+
return __generator(this, function (_a) {
|
|
184
|
+
switch (_a.label) {
|
|
185
|
+
case 0: return [4 /*yield*/, fn(this._value)];
|
|
186
|
+
case 1:
|
|
187
|
+
result = _a.sent();
|
|
188
|
+
return [2 /*return*/, new Pipeline(result)];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* Apply a transformation that might fail.
|
|
195
|
+
* Handles functions that return Result types.
|
|
196
|
+
*
|
|
197
|
+
* @category Error Handling
|
|
198
|
+
* @example
|
|
199
|
+
* const safeDivide = (n: number, divisor: number) =>
|
|
200
|
+
* divisor === 0
|
|
201
|
+
* ? { success: false as const, error: 'Division by zero' }
|
|
202
|
+
* : { success: true as const, data: n / divisor };
|
|
203
|
+
*
|
|
204
|
+
* const result = Pipeline.of(10)
|
|
205
|
+
* .flatMap(n => safeDivide(n, 2))
|
|
206
|
+
* .value();
|
|
207
|
+
* // => { success: true, data: 5 }
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* // Chaining fallible operations
|
|
211
|
+
* const process = Pipeline.of('{"name": "John"}')
|
|
212
|
+
* .flatMap(str => {
|
|
213
|
+
* try {
|
|
214
|
+
* return { success: true as const, data: JSON.parse(str) };
|
|
215
|
+
* } catch {
|
|
216
|
+
* return { success: false as const, error: 'Invalid JSON' };
|
|
217
|
+
* }
|
|
218
|
+
* })
|
|
219
|
+
* .map(result => {
|
|
220
|
+
* if (!result.success) return result;
|
|
221
|
+
* return { success: true as const, data: result.data.name };
|
|
222
|
+
* })
|
|
223
|
+
* .value();
|
|
224
|
+
* // => { success: true, data: 'John' }
|
|
225
|
+
*/
|
|
226
|
+
Pipeline.prototype.flatMap = function (fn) {
|
|
227
|
+
return new Pipeline(fn(this._value));
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Filter the pipeline based on a predicate.
|
|
231
|
+
* Converts the value to a Result type based on the predicate.
|
|
232
|
+
*
|
|
233
|
+
* @category Filtering
|
|
234
|
+
* @example
|
|
235
|
+
* const result = Pipeline.of(5)
|
|
236
|
+
* .filter(x => x > 0, 'Must be positive')
|
|
237
|
+
* .value();
|
|
238
|
+
* // => { success: true, data: 5 }
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* // Validation pipeline
|
|
242
|
+
* const validateUser = (user: any) => Pipeline.of(user)
|
|
243
|
+
* .filter(u => u.name, 'Name is required')
|
|
244
|
+
* .map(result => {
|
|
245
|
+
* if (!result.success) return result;
|
|
246
|
+
* return Pipeline.of(result.data)
|
|
247
|
+
* .filter(u => u.age >= 18, 'Must be 18 or older')
|
|
248
|
+
* .value();
|
|
249
|
+
* })
|
|
250
|
+
* .value();
|
|
251
|
+
*
|
|
252
|
+
* validateUser({ name: 'John', age: 20 }); // => { success: true, data: {...} }
|
|
253
|
+
* validateUser({ name: '', age: 20 }); // => { success: false, error: 'Name is required' }
|
|
254
|
+
*/
|
|
255
|
+
Pipeline.prototype.filter = function (predicate, error) {
|
|
256
|
+
if (predicate(this._value)) {
|
|
257
|
+
return new Pipeline({ success: true, data: this._value });
|
|
258
|
+
}
|
|
259
|
+
return new Pipeline({ success: false, error: error });
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Execute a side effect without changing the value.
|
|
263
|
+
* Useful for logging, debugging, or triggering external actions.
|
|
264
|
+
*
|
|
265
|
+
* @category Side Effects
|
|
266
|
+
* @example
|
|
267
|
+
* const result = Pipeline.of([1, 2, 3, 4, 5])
|
|
268
|
+
* .tap(arr => console.log('Original:', arr))
|
|
269
|
+
* .map(arr => arr.filter(x => x % 2 === 0))
|
|
270
|
+
* .tap(arr => console.log('Filtered:', arr))
|
|
271
|
+
* .map(arr => arr.reduce((a, b) => a + b, 0))
|
|
272
|
+
* .tap(sum => console.log('Sum:', sum))
|
|
273
|
+
* .value();
|
|
274
|
+
* // Logs: Original: [1,2,3,4,5], Filtered: [2,4], Sum: 6
|
|
275
|
+
* // Returns: 6
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* // Analytics tracking
|
|
279
|
+
* const processOrder = Pipeline.of(order)
|
|
280
|
+
* .tap(o => analytics.track('order_started', { orderId: o.id }))
|
|
281
|
+
* .map(o => applyDiscount(o))
|
|
282
|
+
* .tap(o => analytics.track('discount_applied', { amount: o.discount }))
|
|
283
|
+
* .map(o => calculateTax(o))
|
|
284
|
+
* .tap(o => analytics.track('order_completed', { total: o.total }))
|
|
285
|
+
* .value();
|
|
286
|
+
*/
|
|
287
|
+
Pipeline.prototype.tap = function (fn) {
|
|
288
|
+
fn(this._value);
|
|
289
|
+
return this;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Execute an async side effect without changing the value.
|
|
293
|
+
* Useful for async logging, API calls, or other async side effects.
|
|
294
|
+
*
|
|
295
|
+
* @category Async
|
|
296
|
+
* @example
|
|
297
|
+
* const saveUser = await Pipeline.of(userData)
|
|
298
|
+
* .tapAsync(async (user) => {
|
|
299
|
+
* await logToAnalytics('user_created', user);
|
|
300
|
+
* })
|
|
301
|
+
* .then(p => p.tapAsync(async (user) => {
|
|
302
|
+
* await sendWelcomeEmail(user.email);
|
|
303
|
+
* }))
|
|
304
|
+
* .then(p => p.value());
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* // Progress tracking
|
|
308
|
+
* const processLargeFile = await Pipeline.of(file)
|
|
309
|
+
* .tapAsync(f => updateProgress(0))
|
|
310
|
+
* .then(p => p.mapAsync(f => readFile(f)))
|
|
311
|
+
* .then(p => p.tapAsync(_ => updateProgress(33)))
|
|
312
|
+
* .then(p => p.mapAsync(content => parseContent(content)))
|
|
313
|
+
* .then(p => p.tapAsync(_ => updateProgress(66)))
|
|
314
|
+
* .then(p => p.mapAsync(data => saveToDatabase(data)))
|
|
315
|
+
* .then(p => p.tapAsync(_ => updateProgress(100)))
|
|
316
|
+
* .then(p => p.value());
|
|
317
|
+
*/
|
|
318
|
+
Pipeline.prototype.tapAsync = function (fn) {
|
|
319
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
320
|
+
return __generator(this, function (_a) {
|
|
321
|
+
switch (_a.label) {
|
|
322
|
+
case 0: return [4 /*yield*/, fn(this._value)];
|
|
323
|
+
case 1:
|
|
324
|
+
_a.sent();
|
|
325
|
+
return [2 /*return*/, this];
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Get the final value from the pipeline.
|
|
332
|
+
* Extracts the transformed value, ending the pipeline chain.
|
|
333
|
+
*
|
|
334
|
+
* @category Extraction
|
|
335
|
+
* @example
|
|
336
|
+
* const result = Pipeline.of(10)
|
|
337
|
+
* .map(x => x * 2)
|
|
338
|
+
* .map(x => x + 5)
|
|
339
|
+
* .value();
|
|
340
|
+
* // => 25
|
|
341
|
+
*/
|
|
342
|
+
Pipeline.prototype.value = function () {
|
|
343
|
+
return this._value;
|
|
344
|
+
};
|
|
345
|
+
/**
|
|
346
|
+
* Create a new pipeline with the given value.
|
|
347
|
+
* Factory method for creating Pipeline instances.
|
|
348
|
+
*
|
|
349
|
+
* @category Creation
|
|
350
|
+
* @example
|
|
351
|
+
* const pipeline = Pipeline.of(42);
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* // With complex initial values
|
|
355
|
+
* const userPipeline = Pipeline.of({
|
|
356
|
+
* id: 123,
|
|
357
|
+
* name: 'John Doe',
|
|
358
|
+
* email: 'john@example.com'
|
|
359
|
+
* });
|
|
360
|
+
*/
|
|
361
|
+
Pipeline.of = function (value) {
|
|
362
|
+
return new Pipeline(value);
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Apply a function to the pipeline value and return the result.
|
|
366
|
+
* Useful for conditional transformations or extracting values.
|
|
367
|
+
*
|
|
368
|
+
* @category Advanced
|
|
369
|
+
* @example
|
|
370
|
+
* const discount = Pipeline.of(100)
|
|
371
|
+
* .apply(price =>
|
|
372
|
+
* price > 50
|
|
373
|
+
* ? Pipeline.of(price * 0.9) // 10% discount
|
|
374
|
+
* : Pipeline.of(price)
|
|
375
|
+
* )
|
|
376
|
+
* .value();
|
|
377
|
+
* // => 90
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* // Conditional branching
|
|
381
|
+
* const processUser = (user: User) => Pipeline.of(user)
|
|
382
|
+
* .apply(u => {
|
|
383
|
+
* if (u.type === 'premium') {
|
|
384
|
+
* return Pipeline.of(u)
|
|
385
|
+
* .map(u => ({ ...u, benefits: ['ad-free', 'priority-support'] }))
|
|
386
|
+
* .map(u => ({ ...u, discount: 0.2 }));
|
|
387
|
+
* } else {
|
|
388
|
+
* return Pipeline.of(u)
|
|
389
|
+
* .map(u => ({ ...u, benefits: [] }))
|
|
390
|
+
* .map(u => ({ ...u, discount: 0 }));
|
|
391
|
+
* }
|
|
392
|
+
* })
|
|
393
|
+
* .value();
|
|
394
|
+
*/
|
|
395
|
+
Pipeline.prototype.apply = function (fn) {
|
|
396
|
+
return fn(this._value);
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* Combine two pipelines using a binary function.
|
|
400
|
+
* Useful for merging results from multiple pipelines.
|
|
401
|
+
*
|
|
402
|
+
* @category Advanced
|
|
403
|
+
* @example
|
|
404
|
+
* const pipeline1 = Pipeline.of(5);
|
|
405
|
+
* const pipeline2 = Pipeline.of(3);
|
|
406
|
+
*
|
|
407
|
+
* const sum = pipeline1
|
|
408
|
+
* .combine(pipeline2, (a, b) => a + b)
|
|
409
|
+
* .value();
|
|
410
|
+
* // => 8
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* // Combining user data
|
|
414
|
+
* const userPipeline = Pipeline.of({ id: 1, name: 'John' });
|
|
415
|
+
* const preferencesPipeline = Pipeline.of({ theme: 'dark', lang: 'en' });
|
|
416
|
+
*
|
|
417
|
+
* const completeUser = userPipeline
|
|
418
|
+
* .combine(preferencesPipeline, (user, prefs) => ({
|
|
419
|
+
* ...user,
|
|
420
|
+
* preferences: prefs
|
|
421
|
+
* }))
|
|
422
|
+
* .value();
|
|
423
|
+
* // => { id: 1, name: 'John', preferences: { theme: 'dark', lang: 'en' } }
|
|
424
|
+
*/
|
|
425
|
+
Pipeline.prototype.combine = function (other, fn) {
|
|
426
|
+
return new Pipeline(fn(this._value, other._value));
|
|
427
|
+
};
|
|
428
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
429
|
+
Pipeline.prototype.pipeAsync = function () {
|
|
430
|
+
var fns = [];
|
|
431
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
432
|
+
fns[_i] = arguments[_i];
|
|
433
|
+
}
|
|
434
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
435
|
+
var result, _a, fns_1, fn;
|
|
436
|
+
return __generator(this, function (_b) {
|
|
437
|
+
switch (_b.label) {
|
|
438
|
+
case 0:
|
|
439
|
+
result = this._value;
|
|
440
|
+
_a = 0, fns_1 = fns;
|
|
441
|
+
_b.label = 1;
|
|
442
|
+
case 1:
|
|
443
|
+
if (!(_a < fns_1.length)) return [3 /*break*/, 4];
|
|
444
|
+
fn = fns_1[_a];
|
|
445
|
+
return [4 /*yield*/, fn(result)];
|
|
446
|
+
case 2:
|
|
447
|
+
result = _b.sent();
|
|
448
|
+
_b.label = 3;
|
|
449
|
+
case 3:
|
|
450
|
+
_a++;
|
|
451
|
+
return [3 /*break*/, 1];
|
|
452
|
+
case 4: return [2 /*return*/, result];
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
return Pipeline;
|
|
458
|
+
}());
|
|
459
|
+
export { Pipeline };
|
|
460
|
+
//# sourceMappingURL=pipeline.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.mjs","sourceRoot":"","sources":["../src/pipeline.mts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH;IACE;;;;;OAKG;IACH,kBAA6B,MAAS;QAAT,WAAM,GAAN,MAAM,CAAG;IAAG,CAAC;IAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACH,sBAAG,GAAH,UAAO,EAAmB;QACxB,OAAO,IAAI,QAAQ,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACG,2BAAQ,GAAd,UAAkB,EAA4B;;;;;4BAC7B,qBAAM,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,EAAA;;wBAA9B,MAAM,GAAG,SAAqB;wBACpC,sBAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAC;;;;KAC7B;IAED;;;;;;;;;;;;;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