@satoshibits/functional 1.0.3 → 1.1.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/dist/array-utils.d.mts.map +1 -1
- package/dist/array-utils.mjs +6 -3
- package/dist/array-utils.mjs.map +1 -1
- package/dist/composition.d.mts +40 -6
- package/dist/composition.d.mts.map +1 -1
- package/dist/composition.mjs +98 -22
- package/dist/composition.mjs.map +1 -1
- package/dist/index.d.mts +10 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +10 -5
- package/dist/index.mjs.map +1 -1
- package/dist/io.d.mts +326 -0
- package/dist/io.d.mts.map +1 -0
- package/dist/io.mjs +377 -0
- package/dist/io.mjs.map +1 -0
- package/dist/option.d.mts +182 -55
- package/dist/option.d.mts.map +1 -1
- package/dist/option.mjs +251 -60
- package/dist/option.mjs.map +1 -1
- package/dist/performance.d.mts +1 -1
- package/dist/performance.d.mts.map +1 -1
- package/dist/performance.mjs +16 -11
- package/dist/performance.mjs.map +1 -1
- package/dist/reader.d.mts +431 -0
- package/dist/reader.d.mts.map +1 -0
- package/dist/reader.mjs +444 -0
- package/dist/reader.mjs.map +1 -0
- package/dist/result.d.mts +196 -0
- package/dist/result.d.mts.map +1 -1
- package/dist/result.mjs +275 -0
- package/dist/result.mjs.map +1 -1
- package/dist/task.d.mts +361 -0
- package/dist/task.d.mts.map +1 -0
- package/dist/task.mjs +373 -0
- package/dist/task.mjs.map +1 -0
- package/dist/validation.d.mts.map +1 -1
- package/dist/validation.mjs +10 -4
- package/dist/validation.mjs.map +1 -1
- package/package.json +1 -1
package/dist/result.mjs
CHANGED
|
@@ -37,6 +37,17 @@
|
|
|
37
37
|
* @category Core
|
|
38
38
|
* @since 2025-07-03
|
|
39
39
|
*/
|
|
40
|
+
var __assign = (this && this.__assign) || function () {
|
|
41
|
+
__assign = Object.assign || function(t) {
|
|
42
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
43
|
+
s = arguments[i];
|
|
44
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
45
|
+
t[p] = s[p];
|
|
46
|
+
}
|
|
47
|
+
return t;
|
|
48
|
+
};
|
|
49
|
+
return __assign.apply(this, arguments);
|
|
50
|
+
};
|
|
40
51
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
41
52
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
42
53
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -667,6 +678,270 @@ export var Result = {
|
|
|
667
678
|
}
|
|
668
679
|
};
|
|
669
680
|
},
|
|
681
|
+
/**
|
|
682
|
+
* Maps a function returning a Result over an array and sequences the results.
|
|
683
|
+
* @description Applies a Result-returning function to each element of an array
|
|
684
|
+
* and collects all results. If any operation fails, returns the first error.
|
|
685
|
+
*
|
|
686
|
+
* @template T - The input type
|
|
687
|
+
* @template U - The output type
|
|
688
|
+
* @template E - The error type
|
|
689
|
+
* @param {function(T): Result<U, E>} f - Function that returns a Result
|
|
690
|
+
* @returns {function(T[]): Result<U[], E>} A function that traverses arrays with Results
|
|
691
|
+
*
|
|
692
|
+
* @category Combinators
|
|
693
|
+
* @example
|
|
694
|
+
* ```typescript
|
|
695
|
+
* const parseNumber = (s: string): Result<number, string> => {
|
|
696
|
+
* const n = Number(s);
|
|
697
|
+
* return isNaN(n) ? Result.err(`Not a number: ${s}`) : Result.ok(n);
|
|
698
|
+
* };
|
|
699
|
+
*
|
|
700
|
+
* const parseAll = Result.traverse(parseNumber);
|
|
701
|
+
* parseAll(['1', '2', '3']); // => { success: true, data: [1, 2, 3] }
|
|
702
|
+
* parseAll(['1', 'x', '3']); // => { success: false, error: 'Not a number: x' }
|
|
703
|
+
* ```
|
|
704
|
+
*
|
|
705
|
+
* @since 2025-09-18
|
|
706
|
+
*/
|
|
707
|
+
traverse: function (f) { return function (ts) {
|
|
708
|
+
var results = [];
|
|
709
|
+
for (var _i = 0, ts_1 = ts; _i < ts_1.length; _i++) {
|
|
710
|
+
var t = ts_1[_i];
|
|
711
|
+
var result = f(t);
|
|
712
|
+
if (!result.success) {
|
|
713
|
+
return result;
|
|
714
|
+
}
|
|
715
|
+
results.push(result.data);
|
|
716
|
+
}
|
|
717
|
+
return Result.ok(results);
|
|
718
|
+
}; },
|
|
719
|
+
/**
|
|
720
|
+
* Applies a Result of a function to a Result of a value.
|
|
721
|
+
* @description Enables applying functions wrapped in Results to values wrapped
|
|
722
|
+
* in Results. If either Result is an error, returns that error. This is the
|
|
723
|
+
* applicative apply operation for Result types.
|
|
724
|
+
*
|
|
725
|
+
* @template T - The input type
|
|
726
|
+
* @template U - The output type
|
|
727
|
+
* @template E - The error type
|
|
728
|
+
* @param {Result<T, E>} resultValue - Result containing a value
|
|
729
|
+
* @returns {function(Result<function(T): U, E>): Result<U, E>} A function that applies Result functions
|
|
730
|
+
*
|
|
731
|
+
* @category Combinators
|
|
732
|
+
* @example
|
|
733
|
+
* ```typescript
|
|
734
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
735
|
+
* const resultAdd = Result.ok(add);
|
|
736
|
+
* const result5 = Result.ok(5);
|
|
737
|
+
* const result3 = Result.ok(3);
|
|
738
|
+
*
|
|
739
|
+
* const sum = Result.ap(result3)(
|
|
740
|
+
* Result.ap(result5)(
|
|
741
|
+
* Result.map(add)(Result.ok(10))
|
|
742
|
+
* )
|
|
743
|
+
* ); // => { success: true, data: 18 }
|
|
744
|
+
* ```
|
|
745
|
+
*
|
|
746
|
+
* @since 2025-09-18
|
|
747
|
+
*/
|
|
748
|
+
ap: function (resultValue) { return function (resultFn) {
|
|
749
|
+
if (!resultFn.success) {
|
|
750
|
+
return resultFn;
|
|
751
|
+
}
|
|
752
|
+
if (!resultValue.success) {
|
|
753
|
+
return resultValue;
|
|
754
|
+
}
|
|
755
|
+
return Result.ok(resultFn.data(resultValue.data));
|
|
756
|
+
}; },
|
|
757
|
+
/**
|
|
758
|
+
* Maps both the success and error values of a Result.
|
|
759
|
+
* @description Transforms both channels of a Result simultaneously. Useful for
|
|
760
|
+
* normalizing both success values and errors in a single operation.
|
|
761
|
+
*
|
|
762
|
+
* @template T - The input success type
|
|
763
|
+
* @template U - The output success type
|
|
764
|
+
* @template E - The input error type
|
|
765
|
+
* @template F - The output error type
|
|
766
|
+
* @param {function(T): U} f - Function to transform the success value
|
|
767
|
+
* @param {function(E): F} g - Function to transform the error value
|
|
768
|
+
* @returns {function(Result<T, E>): Result<U, F>} A function that transforms both channels
|
|
769
|
+
*
|
|
770
|
+
* @category Transformations
|
|
771
|
+
* @example
|
|
772
|
+
* ```typescript
|
|
773
|
+
* const transform = Result.bimap(
|
|
774
|
+
* (n: number) => n.toString(),
|
|
775
|
+
* (e: Error) => e.message
|
|
776
|
+
* );
|
|
777
|
+
*
|
|
778
|
+
* transform(Result.ok(42)); // => { success: true, data: '42' }
|
|
779
|
+
* transform(Result.err(new Error('fail'))); // => { success: false, error: 'fail' }
|
|
780
|
+
* ```
|
|
781
|
+
*
|
|
782
|
+
* @since 2025-09-18
|
|
783
|
+
*/
|
|
784
|
+
bimap: function (f, g) { return function (result) {
|
|
785
|
+
return result.success ? Result.ok(f(result.data)) : Result.err(g(result.error));
|
|
786
|
+
}; },
|
|
787
|
+
/**
|
|
788
|
+
* Executes a Result for its side effects, discarding the result.
|
|
789
|
+
* @description Runs a Result-returning function but preserves the original value.
|
|
790
|
+
* Useful for logging or other side effects where the result isn't needed.
|
|
791
|
+
*
|
|
792
|
+
* @template T - The type of the value
|
|
793
|
+
* @template E - The error type
|
|
794
|
+
* @param {function(T): Result<any, E>} f - Function that returns a Result (result discarded)
|
|
795
|
+
* @returns {function(Result<T, E>): Result<T, E>} A function that executes side effects
|
|
796
|
+
*
|
|
797
|
+
* @category Combinators
|
|
798
|
+
* @example
|
|
799
|
+
* ```typescript
|
|
800
|
+
* const log = (msg: string): Result<void, never> => {
|
|
801
|
+
* console.log(msg);
|
|
802
|
+
* return Result.ok(undefined);
|
|
803
|
+
* };
|
|
804
|
+
*
|
|
805
|
+
* const result = Result.chainFirst((n: number) => log(`Got: ${n}`))(
|
|
806
|
+
* Result.ok(42)
|
|
807
|
+
* ); // logs "Got: 42", returns { success: true, data: 42 }
|
|
808
|
+
* ```
|
|
809
|
+
*
|
|
810
|
+
* @since 2025-09-18
|
|
811
|
+
*/
|
|
812
|
+
chainFirst: function (f) { return function (result) {
|
|
813
|
+
if (!result.success) {
|
|
814
|
+
return result;
|
|
815
|
+
}
|
|
816
|
+
var sideEffect = f(result.data);
|
|
817
|
+
if (!sideEffect.success) {
|
|
818
|
+
return sideEffect;
|
|
819
|
+
}
|
|
820
|
+
return result;
|
|
821
|
+
}; },
|
|
822
|
+
/**
|
|
823
|
+
* Combines the results of a tuple of Results into a Result of a tuple.
|
|
824
|
+
* @description Takes multiple Results and returns a Result containing a tuple
|
|
825
|
+
* of their values. If any Result is an error, returns the first error.
|
|
826
|
+
*
|
|
827
|
+
* @template T - Tuple type of Results
|
|
828
|
+
* @param {...T} results - Results to combine
|
|
829
|
+
* @returns {Result<{ [K in keyof T]: T[K] extends Result<infer U, any> ? U : never }, T[number] extends Result<any, infer E> ? E : never>} Result of tuple
|
|
830
|
+
*
|
|
831
|
+
* @category Combinators
|
|
832
|
+
* @example
|
|
833
|
+
* ```typescript
|
|
834
|
+
* const result1 = Result.ok(1);
|
|
835
|
+
* const result2 = Result.ok('hello');
|
|
836
|
+
* const result3 = Result.ok(true);
|
|
837
|
+
*
|
|
838
|
+
* const combined = Result.sequenceT(result1, result2, result3);
|
|
839
|
+
* // => { success: true, data: [1, 'hello', true] }
|
|
840
|
+
* ```
|
|
841
|
+
*
|
|
842
|
+
* @since 2025-09-18
|
|
843
|
+
*/
|
|
844
|
+
sequenceT: function () {
|
|
845
|
+
var results = [];
|
|
846
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
847
|
+
results[_i] = arguments[_i];
|
|
848
|
+
}
|
|
849
|
+
var values = [];
|
|
850
|
+
for (var _a = 0, results_3 = results; _a < results_3.length; _a++) {
|
|
851
|
+
var result = results_3[_a];
|
|
852
|
+
if (!result.success) {
|
|
853
|
+
return result;
|
|
854
|
+
}
|
|
855
|
+
values.push(result.data);
|
|
856
|
+
}
|
|
857
|
+
return Result.ok(values);
|
|
858
|
+
},
|
|
859
|
+
/**
|
|
860
|
+
* Combines the results of a record of Results into a Result of a record.
|
|
861
|
+
* @description Takes an object with Result values and returns a Result containing
|
|
862
|
+
* an object with the unwrapped values. If any Result is an error, returns the first error.
|
|
863
|
+
*
|
|
864
|
+
* @template R - Record type with Result values
|
|
865
|
+
* @param {R} results - Record of Results to combine
|
|
866
|
+
* @returns {Result<{ [K in keyof R]: R[K] extends Result<infer U, any> ? U : never }, R[keyof R] extends Result<any, infer E> ? E : never>} Result of record
|
|
867
|
+
*
|
|
868
|
+
* @category Combinators
|
|
869
|
+
* @example
|
|
870
|
+
* ```typescript
|
|
871
|
+
* const results = {
|
|
872
|
+
* user: Result.ok({ name: 'Alice' }),
|
|
873
|
+
* count: Result.ok(42),
|
|
874
|
+
* enabled: Result.ok(true)
|
|
875
|
+
* };
|
|
876
|
+
*
|
|
877
|
+
* const combined = Result.sequenceS(results);
|
|
878
|
+
* // => { success: true, data: { user: { name: 'Alice' }, count: 42, enabled: true } }
|
|
879
|
+
* ```
|
|
880
|
+
*
|
|
881
|
+
* @since 2025-09-18
|
|
882
|
+
*/
|
|
883
|
+
sequenceS: function (results) {
|
|
884
|
+
var keys = Object.keys(results);
|
|
885
|
+
var values = {};
|
|
886
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
887
|
+
var key = keys_1[_i];
|
|
888
|
+
var result = results[key];
|
|
889
|
+
if (!result.success) {
|
|
890
|
+
return result;
|
|
891
|
+
}
|
|
892
|
+
values[key] = result.data;
|
|
893
|
+
}
|
|
894
|
+
return Result.ok(values);
|
|
895
|
+
},
|
|
896
|
+
/**
|
|
897
|
+
* Do notation helper for Result types.
|
|
898
|
+
* @description Provides a way to write sequential Result operations in an
|
|
899
|
+
* imperative style, similar to async/await but for Result types.
|
|
900
|
+
*
|
|
901
|
+
* @template E - The error type
|
|
902
|
+
* @returns {object} Do notation builder
|
|
903
|
+
*
|
|
904
|
+
* @category Do Notation
|
|
905
|
+
* @example
|
|
906
|
+
* ```typescript
|
|
907
|
+
* const result = Result.Do<string>()
|
|
908
|
+
* .bind('x', Result.ok(5))
|
|
909
|
+
* .bind('y', Result.ok(3))
|
|
910
|
+
* .map(({ x, y }) => x + y);
|
|
911
|
+
* // => { success: true, data: 8 }
|
|
912
|
+
*
|
|
913
|
+
* const withError = Result.Do<string>()
|
|
914
|
+
* .bind('x', Result.ok(5))
|
|
915
|
+
* .bind('y', Result.err<number, string>('error'))
|
|
916
|
+
* .map(({ x, y }) => x + y);
|
|
917
|
+
* // => { success: false, error: 'error' }
|
|
918
|
+
* ```
|
|
919
|
+
*
|
|
920
|
+
* @since 2025-09-18
|
|
921
|
+
*/
|
|
922
|
+
Do: function () {
|
|
923
|
+
var createBuilder = function (context) { return ({
|
|
924
|
+
bind: function (key, result) {
|
|
925
|
+
var _a;
|
|
926
|
+
if (!context.success) {
|
|
927
|
+
return createBuilder(context);
|
|
928
|
+
}
|
|
929
|
+
var actualResult = typeof result === 'function' ? result(context.data) : result;
|
|
930
|
+
if (!actualResult.success) {
|
|
931
|
+
return createBuilder(actualResult);
|
|
932
|
+
}
|
|
933
|
+
return createBuilder(Result.ok(__assign(__assign({}, context.data), (_a = {}, _a[key] = actualResult.data, _a))));
|
|
934
|
+
},
|
|
935
|
+
map: function (f) {
|
|
936
|
+
return context.success ? Result.ok(f(context.data)) : context;
|
|
937
|
+
},
|
|
938
|
+
flatMap: function (f) {
|
|
939
|
+
return context.success ? f(context.data) : context;
|
|
940
|
+
},
|
|
941
|
+
value: function () { return context; },
|
|
942
|
+
}); };
|
|
943
|
+
return createBuilder(Result.ok({}));
|
|
944
|
+
},
|
|
670
945
|
};
|
|
671
946
|
/**
|
|
672
947
|
* Utility type guard for checking if a value is a Result.
|
package/dist/result.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.mjs","sourceRoot":"","sources":["../src/result.mts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG
|
|
1
|
+
{"version":3,"file":"result.mjs","sourceRoot":"","sources":["../src/result.mts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDH;;;;;;;GAOG;AACH,MAAM,CAAC,IAAM,MAAM,GAAG;IACpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,EAAE,EAAE,UAAe,IAAO,IAAmB,OAAA,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,MAAA,EAAE,CAAC,EAAzB,CAAyB;IAEtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACH,GAAG,EAAE,UAAwB,KAAQ,IAAmB,OAAA,CAAC;QACvD,OAAO,EAAE,KAAK;QACd,KAAK,OAAA;KACN,CAAC,EAHsD,CAGtD;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,GAAG,EACD,UAAU,CAAiB;QAC3B,OAAA,UAAC,MAAoB;YACnB,OAAA,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,MAAsB;QAAnE,CAAmE;IADrE,CACqE;IAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,OAAO,EACL,UAAU,CAA4B;QACtC,OAAA,UAAC,MAAoB;YACnB,OAAA,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CA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AAC;IAED,IAAM,YAAY,GAAI,MAAuC,CAAC,KAAK,YAAY,KAAK;QAClF,CAAC,CAAG,MAAuC,CAAC,KAAe,CAAC,OAAO;QACnE,CAAC,CAAC,MAAM,CAAE,MAAuC,CAAC,KAAK,CAAC,CAAC;IAE3D,MAAM,IAAI,KAAK,CACb,6CAAsC,YAAY,CAAE,CACrD,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,CAAC,IAAM,IAAI,GACf,UAAO,MAAqB,EAAE,YAAuC;IAAvC,6BAAA,EAAA,uCAAuC;IACrE,OAAA,UAAC,GAAM;QACL,IAAI,CAAC;YACH,IAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YAC1B,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBAC1C,OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;YAClC,CAAC;YACD,OAAO,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;QAAC,WAAM,CAAC;YACP,OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;AAVD,CAUC,CAAC"}
|
package/dist/task.d.mts
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module task
|
|
3
|
+
* @description Task represents a lazy asynchronous computation.
|
|
4
|
+
* A Task is simply a thunk that returns a Promise. This provides a lazy, composable
|
|
5
|
+
* wrapper around async operations, ensuring referential transparency and making
|
|
6
|
+
* async code more predictable and testable. Note that Tasks can reject - for
|
|
7
|
+
* explicit error handling, consider using Task<Result<T, E>> pattern.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Task } from './task.mts';
|
|
12
|
+
*
|
|
13
|
+
* // basic usage
|
|
14
|
+
* const readFile = (path: string): Task<string> =>
|
|
15
|
+
* () => fs.promises.readFile(path, 'utf-8');
|
|
16
|
+
*
|
|
17
|
+
* // composing tasks
|
|
18
|
+
* const processFile = Task.chain((content: string) =>
|
|
19
|
+
* Task.of(content.toUpperCase())
|
|
20
|
+
* )(readFile('data.txt'));
|
|
21
|
+
*
|
|
22
|
+
* // running tasks
|
|
23
|
+
* const result = await Task.run(processFile);
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @category Core
|
|
27
|
+
* @since 2025-09-18
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Task type representing a lazy async computation that always succeeds.
|
|
31
|
+
* @description A Task is a function that returns a Promise. This lazy evaluation
|
|
32
|
+
* allows for composition and transformation before execution, making async code
|
|
33
|
+
* more predictable and easier to test.
|
|
34
|
+
*
|
|
35
|
+
* @template A - The type of the value the task will produce
|
|
36
|
+
*
|
|
37
|
+
* @category Types
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Simple task
|
|
41
|
+
* const delay: Task<void> = () => new Promise(resolve =>
|
|
42
|
+
* setTimeout(resolve, 1000)
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* // Task returning a value
|
|
46
|
+
* const fetchUser: Task<User> = () =>
|
|
47
|
+
* fetch('/api/user').then(r => r.json());
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @since 2025-09-18
|
|
51
|
+
*/
|
|
52
|
+
export type Task<A> = () => Promise<A>;
|
|
53
|
+
/**
|
|
54
|
+
* Task utility functions for working with Task types.
|
|
55
|
+
* @description Provides a functional API for creating, transforming, and composing Tasks.
|
|
56
|
+
* All functions are curried to support functional composition and partial application.
|
|
57
|
+
*
|
|
58
|
+
* @category Utilities
|
|
59
|
+
* @since 2025-09-18
|
|
60
|
+
*/
|
|
61
|
+
export declare const Task: {
|
|
62
|
+
/**
|
|
63
|
+
* Creates a Task that immediately resolves with the given value.
|
|
64
|
+
* @description Factory function for creating tasks from values. The resulting
|
|
65
|
+
* task will always succeed with the provided value.
|
|
66
|
+
*
|
|
67
|
+
* @template A - The type of the value
|
|
68
|
+
* @param {A} value - The value to wrap in a Task
|
|
69
|
+
* @returns {Task<A>} A Task that resolves to the value
|
|
70
|
+
*
|
|
71
|
+
* @category Constructors
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const task = Task.of(42);
|
|
75
|
+
* await Task.run(task); // => 42
|
|
76
|
+
*
|
|
77
|
+
* // Useful for starting chains
|
|
78
|
+
* const result = await Task.run(
|
|
79
|
+
* Task.chain((n: number) => Task.of(n * 2))(
|
|
80
|
+
* Task.of(21)
|
|
81
|
+
* )
|
|
82
|
+
* ); // => 42
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @since 2025-09-18
|
|
86
|
+
*/
|
|
87
|
+
of: <A>(value: A) => Task<A>;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a Task from a Promise.
|
|
90
|
+
* @description Wraps a Promise in a Task, making it lazy. The Promise
|
|
91
|
+
* won't execute until the Task is run.
|
|
92
|
+
*
|
|
93
|
+
* @template A - The type of the value the Promise resolves to
|
|
94
|
+
* @param {() => Promise<A>} f - A function that returns a Promise
|
|
95
|
+
* @returns {Task<A>} A Task wrapping the Promise
|
|
96
|
+
*
|
|
97
|
+
* @category Constructors
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const fetchData = Task.fromPromise(() =>
|
|
101
|
+
* fetch('/api/data').then(r => r.json())
|
|
102
|
+
* );
|
|
103
|
+
*
|
|
104
|
+
* // Promise doesn't execute until run
|
|
105
|
+
* const result = await Task.run(fetchData);
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @since 2025-09-18
|
|
109
|
+
*/
|
|
110
|
+
fromPromise: <A>(f: () => Promise<A>) => Task<A>;
|
|
111
|
+
/**
|
|
112
|
+
* Transforms the value inside a Task using the given function.
|
|
113
|
+
* @description Applies a pure function to the eventual value of a Task,
|
|
114
|
+
* creating a new Task with the transformed value. This is the functor
|
|
115
|
+
* map operation for Task types.
|
|
116
|
+
*
|
|
117
|
+
* @template A - The input type
|
|
118
|
+
* @template B - The output type
|
|
119
|
+
* @param {function(A): B} f - Function to transform the value
|
|
120
|
+
* @returns {function(Task<A>): Task<B>} A function that transforms Tasks
|
|
121
|
+
*
|
|
122
|
+
* @category Transformations
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const double = Task.map((n: number) => n * 2);
|
|
126
|
+
* const task = Task.of(21);
|
|
127
|
+
* const doubled = double(task);
|
|
128
|
+
* await Task.run(doubled); // => 42
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @since 2025-09-18
|
|
132
|
+
*/
|
|
133
|
+
map: <A, B>(f: (value: A) => B) => (task: Task<A>) => Task<B>;
|
|
134
|
+
/**
|
|
135
|
+
* Chains Task-returning operations.
|
|
136
|
+
* @description Sequences two Tasks where the second depends on the result
|
|
137
|
+
* of the first. This is the monadic bind operation for Task types.
|
|
138
|
+
*
|
|
139
|
+
* @template A - The input type
|
|
140
|
+
* @template B - The output type
|
|
141
|
+
* @param {function(A): Task<B>} f - Function that returns a new Task
|
|
142
|
+
* @returns {function(Task<A>): Task<B>} A function that chains Tasks
|
|
143
|
+
*
|
|
144
|
+
* @category Combinators
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const readFile = (path: string): Task<string> =>
|
|
148
|
+
* () => fs.promises.readFile(path, 'utf-8');
|
|
149
|
+
*
|
|
150
|
+
* const parseJson = <T>(content: string): Task<T> =>
|
|
151
|
+
* Task.of(JSON.parse(content));
|
|
152
|
+
*
|
|
153
|
+
* const loadConfig = Task.chain(parseJson<Config>)(
|
|
154
|
+
* readFile('config.json')
|
|
155
|
+
* );
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* @since 2025-09-18
|
|
159
|
+
*/
|
|
160
|
+
chain: <A, B>(f: (value: A) => Task<B>) => (task: Task<A>) => Task<B>;
|
|
161
|
+
/**
|
|
162
|
+
* Alias for chain to match fp-ts naming.
|
|
163
|
+
* @description See {@link chain} for details.
|
|
164
|
+
*
|
|
165
|
+
* @category Combinators
|
|
166
|
+
* @since 2025-09-18
|
|
167
|
+
*/
|
|
168
|
+
flatMap: <A, B>(f: (value: A) => Task<B>) => (task: Task<A>) => Task<B>;
|
|
169
|
+
/**
|
|
170
|
+
* Applies a Task of a function to a Task of a value.
|
|
171
|
+
* @description Enables applying functions wrapped in Tasks to values wrapped
|
|
172
|
+
* in Tasks. This is the applicative apply operation for Task types.
|
|
173
|
+
*
|
|
174
|
+
* @template A - The input type
|
|
175
|
+
* @template B - The output type
|
|
176
|
+
* @param {Task<A>} taskValue - Task containing a value
|
|
177
|
+
* @returns {function(Task<function(A): B>): Task<B>} A function that applies Task functions
|
|
178
|
+
*
|
|
179
|
+
* @category Combinators
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
183
|
+
* const taskAdd = Task.of(add);
|
|
184
|
+
* const task5 = Task.of(5);
|
|
185
|
+
* const task3 = Task.of(3);
|
|
186
|
+
*
|
|
187
|
+
* const result = await Task.run(
|
|
188
|
+
* Task.ap(task3)(
|
|
189
|
+
* Task.ap(task5)(
|
|
190
|
+
* Task.map(add)(Task.of(10))
|
|
191
|
+
* )
|
|
192
|
+
* )
|
|
193
|
+
* ); // => 18
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* @since 2025-09-18
|
|
197
|
+
*/
|
|
198
|
+
ap: <A, B>(taskValue: Task<A>) => (taskFn: Task<(a: A) => B>) => Task<B>;
|
|
199
|
+
/**
|
|
200
|
+
* Runs a Task to completion and returns its Promise.
|
|
201
|
+
* @description Executes a Task, triggering the async computation and
|
|
202
|
+
* returning the resulting Promise.
|
|
203
|
+
*
|
|
204
|
+
* @template A - The type of the value
|
|
205
|
+
* @param {Task<A>} task - The Task to run
|
|
206
|
+
* @returns {Promise<A>} The result of running the Task
|
|
207
|
+
*
|
|
208
|
+
* @category Execution
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* const task = Task.of(42);
|
|
212
|
+
* const result = await Task.run(task); // => 42
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @since 2025-09-18
|
|
216
|
+
*/
|
|
217
|
+
run: <A>(task: Task<A>) => Promise<A>;
|
|
218
|
+
/**
|
|
219
|
+
* Converts an array of Tasks into a Task of an array.
|
|
220
|
+
* @description Runs all Tasks in parallel and collects their results.
|
|
221
|
+
* All Tasks must succeed for the resulting Task to succeed.
|
|
222
|
+
*
|
|
223
|
+
* @template A - The type of values in the Tasks
|
|
224
|
+
* @param {Task<A>[]} tasks - Array of Tasks to sequence
|
|
225
|
+
* @returns {Task<A[]>} A Task containing an array of results
|
|
226
|
+
*
|
|
227
|
+
* @category Combinators
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* const tasks = [
|
|
231
|
+
* Task.of(1),
|
|
232
|
+
* Task.of(2),
|
|
233
|
+
* Task.of(3)
|
|
234
|
+
* ];
|
|
235
|
+
* const combined = Task.sequence(tasks);
|
|
236
|
+
* await Task.run(combined); // => [1, 2, 3]
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* @since 2025-09-18
|
|
240
|
+
*/
|
|
241
|
+
sequence: <A>(tasks: Task<A>[]) => Task<A[]>;
|
|
242
|
+
/**
|
|
243
|
+
* Maps a function returning a Task over an array and sequences the results.
|
|
244
|
+
* @description Applies a Task-returning function to each element of an array
|
|
245
|
+
* and runs all resulting Tasks in parallel.
|
|
246
|
+
*
|
|
247
|
+
* @template A - The input type
|
|
248
|
+
* @template B - The output type
|
|
249
|
+
* @param {function(A): Task<B>} f - Function that returns a Task
|
|
250
|
+
* @returns {function(A[]): Task<B[]>} A function that traverses arrays with Tasks
|
|
251
|
+
*
|
|
252
|
+
* @category Combinators
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* const fetchUser = (id: string): Task<User> =>
|
|
256
|
+
* () => fetch(`/api/users/${id}`).then(r => r.json());
|
|
257
|
+
*
|
|
258
|
+
* const fetchAllUsers = Task.traverse(fetchUser);
|
|
259
|
+
* const users = await Task.run(
|
|
260
|
+
* fetchAllUsers(['1', '2', '3'])
|
|
261
|
+
* );
|
|
262
|
+
* ```
|
|
263
|
+
*
|
|
264
|
+
* @since 2025-09-18
|
|
265
|
+
*/
|
|
266
|
+
traverse: <A, B>(f: (a: A) => Task<B>) => (as: A[]) => Task<B[]>;
|
|
267
|
+
/**
|
|
268
|
+
* Creates a Task that delays for the specified milliseconds.
|
|
269
|
+
* @description Returns a Task that waits for the given duration before
|
|
270
|
+
* resolving with void.
|
|
271
|
+
*
|
|
272
|
+
* @param {number} ms - Number of milliseconds to delay
|
|
273
|
+
* @returns {Task<void>} A Task that delays
|
|
274
|
+
*
|
|
275
|
+
* @category Utilities
|
|
276
|
+
* @example
|
|
277
|
+
* ```typescript
|
|
278
|
+
* const delayed = Task.chain(() => Task.of('done'))(
|
|
279
|
+
* Task.delay(1000)
|
|
280
|
+
* );
|
|
281
|
+
* await Task.run(delayed); // => 'done' (after 1 second)
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @since 2025-09-18
|
|
285
|
+
*/
|
|
286
|
+
delay: (ms: number) => Task<void>;
|
|
287
|
+
/**
|
|
288
|
+
* Combines the results of a tuple of Tasks into a Task of a tuple.
|
|
289
|
+
* @description Takes multiple Tasks and returns a Task containing a tuple
|
|
290
|
+
* of their results. All Tasks run in parallel.
|
|
291
|
+
*
|
|
292
|
+
* @template T - Tuple type of Tasks
|
|
293
|
+
* @param {...T} tasks - Tasks to combine
|
|
294
|
+
* @returns {Task<{ [K in keyof T]: T[K] extends Task<infer U> ? U : never }>} Task of tuple
|
|
295
|
+
*
|
|
296
|
+
* @category Combinators
|
|
297
|
+
* @example
|
|
298
|
+
* ```typescript
|
|
299
|
+
* const task1 = Task.of(1);
|
|
300
|
+
* const task2 = Task.of('hello');
|
|
301
|
+
* const task3 = Task.of(true);
|
|
302
|
+
*
|
|
303
|
+
* const combined = Task.sequenceT(task1, task2, task3);
|
|
304
|
+
* await Task.run(combined); // => [1, 'hello', true]
|
|
305
|
+
* ```
|
|
306
|
+
*
|
|
307
|
+
* @since 2025-09-18
|
|
308
|
+
*/
|
|
309
|
+
sequenceT: <T extends readonly Task<unknown>[]>(...tasks: T) => Task<{ [K in keyof T]: T[K] extends Task<infer U> ? U : never; }>;
|
|
310
|
+
/**
|
|
311
|
+
* Combines the results of a record of Tasks into a Task of a record.
|
|
312
|
+
* @description Takes an object with Task values and returns a Task containing
|
|
313
|
+
* an object with the results. All Tasks run in parallel.
|
|
314
|
+
*
|
|
315
|
+
* @template R - Record type with Task values
|
|
316
|
+
* @param {R} tasks - Record of Tasks to combine
|
|
317
|
+
* @returns {Task<{ [K in keyof R]: R[K] extends Task<infer U> ? U : never }>} Task of record
|
|
318
|
+
*
|
|
319
|
+
* @category Combinators
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* const tasks = {
|
|
323
|
+
* user: Task.of({ name: 'Alice' }),
|
|
324
|
+
* count: Task.of(42),
|
|
325
|
+
* enabled: Task.of(true)
|
|
326
|
+
* };
|
|
327
|
+
*
|
|
328
|
+
* const combined = Task.sequenceS(tasks);
|
|
329
|
+
* await Task.run(combined);
|
|
330
|
+
* // => { user: { name: 'Alice' }, count: 42, enabled: true }
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @since 2025-09-18
|
|
334
|
+
*/
|
|
335
|
+
sequenceS: <R extends Record<string, Task<unknown>>>(tasks: R) => Task<{ [K in keyof R]: R[K] extends Task<infer U> ? U : never; }>;
|
|
336
|
+
/**
|
|
337
|
+
* Executes a Task for its side effects, discarding the result.
|
|
338
|
+
* @description Runs a Task but returns void, useful for Tasks that
|
|
339
|
+
* perform side effects where the result isn't needed.
|
|
340
|
+
*
|
|
341
|
+
* @template A - The type of the value (discarded)
|
|
342
|
+
* @param {function(A): Task<unknown>} f - Function that returns a Task (result discarded)
|
|
343
|
+
* @returns {function(Task<A>): Task<A>} A function that executes side effects
|
|
344
|
+
*
|
|
345
|
+
* @category Combinators
|
|
346
|
+
* @example
|
|
347
|
+
* ```typescript
|
|
348
|
+
* const log = (msg: string): Task<void> =>
|
|
349
|
+
* () => Promise.resolve(console.log(msg));
|
|
350
|
+
*
|
|
351
|
+
* const task = Task.chainFirst((n: number) => log(`Got: ${n}`))(
|
|
352
|
+
* Task.of(42)
|
|
353
|
+
* );
|
|
354
|
+
* await Task.run(task); // logs "Got: 42", returns 42
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* @since 2025-09-18
|
|
358
|
+
*/
|
|
359
|
+
chainFirst: <A>(f: (a: A) => Task<unknown>) => (task: Task<A>) => Task<A>;
|
|
360
|
+
};
|
|
361
|
+
//# sourceMappingURL=task.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task.d.mts","sourceRoot":"","sources":["../src/task.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAEvC;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI;IACf;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;SACE,CAAC,SAAU,CAAC,KAAG,IAAI,CAAC,CAAC,CAAC;IAE3B;;;;;;;;;;;;;;;;;;;;;OAqBG;kBACW,CAAC,KAAM,MAAM,OAAO,CAAC,CAAC,CAAC,KAAG,IAAI,CAAC,CAAC,CAAC;IAE/C;;;;;;;;;;;;;;;;;;;;;OAqBG;UACG,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,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