@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.
Files changed (100) hide show
  1. package/README.md +182 -0
  2. package/esm/mod.js +3 -0
  3. package/esm/package.json +3 -0
  4. package/esm/src/Composition/compose.js +3 -0
  5. package/esm/src/Composition/curry.js +42 -0
  6. package/esm/src/Composition/flip.js +20 -0
  7. package/esm/src/Composition/flow.js +8 -0
  8. package/esm/src/Composition/fn.js +85 -0
  9. package/esm/src/Composition/index.js +10 -0
  10. package/esm/src/Composition/memoize.js +66 -0
  11. package/esm/src/Composition/not.js +25 -0
  12. package/esm/src/Composition/pipe.js +3 -0
  13. package/esm/src/Composition/tap.js +33 -0
  14. package/esm/src/Composition/uncurry.js +32 -0
  15. package/esm/src/Core/Arr.js +461 -0
  16. package/esm/src/Core/InternalTypes.js +1 -0
  17. package/esm/src/Core/Option.js +195 -0
  18. package/esm/src/Core/Rec.js +167 -0
  19. package/esm/src/Core/RemoteData.js +210 -0
  20. package/esm/src/Core/Result.js +173 -0
  21. package/esm/src/Core/Task.js +108 -0
  22. package/esm/src/Core/TaskResult.js +63 -0
  23. package/esm/src/Core/Validation.js +215 -0
  24. package/esm/src/Core/index.js +8 -0
  25. package/esm/src/Types/NonEmptyList.js +14 -0
  26. package/esm/src/Types/index.js +1 -0
  27. package/package.json +60 -0
  28. package/script/mod.js +19 -0
  29. package/script/package.json +3 -0
  30. package/script/src/Composition/compose.js +6 -0
  31. package/script/src/Composition/curry.js +48 -0
  32. package/script/src/Composition/flip.js +24 -0
  33. package/script/src/Composition/flow.js +11 -0
  34. package/script/src/Composition/fn.js +98 -0
  35. package/script/src/Composition/index.js +26 -0
  36. package/script/src/Composition/memoize.js +71 -0
  37. package/script/src/Composition/not.js +29 -0
  38. package/script/src/Composition/pipe.js +6 -0
  39. package/script/src/Composition/tap.js +37 -0
  40. package/script/src/Composition/uncurry.js +38 -0
  41. package/script/src/Core/Arr.js +464 -0
  42. package/script/src/Core/InternalTypes.js +2 -0
  43. package/script/src/Core/Option.js +198 -0
  44. package/script/src/Core/Rec.js +170 -0
  45. package/script/src/Core/RemoteData.js +213 -0
  46. package/script/src/Core/Result.js +176 -0
  47. package/script/src/Core/Task.js +111 -0
  48. package/script/src/Core/TaskResult.js +66 -0
  49. package/script/src/Core/Validation.js +218 -0
  50. package/script/src/Core/index.js +24 -0
  51. package/script/src/Types/NonEmptyList.js +18 -0
  52. package/script/src/Types/index.js +17 -0
  53. package/types/mod.d.ts +4 -0
  54. package/types/mod.d.ts.map +1 -0
  55. package/types/src/Composition/compose.d.ts +33 -0
  56. package/types/src/Composition/compose.d.ts.map +1 -0
  57. package/types/src/Composition/curry.d.ts +43 -0
  58. package/types/src/Composition/curry.d.ts.map +1 -0
  59. package/types/src/Composition/flip.d.ts +21 -0
  60. package/types/src/Composition/flip.d.ts.map +1 -0
  61. package/types/src/Composition/flow.d.ts +56 -0
  62. package/types/src/Composition/flow.d.ts.map +1 -0
  63. package/types/src/Composition/fn.d.ts +76 -0
  64. package/types/src/Composition/fn.d.ts.map +1 -0
  65. package/types/src/Composition/index.d.ts +11 -0
  66. package/types/src/Composition/index.d.ts.map +1 -0
  67. package/types/src/Composition/memoize.d.ts +46 -0
  68. package/types/src/Composition/memoize.d.ts.map +1 -0
  69. package/types/src/Composition/not.d.ts +26 -0
  70. package/types/src/Composition/not.d.ts.map +1 -0
  71. package/types/src/Composition/pipe.d.ts +56 -0
  72. package/types/src/Composition/pipe.d.ts.map +1 -0
  73. package/types/src/Composition/tap.d.ts +31 -0
  74. package/types/src/Composition/tap.d.ts.map +1 -0
  75. package/types/src/Composition/uncurry.d.ts +54 -0
  76. package/types/src/Composition/uncurry.d.ts.map +1 -0
  77. package/types/src/Core/Arr.d.ts +355 -0
  78. package/types/src/Core/Arr.d.ts.map +1 -0
  79. package/types/src/Core/InternalTypes.d.ts +14 -0
  80. package/types/src/Core/InternalTypes.d.ts.map +1 -0
  81. package/types/src/Core/Option.d.ts +214 -0
  82. package/types/src/Core/Option.d.ts.map +1 -0
  83. package/types/src/Core/Rec.d.ts +121 -0
  84. package/types/src/Core/Rec.d.ts.map +1 -0
  85. package/types/src/Core/RemoteData.d.ts +196 -0
  86. package/types/src/Core/RemoteData.d.ts.map +1 -0
  87. package/types/src/Core/Result.d.ts +185 -0
  88. package/types/src/Core/Result.d.ts.map +1 -0
  89. package/types/src/Core/Task.d.ts +125 -0
  90. package/types/src/Core/Task.d.ts.map +1 -0
  91. package/types/src/Core/TaskResult.d.ts +78 -0
  92. package/types/src/Core/TaskResult.d.ts.map +1 -0
  93. package/types/src/Core/Validation.d.ts +217 -0
  94. package/types/src/Core/Validation.d.ts.map +1 -0
  95. package/types/src/Core/index.d.ts +9 -0
  96. package/types/src/Core/index.d.ts.map +1 -0
  97. package/types/src/Types/NonEmptyList.d.ts +29 -0
  98. package/types/src/Types/NonEmptyList.d.ts.map +1 -0
  99. package/types/src/Types/index.d.ts +2 -0
  100. package/types/src/Types/index.d.ts.map +1 -0
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Result = void 0;
4
+ var Result;
5
+ (function (Result) {
6
+ /**
7
+ * Wraps a value in a successful Result (Ok).
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * Result.of(42); // Ok(42)
12
+ * ```
13
+ */
14
+ Result.of = (value) => Result.toOk(value);
15
+ /**
16
+ * Creates a failed Result with the given error.
17
+ */
18
+ Result.toErr = (error) => ({ kind: "Error", error });
19
+ /**
20
+ * Creates a successful Result with the given value.
21
+ */
22
+ Result.toOk = (value) => ({ kind: "Ok", value });
23
+ /**
24
+ * Type guard that checks if an Result is Ok.
25
+ */
26
+ Result.isOk = (data) => data.kind === "Ok";
27
+ /**
28
+ * Type guard that checks if an Result is Err.
29
+ */
30
+ Result.isErr = (data) => data.kind === "Error";
31
+ /**
32
+ * Creates an Result from a function that may throw.
33
+ * Catches any errors and transforms them using the onError function.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const parseJson = (s: string): Result<string, unknown> =>
38
+ * Result.tryCatch(
39
+ * () => JSON.parse(s),
40
+ * (e) => `Parse error: ${e}`
41
+ * );
42
+ * ```
43
+ */
44
+ Result.tryCatch = (f, onError) => {
45
+ try {
46
+ return Result.toOk(f());
47
+ }
48
+ catch (e) {
49
+ return Result.toErr(onError(e));
50
+ }
51
+ };
52
+ /**
53
+ * Transforms the success value inside an Result.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * pipe(Result.of(5), Result.map(n => n * 2)); // Ok(10)
58
+ * pipe(Result.toErr("error"), Result.map(n => n * 2)); // Err("error")
59
+ * ```
60
+ */
61
+ Result.map = (f) => (data) => Result.isOk(data) ? Result.toOk(f(data.value)) : data;
62
+ /**
63
+ * Transforms the error value inside an Result.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * pipe(Result.toErr("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
68
+ * ```
69
+ */
70
+ Result.mapError = (f) => (data) => Result.isErr(data) ? Result.toErr(f(data.error)) : data;
71
+ /**
72
+ * Chains Result computations. If the first is Ok, passes the value to f.
73
+ * If the first is Err, propagates the error.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const validatePositive = (n: number): Result<string, number> =>
78
+ * n > 0 ? Result.of(n) : Result.toErr("Must be positive");
79
+ *
80
+ * pipe(Result.of(5), Result.chain(validatePositive)); // Ok(5)
81
+ * pipe(Result.of(-1), Result.chain(validatePositive)); // Err("Must be positive")
82
+ * ```
83
+ */
84
+ Result.chain = (f) => (data) => Result.isOk(data) ? f(data.value) : data;
85
+ /**
86
+ * Extracts the value from an Result by providing handlers for both cases.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * pipe(
91
+ * Result.of(5),
92
+ * Result.fold(
93
+ * e => `Error: ${e}`,
94
+ * n => `Value: ${n}`
95
+ * )
96
+ * ); // "Value: 5"
97
+ * ```
98
+ */
99
+ Result.fold = (onErr, onOk) => (data) => Result.isOk(data) ? onOk(data.value) : onErr(data.error);
100
+ /**
101
+ * Pattern matches on a Result, returning the result of the matching case.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * pipe(
106
+ * result,
107
+ * Result.match({
108
+ * ok: value => `Got ${value}`,
109
+ * err: error => `Failed: ${error}`
110
+ * })
111
+ * );
112
+ * ```
113
+ */
114
+ Result.match = (cases) => (data) => Result.isOk(data) ? cases.ok(data.value) : cases.err(data.error);
115
+ /**
116
+ * Returns the success value or a default value if the Result is an error.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * pipe(Result.of(5), Result.getOrElse(0)); // 5
121
+ * pipe(Result.toErr("error"), Result.getOrElse(0)); // 0
122
+ * ```
123
+ */
124
+ Result.getOrElse = (defaultValue) => (data) => Result.isOk(data) ? data.value : defaultValue;
125
+ /**
126
+ * Executes a side effect on the success value without changing the Result.
127
+ * Useful for logging or debugging.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * pipe(
132
+ * Result.of(5),
133
+ * Result.tap(n => console.log("Value:", n)),
134
+ * Result.map(n => n * 2)
135
+ * );
136
+ * ```
137
+ */
138
+ Result.tap = (f) => (data) => {
139
+ if (Result.isOk(data))
140
+ f(data.value);
141
+ return data;
142
+ };
143
+ /**
144
+ * Recovers from an error by providing a fallback Result.
145
+ */
146
+ Result.recover = (fallback) => (data) => Result.isOk(data) ? data : fallback();
147
+ /**
148
+ * Recovers from an error unless it matches the blocked error.
149
+ */
150
+ Result.recoverUnless = (blockedErr, fallback) => (data) => Result.isErr(data) && data.error !== blockedErr ? fallback() : data;
151
+ /**
152
+ * Converts a Result to an Option.
153
+ * Ok becomes Some, Err becomes None (the error is discarded).
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * Result.toOption(Result.toOk(42)); // Some(42)
158
+ * Result.toOption(Result.toErr("oops")); // None
159
+ * ```
160
+ */
161
+ Result.toOption = (data) => Result.isOk(data) ? { kind: "Some", value: data.value } : { kind: "None" };
162
+ /**
163
+ * Applies a function wrapped in an Result to a value wrapped in an Result.
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * const add = (a: number) => (b: number) => a + b;
168
+ * pipe(
169
+ * Result.of(add),
170
+ * Result.ap(Result.of(5)),
171
+ * Result.ap(Result.of(3))
172
+ * ); // Ok(8)
173
+ * ```
174
+ */
175
+ Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.toOk(data.value(arg.value)) : Result.isErr(data) ? data : arg;
176
+ })(Result || (exports.Result = Result = {}));
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Task = void 0;
4
+ var Task;
5
+ (function (Task) {
6
+ /**
7
+ * Wraps a value in a Task that immediately resolves to that value.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const task = Task.of(42);
12
+ * task().then(console.log); // 42
13
+ * ```
14
+ */
15
+ Task.of = (value) => () => Promise.resolve(value);
16
+ /**
17
+ * Creates a Task that will reject with the given error.
18
+ */
19
+ Task.fail = (error) => () => Promise.reject(error);
20
+ /**
21
+ * Creates a Task from a function that returns a Promise.
22
+ * Alias for directly creating a Task.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const fetchUser = Task.from(() => fetch('/user').then(r => r.json()));
27
+ * ```
28
+ */
29
+ Task.from = (f) => f;
30
+ /**
31
+ * Transforms the value inside a Task.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * pipe(
36
+ * Task.of(5),
37
+ * Task.map(n => n * 2)
38
+ * )(); // Promise<10>
39
+ * ```
40
+ */
41
+ Task.map = (f) => (data) => () => data().then(f);
42
+ /**
43
+ * Chains Task computations. If the first succeeds, passes the value to f.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const fetchUser = (id: string): Task<User> => () => fetch(`/users/${id}`).then(r => r.json());
48
+ * const fetchPosts = (user: User): Task<Post[]> => () => fetch(`/posts?userId=${user.id}`).then(r => r.json());
49
+ *
50
+ * pipe(
51
+ * fetchUser("123"),
52
+ * Task.chain(fetchPosts)
53
+ * )(); // Promise<Post[]>
54
+ * ```
55
+ */
56
+ Task.chain = (f) => (data) => () => data().then((a) => f(a)());
57
+ /**
58
+ * Applies a function wrapped in a Task to a value wrapped in a Task.
59
+ * Both Tasks run in parallel.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * const add = (a: number) => (b: number) => a + b;
64
+ * pipe(
65
+ * Task.of(add),
66
+ * Task.ap(Task.of(5)),
67
+ * Task.ap(Task.of(3))
68
+ * )(); // Promise<8>
69
+ * ```
70
+ */
71
+ Task.ap = (arg) => (data) => () => Promise.all([data(), arg()]).then(([f, a]) => f(a));
72
+ /**
73
+ * Executes a side effect on the value without changing the Task.
74
+ * Useful for logging or debugging.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * pipe(
79
+ * fetchData,
80
+ * Task.tap(data => console.log("Fetched:", data)),
81
+ * Task.map(transform)
82
+ * );
83
+ * ```
84
+ */
85
+ Task.tap = (f) => (data) => () => data().then((a) => {
86
+ f(a);
87
+ return a;
88
+ });
89
+ /**
90
+ * Runs multiple Tasks in parallel and collects their results.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * Task.all([fetchUser, fetchPosts, fetchComments])();
95
+ * // Promise<[User, Post[], Comment[]]>
96
+ * ```
97
+ */
98
+ Task.all = (tasks) => () => Promise.all(tasks.map((t) => t()));
99
+ /**
100
+ * Delays the execution of a Task by the specified milliseconds.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * pipe(
105
+ * Task.of(42),
106
+ * Task.delay(1000)
107
+ * )(); // Resolves after 1 second
108
+ * ```
109
+ */
110
+ Task.delay = (ms) => (data) => () => new Promise((resolve, reject) => setTimeout(() => data().then(resolve, reject), ms));
111
+ })(Task || (exports.Task = Task = {}));
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskResult = void 0;
4
+ const Result_js_1 = require("./Result.js");
5
+ const Task_js_1 = require("./Task.js");
6
+ var TaskResult;
7
+ (function (TaskResult) {
8
+ /**
9
+ * Wraps a value in a successful TaskResult.
10
+ */
11
+ TaskResult.of = (value) => Task_js_1.Task.of(Result_js_1.Result.toOk(value));
12
+ /**
13
+ * Creates a failed TaskResult with the given error.
14
+ */
15
+ TaskResult.fail = (error) => Task_js_1.Task.of(Result_js_1.Result.toErr(error));
16
+ /**
17
+ * Creates a TaskResult from a function that may throw.
18
+ * Catches any errors and transforms them using the onError function.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const parseJson = (s: string): TaskResult<string, unknown> =>
23
+ * TaskResult.tryCatch(
24
+ * async () => JSON.parse(s),
25
+ * (e) => `Parse error: ${e}`
26
+ * );
27
+ * ```
28
+ */
29
+ TaskResult.tryCatch = (f, onError) => () => f()
30
+ .then(Result_js_1.Result.toOk)
31
+ .catch((e) => Result_js_1.Result.toErr(onError(e)));
32
+ /**
33
+ * Transforms the success value inside a TaskResult.
34
+ */
35
+ TaskResult.map = (f) => (data) => Task_js_1.Task.map(Result_js_1.Result.map(f))(data);
36
+ /**
37
+ * Transforms the error value inside a TaskResult.
38
+ */
39
+ TaskResult.mapError = (f) => (data) => Task_js_1.Task.map(Result_js_1.Result.mapError(f))(data);
40
+ /**
41
+ * Chains TaskResult computations. If the first succeeds, passes the value to f.
42
+ * If the first fails, propagates the error.
43
+ */
44
+ TaskResult.chain = (f) => (data) => Task_js_1.Task.chain((result) => Result_js_1.Result.isOk(result) ? f(result.value) : Task_js_1.Task.of(Result_js_1.Result.toErr(result.error)))(data);
45
+ /**
46
+ * Extracts the value from a TaskResult by providing handlers for both cases.
47
+ */
48
+ TaskResult.fold = (onErr, onOk) => (data) => Task_js_1.Task.map(Result_js_1.Result.fold(onErr, onOk))(data);
49
+ /**
50
+ * Pattern matches on a TaskResult, returning a Task of the result.
51
+ */
52
+ TaskResult.match = (cases) => (data) => Task_js_1.Task.map(Result_js_1.Result.match(cases))(data);
53
+ /**
54
+ * Recovers from an error by providing a fallback TaskResult.
55
+ */
56
+ TaskResult.recover = (fallback) => (data) => Task_js_1.Task.chain((result) => Result_js_1.Result.isErr(result) ? fallback(result.error) : Task_js_1.Task.of(result))(data);
57
+ /**
58
+ * Returns the success value or a default value if the TaskResult is an error.
59
+ */
60
+ TaskResult.getOrElse = (defaultValue) => (data) => Task_js_1.Task.map(Result_js_1.Result.getOrElse(defaultValue))(data);
61
+ /**
62
+ * Executes a side effect on the success value without changing the TaskResult.
63
+ * Useful for logging or debugging.
64
+ */
65
+ TaskResult.tap = (f) => (data) => Task_js_1.Task.map(Result_js_1.Result.tap(f))(data);
66
+ })(TaskResult || (exports.TaskResult = TaskResult = {}));
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Validation = void 0;
4
+ const NonEmptyList_js_1 = require("../Types/NonEmptyList.js");
5
+ var Validation;
6
+ (function (Validation) {
7
+ /**
8
+ * Wraps a value in a valid Validation.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * Validation.of(42); // Valid(42)
13
+ * ```
14
+ */
15
+ Validation.of = (value) => ({
16
+ kind: "Valid",
17
+ value,
18
+ });
19
+ /**
20
+ * Creates a valid Validation with the given value.
21
+ */
22
+ Validation.toValid = (value) => ({ kind: "Valid", value });
23
+ /**
24
+ * Type guard that checks if a Validation is valid.
25
+ */
26
+ Validation.isValid = (data) => data.kind === "Valid";
27
+ /**
28
+ * Creates an invalid Validation with the given errors.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * Validation.toInvalid(["Email is invalid", "Password too short"]);
33
+ * ```
34
+ */
35
+ Validation.toInvalid = (errors) => ({
36
+ kind: "Invalid",
37
+ errors,
38
+ });
39
+ /**
40
+ * Type guard that checks if a Validation is invalid.
41
+ */
42
+ Validation.isInvalid = (data) => data.kind === "Invalid";
43
+ /**
44
+ * Creates an invalid Validation from a single error.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * Validation.fail("Invalid input");
49
+ * ```
50
+ */
51
+ Validation.fail = (error) => Validation.toInvalid([error]);
52
+ /**
53
+ * Transforms the success value inside a Validation.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * pipe(Validation.of(5), Validation.map(n => n * 2)); // Valid(10)
58
+ * pipe(Validation.fail("oops"), Validation.map(n => n * 2)); // Invalid(["oops"])
59
+ * ```
60
+ */
61
+ Validation.map = (f) => (data) => Validation.isValid(data) ? Validation.of(f(data.value)) : data;
62
+ /**
63
+ * Chains Validation computations. If the first is Valid, passes the value to f.
64
+ * If the first is Invalid, propagates the errors.
65
+ *
66
+ * Note: chain short-circuits on first error. Use `ap` to accumulate errors.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * const validatePositive = (n: number): Validation<string, number> =>
71
+ * n > 0 ? Validation.of(n) : Validation.fail("Must be positive");
72
+ *
73
+ * pipe(Validation.of(5), Validation.chain(validatePositive)); // Valid(5)
74
+ * pipe(Validation.of(-1), Validation.chain(validatePositive)); // Invalid(["Must be positive"])
75
+ * ```
76
+ */
77
+ Validation.chain = (f) => (data) => Validation.isValid(data) ? f(data.value) : data;
78
+ /**
79
+ * Applies a function wrapped in a Validation to a value wrapped in a Validation.
80
+ * Accumulates errors from both sides.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * const add = (a: number) => (b: number) => a + b;
85
+ * pipe(
86
+ * Validation.of(add),
87
+ * Validation.ap(Validation.of(5)),
88
+ * Validation.ap(Validation.of(3))
89
+ * ); // Valid(8)
90
+ *
91
+ * pipe(
92
+ * Validation.of(add),
93
+ * Validation.ap(Validation.fail<string, number>("bad a")),
94
+ * Validation.ap(Validation.fail<string, number>("bad b"))
95
+ * ); // Invalid(["bad a", "bad b"])
96
+ * ```
97
+ */
98
+ Validation.ap = (arg) => (data) => {
99
+ if (Validation.isValid(data) && Validation.isValid(arg))
100
+ return Validation.of(data.value(arg.value));
101
+ const errors = [
102
+ ...(Validation.isInvalid(data) ? data.errors : []),
103
+ ...(Validation.isInvalid(arg) ? arg.errors : []),
104
+ ];
105
+ return (0, NonEmptyList_js_1.isNonEmptyList)(errors) ? Validation.toInvalid(errors) : Validation.of(data);
106
+ };
107
+ /**
108
+ * Extracts the value from a Validation by providing handlers for both cases.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * pipe(
113
+ * Validation.of(42),
114
+ * Validation.fold(
115
+ * errors => `Errors: ${errors.join(", ")}`,
116
+ * value => `Value: ${value}`
117
+ * )
118
+ * );
119
+ * ```
120
+ */
121
+ Validation.fold = (onInvalid, onValid) => (data) => Validation.isValid(data) ? onValid(data.value) : onInvalid(data.errors);
122
+ /**
123
+ * Pattern matches on a Validation, returning the result of the matching case.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * pipe(
128
+ * validation,
129
+ * Validation.match({
130
+ * valid: value => `Got ${value}`,
131
+ * invalid: errors => `Failed: ${errors.join(", ")}`
132
+ * })
133
+ * );
134
+ * ```
135
+ */
136
+ Validation.match = (cases) => (data) => Validation.isValid(data) ? cases.valid(data.value) : cases.invalid(data.errors);
137
+ /**
138
+ * Returns the success value or a default value if the Validation is invalid.
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * pipe(Validation.of(5), Validation.getOrElse(0)); // 5
143
+ * pipe(Validation.fail("oops"), Validation.getOrElse(0)); // 0
144
+ * ```
145
+ */
146
+ Validation.getOrElse = (defaultValue) => (data) => Validation.isValid(data) ? data.value : defaultValue;
147
+ /**
148
+ * Executes a side effect on the success value without changing the Validation.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * pipe(
153
+ * Validation.of(5),
154
+ * Validation.tap(n => console.log("Value:", n)),
155
+ * Validation.map(n => n * 2)
156
+ * );
157
+ * ```
158
+ */
159
+ Validation.tap = (f) => (data) => {
160
+ if (Validation.isValid(data))
161
+ f(data.value);
162
+ return data;
163
+ };
164
+ /**
165
+ * Recovers from an Invalid state by providing a fallback Validation.
166
+ */
167
+ Validation.recover = (fallback) => (data) => Validation.isValid(data) ? data : fallback();
168
+ /**
169
+ * Recovers from an Invalid state unless the errors contain any of the blocked errors.
170
+ */
171
+ Validation.recoverUnless = (blockedErrors, fallback) => (data) => Validation.isInvalid(data) &&
172
+ !data.errors.some((err) => blockedErrors.includes(err))
173
+ ? fallback()
174
+ : data;
175
+ /**
176
+ * Combines two Validation instances, accumulating errors from both.
177
+ * If both are Valid, returns the second valid value.
178
+ * If either is Invalid, combines their errors into a single Invalid.
179
+ *
180
+ * @example
181
+ * ```ts
182
+ * Validation.combine(
183
+ * Validation.fail("Error 1"),
184
+ * Validation.fail("Error 2")
185
+ * ); // Invalid(["Error 1", "Error 2"])
186
+ *
187
+ * Validation.combine(
188
+ * Validation.of("a"),
189
+ * Validation.of("b")
190
+ * ); // Valid("b")
191
+ * ```
192
+ */
193
+ Validation.combine = (first, second) => {
194
+ if (Validation.isValid(first) && Validation.isValid(second)) {
195
+ return second;
196
+ }
197
+ const errors = [
198
+ ...(Validation.isInvalid(first) ? first.errors : []),
199
+ ...(Validation.isInvalid(second) ? second.errors : []),
200
+ ];
201
+ return (0, NonEmptyList_js_1.isNonEmptyList)(errors) ? Validation.toInvalid(errors) : second;
202
+ };
203
+ /**
204
+ * Combines multiple Validation instances, accumulating all errors.
205
+ * If all are Valid, returns the last valid value.
206
+ * Returns undefined for an empty array.
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * Validation.combineAll([
211
+ * validateName(name),
212
+ * validateEmail(email),
213
+ * validateAge(age)
214
+ * ]);
215
+ * ```
216
+ */
217
+ Validation.combineAll = (data) => data.length === 0 ? undefined : data.reduce((acc, v) => Validation.combine(acc, v));
218
+ })(Validation || (exports.Validation = Validation = {}));
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Arr.js"), exports);
18
+ __exportStar(require("./Option.js"), exports);
19
+ __exportStar(require("./Rec.js"), exports);
20
+ __exportStar(require("./RemoteData.js"), exports);
21
+ __exportStar(require("./Result.js"), exports);
22
+ __exportStar(require("./Task.js"), exports);
23
+ __exportStar(require("./TaskResult.js"), exports);
24
+ __exportStar(require("./Validation.js"), exports);
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNonEmptyList = void 0;
4
+ /**
5
+ * Type guard that checks if an array is non-empty.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const items: string[] = getItems();
10
+ *
11
+ * if (isNonEmptyList(items)) {
12
+ * // TypeScript knows items has at least one element
13
+ * const first = items[0]; // string, not string | undefined
14
+ * }
15
+ * ```
16
+ */
17
+ const isNonEmptyList = (list) => list.length > 0;
18
+ exports.isNonEmptyList = isNonEmptyList;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./NonEmptyList.js"), exports);
package/types/mod.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./src/Composition/index.js";
2
+ export * from "./src/Core/index.js";
3
+ export * from "./src/Composition/index.js";
4
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,4BAA4B,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Composes functions from right to left, returning a new function.
3
+ * This is the traditional mathematical function composition: (f . g)(x) = f(g(x))
4
+ *
5
+ * Unlike `flow` which reads left-to-right, `compose` reads right-to-left,
6
+ * matching how nested function calls would be written.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const addOne = (n: number) => n + 1;
11
+ * const double = (n: number) => n * 2;
12
+ *
13
+ * // compose: right-to-left (double first, then addOne)
14
+ * const composed = compose(addOne, double);
15
+ * composed(5); // 11 (5 * 2 + 1)
16
+ *
17
+ * // flow: left-to-right (addOne first, then double)
18
+ * const flowed = flow(addOne, double);
19
+ * flowed(5); // 12 ((5 + 1) * 2)
20
+ * ```
21
+ *
22
+ * @see {@link flow} for left-to-right composition
23
+ */
24
+ export declare function compose<A, B>(ab: (a: A) => B): (a: A) => B;
25
+ export declare function compose<A, B, C>(bc: (b: B) => C, ab: (a: A) => B): (a: A) => C;
26
+ export declare function compose<A, B, C, D>(cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => D;
27
+ export declare function compose<A, B, C, D, E>(de: (d: D) => E, cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => E;
28
+ export declare function compose<A, B, C, D, E, F>(ef: (e: E) => F, de: (d: D) => E, cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => F;
29
+ export declare function compose<A, B, C, D, E, F, G>(fg: (f: F) => G, ef: (e: E) => F, de: (d: D) => E, cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => G;
30
+ export declare function compose<A, B, C, D, E, F, G, H>(gh: (g: G) => H, fg: (f: F) => G, ef: (e: E) => F, de: (d: D) => E, cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => H;
31
+ export declare function compose<A, B, C, D, E, F, G, H, I>(hi: (h: H) => I, gh: (g: G) => H, fg: (f: F) => G, ef: (e: E) => F, de: (d: D) => E, cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => I;
32
+ export declare function compose<A, B, C, D, E, F, G, H, I, J>(ij: (i: I) => J, hi: (h: H) => I, gh: (g: G) => H, fg: (f: F) => G, ef: (e: E) => F, de: (d: D) => E, cd: (c: C) => D, bc: (b: B) => C, ab: (a: A) => B): (a: A) => J;
33
+ //# sourceMappingURL=compose.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compose.d.ts","sourceRoot":"","sources":["../../../src/src/Composition/compose.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;AAC5D,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;AAChF,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAChC,EAAE,EAAE,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