@nlozgachev/pipekit 0.1.8 → 0.3.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.
Files changed (59) hide show
  1. package/README.md +18 -13
  2. package/esm/src/Composition/flip.js +2 -2
  3. package/esm/src/Composition/fn.js +1 -1
  4. package/esm/src/Composition/tap.js +1 -1
  5. package/esm/src/Core/Arr.js +16 -16
  6. package/esm/src/Core/Option.js +27 -36
  7. package/esm/src/Core/Rec.js +1 -1
  8. package/esm/src/Core/RemoteData.js +9 -13
  9. package/esm/src/Core/Result.js +23 -32
  10. package/esm/src/Core/Task.js +95 -24
  11. package/esm/src/Core/TaskOption.js +8 -6
  12. package/esm/src/Core/TaskResult.js +67 -6
  13. package/esm/src/Core/TaskValidation.js +12 -8
  14. package/esm/src/Core/These.js +42 -42
  15. package/esm/src/Core/Validation.js +42 -43
  16. package/esm/src/Types/Brand.js +3 -3
  17. package/package.json +1 -1
  18. package/script/src/Composition/flip.js +2 -2
  19. package/script/src/Composition/fn.js +1 -1
  20. package/script/src/Composition/tap.js +1 -1
  21. package/script/src/Core/Arr.js +16 -16
  22. package/script/src/Core/Option.js +27 -36
  23. package/script/src/Core/Rec.js +1 -1
  24. package/script/src/Core/RemoteData.js +9 -13
  25. package/script/src/Core/Result.js +23 -32
  26. package/script/src/Core/Task.js +95 -24
  27. package/script/src/Core/TaskOption.js +8 -6
  28. package/script/src/Core/TaskResult.js +67 -6
  29. package/script/src/Core/TaskValidation.js +12 -8
  30. package/script/src/Core/These.js +42 -42
  31. package/script/src/Core/Validation.js +42 -43
  32. package/script/src/Types/Brand.js +3 -3
  33. package/types/src/Composition/flip.d.ts +2 -2
  34. package/types/src/Composition/fn.d.ts +1 -1
  35. package/types/src/Composition/pipe.d.ts +1 -1
  36. package/types/src/Composition/tap.d.ts +1 -1
  37. package/types/src/Composition/uncurry.d.ts +3 -3
  38. package/types/src/Core/Arr.d.ts +5 -5
  39. package/types/src/Core/Arr.d.ts.map +1 -1
  40. package/types/src/Core/Option.d.ts +21 -30
  41. package/types/src/Core/Option.d.ts.map +1 -1
  42. package/types/src/Core/Rec.d.ts.map +1 -1
  43. package/types/src/Core/RemoteData.d.ts +9 -13
  44. package/types/src/Core/RemoteData.d.ts.map +1 -1
  45. package/types/src/Core/Result.d.ts +19 -28
  46. package/types/src/Core/Result.d.ts.map +1 -1
  47. package/types/src/Core/Task.d.ts +81 -34
  48. package/types/src/Core/Task.d.ts.map +1 -1
  49. package/types/src/Core/TaskOption.d.ts +1 -1
  50. package/types/src/Core/TaskOption.d.ts.map +1 -1
  51. package/types/src/Core/TaskResult.d.ts +41 -2
  52. package/types/src/Core/TaskResult.d.ts.map +1 -1
  53. package/types/src/Core/TaskValidation.d.ts +10 -6
  54. package/types/src/Core/TaskValidation.d.ts.map +1 -1
  55. package/types/src/Core/These.d.ts +33 -33
  56. package/types/src/Core/These.d.ts.map +1 -1
  57. package/types/src/Core/Validation.d.ts +38 -42
  58. package/types/src/Core/Validation.d.ts.map +1 -1
  59. package/types/src/Types/Brand.d.ts +5 -5
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @nlozgachev/pipekit
2
2
 
3
- [![npm](https://img.shields.io/npm/v/@nlozgachev/pipekit?style=for-the-badge&color=000&logo=npm&label&logoColor=fff)](https://www.npmjs.com/package/@nlozgachev/pipekit)[![JSR Version](https://img.shields.io/jsr/v/@nlozgachev/pipekit?style=for-the-badge&color=000&logo=jsr&label&logoColor=fff)](https://jsr.io/@nlozgachev/pipekit)[![TypeScript](https://img.shields.io/badge/-0?style=for-the-badge&color=000&logo=typescript&label&logoColor=fff)](https://www.typescriptlang.org)[![Deno](https://img.shields.io/badge/-0?style=for-the-badge&color=000&logo=Deno&label&logoColor=fff)](https://deno.com)
3
+ [![npm](https://img.shields.io/npm/v/@nlozgachev/pipekit?style=for-the-badge&color=000&logo=npm&label&logoColor=fff)](https://www.npmjs.com/package/@nlozgachev/pipekit)[![JSR Version](https://img.shields.io/jsr/v/@nlozgachev/pipekit?style=for-the-badge&color=000&logo=jsr&label&logoColor=fff)](https://jsr.io/@nlozgachev/pipekit)[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/nlozgachev/pipekit/publish.yml?style=for-the-badge&color=000&logo=githubactions&label&logoColor=fff)](https://github.com/nlozgachev/pipekit/actions/workflows/publish.yml)[![Codecov](https://img.shields.io/codecov/c/github/nlozgachev/pipekit?style=for-the-badge&color=000&logo=codecov&label&logoColor=fff)](https://app.codecov.io/github/nlozgachev/pipekit)[![TypeScript](https://img.shields.io/badge/-0?style=for-the-badge&color=000&logo=typescript&label&logoColor=fff)](https://www.typescriptlang.org)[![Deno](https://img.shields.io/badge/-0?style=for-the-badge&color=000&logo=Deno&label&logoColor=fff)](https://deno.com)
4
4
 
5
5
  A TypeScript toolkit for writing code that means exactly what it says.
6
6
 
@@ -14,7 +14,10 @@ deno add jsr:@nlozgachev/pipekit
14
14
 
15
15
  ## What is this?
16
16
 
17
- A toolkit for expressing uncertainty precisely. Instead of `T | null`, `try/catch`, and loading state flag soup, you get types that name every possible state and make invalid ones unrepresentable. Each type comes with a consistent set of operations — `map`, `chain`, `match`, `getOrElse` — that compose with `pipe` and `flow`.
17
+ A toolkit for expressing uncertainty precisely. Instead of `T | null`, `try/catch`, and loading
18
+ state flag soup, you get types that name every possible state and make invalid ones unrepresentable.
19
+ Each type comes with a consistent set of operations — `map`, `chain`, `match`, `getOrElse` — that
20
+ compose with `pipe` and `flow`.
18
21
 
19
22
  No FP jargon required. You won't find `Monad`, `Functor`, or `Applicative` in the API.
20
23
 
@@ -24,13 +27,15 @@ No FP jargon required. You won't find `Monad`, `Functor`, or `Applicative` in th
24
27
 
25
28
  - **`Option<A>`** — a value that might not exist. Replaces `T | null | undefined`.
26
29
  - **`Result<E, A>`** — an operation that succeeds or fails. Replaces `try/catch`.
27
- - **`Validation<E, A>`** — like `Result`, but accumulates all errors instead of stopping at the first.
30
+ - **`Validation<E, A>`** — like `Result`, but accumulates all errors instead of stopping at the
31
+ first.
28
32
  - **`Task<A>`** — a lazy async operation that doesn't run until called.
29
33
  - **`TaskResult<E, A>`** — `Task` + `Result`. A lazy async operation that can fail.
30
34
  - **`TaskOption<A>`** — `Task` + `Option`. Replaces `Promise<T | null>`.
31
35
  - **`TaskValidation<E, A>`** — `Task` + `Validation`. For async checks that all need to run.
32
36
  - **`These<E, A>`** — an inclusive OR: holds an error, a value, or both at once.
33
- - **`RemoteData<E, A>`** — the four states of a data fetch: `NotAsked`, `Loading`, `Failure`, `Success`.
37
+ - **`RemoteData<E, A>`** — the four states of a data fetch: `NotAsked`, `Loading`, `Failure`,
38
+ `Success`.
34
39
  - **`Arr`** — array utilities that return `Option` instead of `undefined`.
35
40
  - **`Rec`** — record/object utilities.
36
41
 
@@ -52,19 +57,19 @@ import { pipe } from "@nlozgachev/pipekit/Composition";
52
57
 
53
58
  // Chain nullable lookups without nested null checks
54
59
  const city = pipe(
55
- getUser(userId), // User | null
56
- Option.fromNullable, // Option<User>
57
- Option.chain((u) => Option.fromNullable(u.address)),// Option<Address>
58
- Option.chain((a) => Option.fromNullable(a.city)), // Option<string>
59
- Option.map((c) => c.toUpperCase()), // Option<string>
60
- Option.getOrElse("UNKNOWN"), // string
60
+ getUser(userId), // User | null
61
+ Option.fromNullable, // Option<User>
62
+ Option.chain((u) => Option.fromNullable(u.address)), // Option<Address>
63
+ Option.chain((a) => Option.fromNullable(a.city)), // Option<string>
64
+ Option.map((c) => c.toUpperCase()), // Option<string>
65
+ Option.getOrElse("UNKNOWN"), // string
61
66
  );
62
67
 
63
68
  // Parse input and look up a record — both steps can fail
64
69
  const record = pipe(
65
- parseId(rawInput), // Result<ParseError, number>
66
- Result.chain((id) => db.find(id)), // Result<ParseError | NotFoundError, Record>
67
- Result.map((r) => r.name), // Result<ParseError | NotFoundError, string>
70
+ parseId(rawInput), // Result<ParseError, number>
71
+ Result.chain((id) => db.find(id)), // Result<ParseError | NotFoundError, Record>
72
+ Result.map((r) => r.name), // Result<ParseError | NotFoundError, string>
68
73
  );
69
74
  ```
70
75
 
@@ -6,13 +6,13 @@
6
6
  * ```ts
7
7
  * // Original data-last (for pipe)
8
8
  * pipe(
9
- * Option.of(5),
9
+ * Option.some(5),
10
10
  * Option.map(n => n * 2)
11
11
  * ); // Some(10)
12
12
  *
13
13
  * // Flipped to data-first
14
14
  * const mapFirst = flip(Option.map);
15
- * mapFirst(Option.of(5))(n => n * 2); // Some(10)
15
+ * mapFirst(Option.some(5))(n => n * 2); // Some(10)
16
16
  * ```
17
17
  *
18
18
  * @see {@link uncurry} for converting curried functions to multi-argument functions
@@ -4,7 +4,7 @@
4
4
  * @example
5
5
  * ```ts
6
6
  * identity(42); // 42
7
- * pipe(Option.of(5), Option.fold(() => 0, identity)); // 5
7
+ * pipe(Option.some(5), Option.fold(() => 0, identity)); // 5
8
8
  * ```
9
9
  */
10
10
  export const identity = (a) => a;
@@ -6,7 +6,7 @@
6
6
  * ```ts
7
7
  * // Debugging a pipeline
8
8
  * pipe(
9
- * Option.of(5),
9
+ * Option.some(5),
10
10
  * tap(x => console.log("Before map:", x)),
11
11
  * Option.map(n => n * 2),
12
12
  * tap(x => console.log("After map:", x)),
@@ -28,7 +28,7 @@ export var Arr;
28
28
  * Arr.head([]); // None
29
29
  * ```
30
30
  */
31
- Arr.head = (data) => data.length > 0 ? Option.toSome(data[0]) : Option.toNone();
31
+ Arr.head = (data) => data.length > 0 ? Option.some(data[0]) : Option.none();
32
32
  /**
33
33
  * Returns the last element of an array, or None if the array is empty.
34
34
  *
@@ -38,7 +38,7 @@ export var Arr;
38
38
  * Arr.last([]); // None
39
39
  * ```
40
40
  */
41
- Arr.last = (data) => data.length > 0 ? Option.toSome(data[data.length - 1]) : Option.toNone();
41
+ Arr.last = (data) => data.length > 0 ? Option.some(data[data.length - 1]) : Option.none();
42
42
  /**
43
43
  * Returns all elements except the first, or None if the array is empty.
44
44
  *
@@ -48,7 +48,7 @@ export var Arr;
48
48
  * Arr.tail([]); // None
49
49
  * ```
50
50
  */
51
- Arr.tail = (data) => data.length > 0 ? Option.toSome(data.slice(1)) : Option.toNone();
51
+ Arr.tail = (data) => data.length > 0 ? Option.some(data.slice(1)) : Option.none();
52
52
  /**
53
53
  * Returns all elements except the last, or None if the array is empty.
54
54
  *
@@ -58,7 +58,7 @@ export var Arr;
58
58
  * Arr.init([]); // None
59
59
  * ```
60
60
  */
61
- Arr.init = (data) => data.length > 0 ? Option.toSome(data.slice(0, -1)) : Option.toNone();
61
+ Arr.init = (data) => data.length > 0 ? Option.some(data.slice(0, -1)) : Option.none();
62
62
  // --- Search ---
63
63
  /**
64
64
  * Returns the first element matching the predicate, or None.
@@ -70,7 +70,7 @@ export var Arr;
70
70
  */
71
71
  Arr.findFirst = (predicate) => (data) => {
72
72
  const idx = data.findIndex(predicate);
73
- return idx >= 0 ? Option.toSome(data[idx]) : Option.toNone();
73
+ return idx >= 0 ? Option.some(data[idx]) : Option.none();
74
74
  };
75
75
  /**
76
76
  * Returns the last element matching the predicate, or None.
@@ -83,9 +83,9 @@ export var Arr;
83
83
  Arr.findLast = (predicate) => (data) => {
84
84
  for (let i = data.length - 1; i >= 0; i--) {
85
85
  if (predicate(data[i]))
86
- return Option.toSome(data[i]);
86
+ return Option.some(data[i]);
87
87
  }
88
- return Option.toNone();
88
+ return Option.none();
89
89
  };
90
90
  /**
91
91
  * Returns the index of the first element matching the predicate, or None.
@@ -97,7 +97,7 @@ export var Arr;
97
97
  */
98
98
  Arr.findIndex = (predicate) => (data) => {
99
99
  const idx = data.findIndex(predicate);
100
- return idx >= 0 ? Option.toSome(idx) : Option.toNone();
100
+ return idx >= 0 ? Option.some(idx) : Option.none();
101
101
  };
102
102
  // --- Transform ---
103
103
  /**
@@ -303,7 +303,7 @@ export var Arr;
303
303
  * ```ts
304
304
  * const parseNum = (s: string): Option<number> => {
305
305
  * const n = Number(s);
306
- * return isNaN(n) ? Option.toNone() : Option.of(n);
306
+ * return isNaN(n) ? Option.none() : Option.some(n);
307
307
  * };
308
308
  *
309
309
  * pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
@@ -315,10 +315,10 @@ export var Arr;
315
315
  for (const a of data) {
316
316
  const mapped = f(a);
317
317
  if (Option.isNone(mapped))
318
- return Option.toNone();
318
+ return Option.none();
319
319
  result.push(mapped.value);
320
320
  }
321
- return Option.toSome(result);
321
+ return Option.some(result);
322
322
  };
323
323
  /**
324
324
  * Maps each element to a Result and collects the results.
@@ -328,7 +328,7 @@ export var Arr;
328
328
  * ```ts
329
329
  * pipe(
330
330
  * [1, 2, 3],
331
- * Arr.traverseResult(n => n > 0 ? Result.toOk(n) : Result.toErr("negative"))
331
+ * Arr.traverseResult(n => n > 0 ? Result.ok(n) : Result.err("negative"))
332
332
  * ); // Ok([1, 2, 3])
333
333
  * ```
334
334
  */
@@ -340,7 +340,7 @@ export var Arr;
340
340
  return mapped;
341
341
  result.push(mapped.value);
342
342
  }
343
- return Result.toOk(result);
343
+ return Result.ok(result);
344
344
  };
345
345
  /**
346
346
  * Maps each element to a Task and runs all in parallel.
@@ -349,7 +349,7 @@ export var Arr;
349
349
  * ```ts
350
350
  * pipe(
351
351
  * [1, 2, 3],
352
- * Arr.traverseTask(n => Task.of(n * 2))
352
+ * Arr.traverseTask(n => Task.resolve(n * 2))
353
353
  * )(); // Promise<[2, 4, 6]>
354
354
  * ```
355
355
  */
@@ -360,8 +360,8 @@ export var Arr;
360
360
  *
361
361
  * @example
362
362
  * ```ts
363
- * Arr.sequence([Option.of(1), Option.of(2)]); // Some([1, 2])
364
- * Arr.sequence([Option.of(1), Option.toNone()]); // None
363
+ * Arr.sequence([Option.some(1), Option.some(2)]); // Some([1, 2])
364
+ * Arr.sequence([Option.some(1), Option.none()]); // None
365
365
  * ```
366
366
  */
367
367
  Arr.sequence = (data) => Arr.traverse((a) => a)(data);
@@ -1,19 +1,10 @@
1
1
  import { Result } from "./Result.js";
2
2
  export var Option;
3
3
  (function (Option) {
4
- /**
5
- * Wraps a value in a Some.
6
- *
7
- * @example
8
- * ```ts
9
- * Option.of(42); // Some(42)
10
- * ```
11
- */
12
- Option.of = (value) => Option.toSome(value);
13
4
  /**
14
5
  * Creates a Some containing the given value.
15
6
  */
16
- Option.toSome = (value) => ({ kind: "Some", value });
7
+ Option.some = (value) => ({ kind: "Some", value });
17
8
  /**
18
9
  * Type guard that checks if a Option is Some.
19
10
  */
@@ -21,7 +12,7 @@ export var Option;
21
12
  /**
22
13
  * Creates a None (empty Option).
23
14
  */
24
- Option.toNone = () => ({ kind: "None" });
15
+ Option.none = () => ({ kind: "None" });
25
16
  /**
26
17
  * Type guard that checks if a Option is None.
27
18
  */
@@ -36,7 +27,7 @@ export var Option;
36
27
  * Option.fromNullable(42); // Some(42)
37
28
  * ```
38
29
  */
39
- Option.fromNullable = (value) => value === null || value === undefined ? Option.toNone() : Option.toSome(value);
30
+ Option.fromNullable = (value) => value === null || value === undefined ? Option.none() : Option.some(value);
40
31
  /**
41
32
  * Extracts the value from a Option, returning null if None.
42
33
  */
@@ -49,7 +40,7 @@ export var Option;
49
40
  * Creates a Option from a possibly undefined value.
50
41
  * Returns None if undefined, Some otherwise.
51
42
  */
52
- Option.fromUndefined = (value) => value === undefined ? Option.toNone() : Option.toSome(value);
43
+ Option.fromUndefined = (value) => value === undefined ? Option.none() : Option.some(value);
53
44
  /**
54
45
  * Converts an Option to a Result.
55
46
  * Some becomes Ok, None becomes Err with the provided error.
@@ -57,38 +48,38 @@ export var Option;
57
48
  * @example
58
49
  * ```ts
59
50
  * pipe(
60
- * Option.of(42),
51
+ * Option.some(42),
61
52
  * Option.toResult(() => "Value was missing")
62
53
  * ); // Ok(42)
63
54
  *
64
55
  * pipe(
65
- * Option.toNone(),
56
+ * Option.none(),
66
57
  * Option.toResult(() => "Value was missing")
67
58
  * ); // Err("Value was missing")
68
59
  * ```
69
60
  */
70
- Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result.toOk(data.value) : Result.toErr(onNone());
61
+ Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result.ok(data.value) : Result.err(onNone());
71
62
  /**
72
63
  * Creates an Option from a Result.
73
64
  * Ok becomes Some, Err becomes None (the error is discarded).
74
65
  *
75
66
  * @example
76
67
  * ```ts
77
- * Option.fromResult(Result.toOk(42)); // Some(42)
78
- * Option.fromResult(Result.toErr("oops")); // None
68
+ * Option.fromResult(Result.ok(42)); // Some(42)
69
+ * Option.fromResult(Result.err("oops")); // None
79
70
  * ```
80
71
  */
81
- Option.fromResult = (data) => Result.isOk(data) ? Option.toSome(data.value) : Option.toNone();
72
+ Option.fromResult = (data) => Result.isOk(data) ? Option.some(data.value) : Option.none();
82
73
  /**
83
74
  * Transforms the value inside a Option if it exists.
84
75
  *
85
76
  * @example
86
77
  * ```ts
87
- * pipe(Option.of(5), Option.map(n => n * 2)); // Some(10)
88
- * pipe(Option.toNone(), Option.map(n => n * 2)); // None
78
+ * pipe(Option.some(5), Option.map(n => n * 2)); // Some(10)
79
+ * pipe(Option.none(), Option.map(n => n * 2)); // None
89
80
  * ```
90
81
  */
91
- Option.map = (f) => (data) => Option.isSome(data) ? Option.toSome(f(data.value)) : data;
82
+ Option.map = (f) => (data) => Option.isSome(data) ? Option.some(f(data.value)) : data;
92
83
  /**
93
84
  * Chains Option computations. If the first is Some, passes the value to f.
94
85
  * If the first is None, propagates None.
@@ -97,11 +88,11 @@ export var Option;
97
88
  * ```ts
98
89
  * const parseNumber = (s: string): Option<number> => {
99
90
  * const n = parseInt(s, 10);
100
- * return isNaN(n) ? Option.toNone() : Option.of(n);
91
+ * return isNaN(n) ? Option.none() : Option.some(n);
101
92
  * };
102
93
  *
103
- * pipe(Option.of("42"), Option.chain(parseNumber)); // Some(42)
104
- * pipe(Option.of("abc"), Option.chain(parseNumber)); // None
94
+ * pipe(Option.some("42"), Option.chain(parseNumber)); // Some(42)
95
+ * pipe(Option.some("abc"), Option.chain(parseNumber)); // None
105
96
  * ```
106
97
  */
107
98
  Option.chain = (f) => (data) => Option.isSome(data) ? f(data.value) : data;
@@ -111,7 +102,7 @@ export var Option;
111
102
  * @example
112
103
  * ```ts
113
104
  * pipe(
114
- * Option.of(5),
105
+ * Option.some(5),
115
106
  * Option.fold(
116
107
  * () => "No value",
117
108
  * n => `Value: ${n}`
@@ -140,8 +131,8 @@ export var Option;
140
131
  *
141
132
  * @example
142
133
  * ```ts
143
- * pipe(Option.of(5), Option.getOrElse(0)); // 5
144
- * pipe(Option.toNone(), Option.getOrElse(0)); // 0
134
+ * pipe(Option.some(5), Option.getOrElse(0)); // 5
135
+ * pipe(Option.none(), Option.getOrElse(0)); // 0
145
136
  * ```
146
137
  */
147
138
  Option.getOrElse = (defaultValue) => (data) => Option.isSome(data) ? data.value : defaultValue;
@@ -152,7 +143,7 @@ export var Option;
152
143
  * @example
153
144
  * ```ts
154
145
  * pipe(
155
- * Option.of(5),
146
+ * Option.some(5),
156
147
  * Option.tap(n => console.log("Value:", n)),
157
148
  * Option.map(n => n * 2)
158
149
  * );
@@ -169,11 +160,11 @@ export var Option;
169
160
  *
170
161
  * @example
171
162
  * ```ts
172
- * pipe(Option.of(5), Option.filter(n => n > 3)); // Some(5)
173
- * pipe(Option.of(2), Option.filter(n => n > 3)); // None
163
+ * pipe(Option.some(5), Option.filter(n => n > 3)); // Some(5)
164
+ * pipe(Option.some(2), Option.filter(n => n > 3)); // None
174
165
  * ```
175
166
  */
176
- Option.filter = (predicate) => (data) => Option.isSome(data) && predicate(data.value) ? data : Option.toNone();
167
+ Option.filter = (predicate) => (data) => Option.isSome(data) && predicate(data.value) ? data : Option.none();
177
168
  /**
178
169
  * Recovers from a None by providing a fallback Option.
179
170
  */
@@ -185,11 +176,11 @@ export var Option;
185
176
  * ```ts
186
177
  * const add = (a: number) => (b: number) => a + b;
187
178
  * pipe(
188
- * Option.of(add),
189
- * Option.ap(Option.of(5)),
190
- * Option.ap(Option.of(3))
179
+ * Option.some(add),
180
+ * Option.ap(Option.some(5)),
181
+ * Option.ap(Option.some(3))
191
182
  * ); // Some(8)
192
183
  * ```
193
184
  */
194
- Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.toSome(data.value(arg.value)) : Option.toNone();
185
+ Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.some(data.value(arg.value)) : Option.none();
195
186
  })(Option || (Option = {}));
@@ -87,7 +87,7 @@ export var Rec;
87
87
  * pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
88
88
  * ```
89
89
  */
90
- Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option.toSome(data[key]) : Option.toNone();
90
+ Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option.some(data[key]) : Option.none();
91
91
  /**
92
92
  * Returns all keys of a record.
93
93
  */
@@ -22,10 +22,6 @@ export var RemoteData;
22
22
  kind: "Success",
23
23
  value,
24
24
  });
25
- /**
26
- * Wraps a value in a Success RemoteData. Alias for `success`.
27
- */
28
- RemoteData.of = RemoteData.success;
29
25
  /**
30
26
  * Type guard that checks if a RemoteData is NotAsked.
31
27
  */
@@ -47,7 +43,7 @@ export var RemoteData;
47
43
  *
48
44
  * @example
49
45
  * ```ts
50
- * pipe(RemoteData.of(5), RemoteData.map(n => n * 2)); // Success(10)
46
+ * pipe(RemoteData.success(5), RemoteData.map(n => n * 2)); // Success(10)
51
47
  * pipe(RemoteData.loading(), RemoteData.map(n => n * 2)); // Loading
52
48
  * ```
53
49
  */
@@ -68,8 +64,8 @@ export var RemoteData;
68
64
  * @example
69
65
  * ```ts
70
66
  * pipe(
71
- * RemoteData.of(5),
72
- * RemoteData.chain(n => n > 0 ? RemoteData.of(n) : RemoteData.failure("negative"))
67
+ * RemoteData.success(5),
68
+ * RemoteData.chain(n => n > 0 ? RemoteData.success(n) : RemoteData.failure("negative"))
73
69
  * );
74
70
  * ```
75
71
  */
@@ -81,9 +77,9 @@ export var RemoteData;
81
77
  * ```ts
82
78
  * const add = (a: number) => (b: number) => a + b;
83
79
  * pipe(
84
- * RemoteData.of(add),
85
- * RemoteData.ap(RemoteData.of(5)),
86
- * RemoteData.ap(RemoteData.of(3))
80
+ * RemoteData.success(add),
81
+ * RemoteData.ap(RemoteData.success(5)),
82
+ * RemoteData.ap(RemoteData.success(3))
87
83
  * ); // Success(8)
88
84
  * ```
89
85
  */
@@ -160,7 +156,7 @@ export var RemoteData;
160
156
  *
161
157
  * @example
162
158
  * ```ts
163
- * pipe(RemoteData.of(5), RemoteData.getOrElse(0)); // 5
159
+ * pipe(RemoteData.success(5), RemoteData.getOrElse(0)); // 5
164
160
  * pipe(RemoteData.loading(), RemoteData.getOrElse(0)); // 0
165
161
  * ```
166
162
  */
@@ -171,7 +167,7 @@ export var RemoteData;
171
167
  * @example
172
168
  * ```ts
173
169
  * pipe(
174
- * RemoteData.of(5),
170
+ * RemoteData.success(5),
175
171
  * RemoteData.tap(n => console.log("Value:", n)),
176
172
  * RemoteData.map(n => n * 2)
177
173
  * );
@@ -199,7 +195,7 @@ export var RemoteData;
199
195
  * @example
200
196
  * ```ts
201
197
  * pipe(
202
- * RemoteData.of(42),
198
+ * RemoteData.success(42),
203
199
  * RemoteData.toResult(() => "not loaded")
204
200
  * ); // Ok(42)
205
201
  * ```
@@ -1,22 +1,13 @@
1
1
  export var Result;
2
2
  (function (Result) {
3
3
  /**
4
- * Wraps a value in a successful Result (Ok).
5
- *
6
- * @example
7
- * ```ts
8
- * Result.of(42); // Ok(42)
9
- * ```
4
+ * Creates a successful Result with the given value.
10
5
  */
11
- Result.of = (value) => Result.toOk(value);
6
+ Result.ok = (value) => ({ kind: "Ok", value });
12
7
  /**
13
8
  * Creates a failed Result with the given error.
14
9
  */
15
- Result.toErr = (error) => ({ kind: "Error", error });
16
- /**
17
- * Creates a successful Result with the given value.
18
- */
19
- Result.toOk = (value) => ({ kind: "Ok", value });
10
+ Result.err = (error) => ({ kind: "Error", error });
20
11
  /**
21
12
  * Type guard that checks if an Result is Ok.
22
13
  */
@@ -40,10 +31,10 @@ export var Result;
40
31
  */
41
32
  Result.tryCatch = (f, onError) => {
42
33
  try {
43
- return Result.toOk(f());
34
+ return Result.ok(f());
44
35
  }
45
36
  catch (e) {
46
- return Result.toErr(onError(e));
37
+ return Result.err(onError(e));
47
38
  }
48
39
  };
49
40
  /**
@@ -51,20 +42,20 @@ export var Result;
51
42
  *
52
43
  * @example
53
44
  * ```ts
54
- * pipe(Result.of(5), Result.map(n => n * 2)); // Ok(10)
55
- * pipe(Result.toErr("error"), Result.map(n => n * 2)); // Err("error")
45
+ * pipe(Result.ok(5), Result.map(n => n * 2)); // Ok(10)
46
+ * pipe(Result.err("error"), Result.map(n => n * 2)); // Err("error")
56
47
  * ```
57
48
  */
58
- Result.map = (f) => (data) => Result.isOk(data) ? Result.toOk(f(data.value)) : data;
49
+ Result.map = (f) => (data) => Result.isOk(data) ? Result.ok(f(data.value)) : data;
59
50
  /**
60
51
  * Transforms the error value inside an Result.
61
52
  *
62
53
  * @example
63
54
  * ```ts
64
- * pipe(Result.toErr("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
55
+ * pipe(Result.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
65
56
  * ```
66
57
  */
67
- Result.mapError = (f) => (data) => Result.isErr(data) ? Result.toErr(f(data.error)) : data;
58
+ Result.mapError = (f) => (data) => Result.isErr(data) ? Result.err(f(data.error)) : data;
68
59
  /**
69
60
  * Chains Result computations. If the first is Ok, passes the value to f.
70
61
  * If the first is Err, propagates the error.
@@ -72,10 +63,10 @@ export var Result;
72
63
  * @example
73
64
  * ```ts
74
65
  * const validatePositive = (n: number): Result<string, number> =>
75
- * n > 0 ? Result.of(n) : Result.toErr("Must be positive");
66
+ * n > 0 ? Result.ok(n) : Result.err("Must be positive");
76
67
  *
77
- * pipe(Result.of(5), Result.chain(validatePositive)); // Ok(5)
78
- * pipe(Result.of(-1), Result.chain(validatePositive)); // Err("Must be positive")
68
+ * pipe(Result.ok(5), Result.chain(validatePositive)); // Ok(5)
69
+ * pipe(Result.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")
79
70
  * ```
80
71
  */
81
72
  Result.chain = (f) => (data) => Result.isOk(data) ? f(data.value) : data;
@@ -85,7 +76,7 @@ export var Result;
85
76
  * @example
86
77
  * ```ts
87
78
  * pipe(
88
- * Result.of(5),
79
+ * Result.ok(5),
89
80
  * Result.fold(
90
81
  * e => `Error: ${e}`,
91
82
  * n => `Value: ${n}`
@@ -114,8 +105,8 @@ export var Result;
114
105
  *
115
106
  * @example
116
107
  * ```ts
117
- * pipe(Result.of(5), Result.getOrElse(0)); // 5
118
- * pipe(Result.toErr("error"), Result.getOrElse(0)); // 0
108
+ * pipe(Result.ok(5), Result.getOrElse(0)); // 5
109
+ * pipe(Result.err("error"), Result.getOrElse(0)); // 0
119
110
  * ```
120
111
  */
121
112
  Result.getOrElse = (defaultValue) => (data) => Result.isOk(data) ? data.value : defaultValue;
@@ -126,7 +117,7 @@ export var Result;
126
117
  * @example
127
118
  * ```ts
128
119
  * pipe(
129
- * Result.of(5),
120
+ * Result.ok(5),
130
121
  * Result.tap(n => console.log("Value:", n)),
131
122
  * Result.map(n => n * 2)
132
123
  * );
@@ -151,8 +142,8 @@ export var Result;
151
142
  *
152
143
  * @example
153
144
  * ```ts
154
- * Result.toOption(Result.toOk(42)); // Some(42)
155
- * Result.toOption(Result.toErr("oops")); // None
145
+ * Result.toOption(Result.ok(42)); // Some(42)
146
+ * Result.toOption(Result.err("oops")); // None
156
147
  * ```
157
148
  */
158
149
  Result.toOption = (data) => Result.isOk(data) ? { kind: "Some", value: data.value } : { kind: "None" };
@@ -163,11 +154,11 @@ export var Result;
163
154
  * ```ts
164
155
  * const add = (a: number) => (b: number) => a + b;
165
156
  * pipe(
166
- * Result.of(add),
167
- * Result.ap(Result.of(5)),
168
- * Result.ap(Result.of(3))
157
+ * Result.ok(add),
158
+ * Result.ap(Result.ok(5)),
159
+ * Result.ap(Result.ok(3))
169
160
  * ); // Ok(8)
170
161
  * ```
171
162
  */
172
- Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.toOk(data.value(arg.value)) : Result.isErr(data) ? data : arg;
163
+ Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.ok(data.value(arg.value)) : Result.isErr(data) ? data : arg;
173
164
  })(Result || (Result = {}));