@onrails/maybe 0.1.4 → 0.2.1
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/DESIGN.md +1 -3
- package/dist/fluent.cjs +25 -51
- package/dist/fluent.cjs.map +1 -1
- package/dist/fluent.d.cts +13 -3
- package/dist/fluent.d.ts +13 -3
- package/dist/fluent.js +25 -51
- package/dist/fluent.js.map +1 -1
- package/dist/index.cjs +16 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +79 -43
- package/dist/index.d.ts +79 -43
- package/dist/index.js +17 -30
- package/dist/index.js.map +1 -1
- package/dist/interop.cjs +15 -3
- package/dist/interop.cjs.map +1 -1
- package/dist/interop.d.cts +14 -4
- package/dist/interop.d.ts +14 -4
- package/dist/interop.js +16 -4
- package/dist/interop.js.map +1 -1
- package/package.json +4 -3
- package/dist/types-vkiMAo4w.d.cts +0 -15
- package/dist/types-vkiMAo4w.d.ts +0 -15
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tagged optional value — a discriminated union over `_tag` where absence
|
|
3
|
+
* (`None`) is an expected outcome, not a failure. Construct with {@link some} /
|
|
4
|
+
* {@link none}, narrow with {@link isSome} / {@link isNone}, and collapse with
|
|
5
|
+
* {@link match}.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* function findUser(id: string): Maybe<User> {
|
|
10
|
+
* return fromNullable(db.users.get(id));
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
type Maybe<T> = {
|
|
15
|
+
readonly _tag: "Some";
|
|
16
|
+
readonly value: T;
|
|
17
|
+
} | {
|
|
18
|
+
readonly _tag: "None";
|
|
19
|
+
};
|
|
20
|
+
/** The `Some` branch of a {@link Maybe} — a present value tagged `"Some"`. */
|
|
21
|
+
type Some<T> = Extract<Maybe<T>, {
|
|
22
|
+
_tag: "Some";
|
|
23
|
+
}>;
|
|
24
|
+
/** The `None` branch of a {@link Maybe} — expected absence, tagged `"None"`. */
|
|
25
|
+
type None = Extract<Maybe<never>, {
|
|
26
|
+
_tag: "None";
|
|
27
|
+
}>;
|
|
3
28
|
/**
|
|
4
29
|
* Lifts a value into the `Some` branch.
|
|
5
30
|
*
|
|
@@ -9,11 +34,6 @@ import { M as Maybe, N as None, S as Some } from './types-vkiMAo4w.cjs';
|
|
|
9
34
|
* ```
|
|
10
35
|
*/
|
|
11
36
|
declare const some: <T>(value: T) => Maybe<T>;
|
|
12
|
-
/**
|
|
13
|
-
* Fantasy Land `of` — alias of {@link some}.
|
|
14
|
-
* @deprecated Use {@link some} instead.
|
|
15
|
-
*/
|
|
16
|
-
declare const of: <T>(value: T) => Maybe<T>;
|
|
17
37
|
/**
|
|
18
38
|
* The `None` value — represents expected absence. Not a failure.
|
|
19
39
|
*
|
|
@@ -36,6 +56,13 @@ declare const none: <T = never>() => Maybe<T>;
|
|
|
36
56
|
declare const isSome: <T>(maybe: Maybe<T>) => maybe is Some<T>;
|
|
37
57
|
/**
|
|
38
58
|
* Type-narrowing predicate: returns `true` when the value is `None`.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* if (isNone(fromNullable(user))) {
|
|
63
|
+
* return redirect("/login"); // narrowed to None branch
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
39
66
|
*/
|
|
40
67
|
declare const isNone: <T>(maybe: Maybe<T>) => maybe is None;
|
|
41
68
|
/**
|
|
@@ -44,7 +71,7 @@ declare const isNone: <T>(maybe: Maybe<T>) => maybe is None;
|
|
|
44
71
|
*
|
|
45
72
|
* @example
|
|
46
73
|
* ```ts
|
|
47
|
-
* fromNullable(
|
|
74
|
+
* fromNullable(cache.get(key)); // Maybe<V>
|
|
48
75
|
* fromNullable(null); // None
|
|
49
76
|
* ```
|
|
50
77
|
*/
|
|
@@ -58,8 +85,10 @@ declare const fromNullable: <T>(value: T | null | undefined) => Maybe<T>;
|
|
|
58
85
|
* pipe(fromNullable(input), map((s) => s.trim())); // curried
|
|
59
86
|
* ```
|
|
60
87
|
*/
|
|
61
|
-
declare
|
|
62
|
-
|
|
88
|
+
declare const map: {
|
|
89
|
+
<T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U>;
|
|
90
|
+
<T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;
|
|
91
|
+
};
|
|
63
92
|
/**
|
|
64
93
|
* Canonical bind for `Maybe`. Chains a `Maybe`-returning step;
|
|
65
94
|
* short-circuits on `None`. Dual-form.
|
|
@@ -71,16 +100,33 @@ declare function map<T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;
|
|
|
71
100
|
* );
|
|
72
101
|
* ```
|
|
73
102
|
*/
|
|
74
|
-
declare
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
103
|
+
declare const flatMap: {
|
|
104
|
+
<T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;
|
|
105
|
+
<T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Neverthrow-compat alias of {@link flatMap} — same dual-form shape. Prefer
|
|
109
|
+
* {@link flatMap} as the canonical name; reach for `andThen` only when porting
|
|
110
|
+
* neverthrow code or matching its vocabulary.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* andThen(fromNullable(user), (u) =>
|
|
115
|
+
* u.active ? some(u) : none(),
|
|
116
|
+
* );
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
declare const andThen: {
|
|
120
|
+
<T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;
|
|
121
|
+
<T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;
|
|
122
|
+
};
|
|
78
123
|
/**
|
|
79
124
|
* Terminal collapse — fold both branches into a single value. Positional,
|
|
80
125
|
* dual-form. Same shape as `Result.match` for sibling consistency.
|
|
81
126
|
*
|
|
82
|
-
* For files that also import `match` from `ts-pattern`,
|
|
83
|
-
*
|
|
127
|
+
* For files that also import `match` from `ts-pattern`, use a namespace
|
|
128
|
+
* import (`import * as Maybe from "@onrails/maybe"` → `Maybe.match`) to
|
|
129
|
+
* dissolve the collision.
|
|
84
130
|
*
|
|
85
131
|
* @example
|
|
86
132
|
* ```ts
|
|
@@ -91,28 +137,14 @@ declare const andThen: typeof flatMap;
|
|
|
91
137
|
* );
|
|
92
138
|
* ```
|
|
93
139
|
*/
|
|
94
|
-
declare
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* Returns the `Some` value, or `defaultValue` when the value is `None`.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* unwrapOr(fromNullable(user), { name: "guest" });
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
declare const unwrapOr: <T>(maybe: Maybe<T>, defaultValue: T) => T;
|
|
110
|
-
/**
|
|
111
|
-
* Returns the `Some` value, or `defaultValue` when the value is `None`.
|
|
112
|
-
*
|
|
113
|
-
* @deprecated Use canonical {@link unwrapOr} instead.
|
|
114
|
-
*/
|
|
115
|
-
declare const getOrElse: <T>(maybe: Maybe<T>, defaultValue: T) => T;
|
|
140
|
+
declare const match: {
|
|
141
|
+
<T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U;
|
|
142
|
+
<T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;
|
|
143
|
+
};
|
|
144
|
+
declare const unwrapOr: {
|
|
145
|
+
<T>(maybe: Maybe<T>, defaultValue: T): T;
|
|
146
|
+
<T>(defaultValue: T): (maybe: Maybe<T>) => T;
|
|
147
|
+
};
|
|
116
148
|
/**
|
|
117
149
|
* Unwraps the `Some` value or throws if the value is `None`.
|
|
118
150
|
*
|
|
@@ -167,8 +199,10 @@ declare const optional: <T, U>(value: T | null | undefined, fn: (value: T) => Ma
|
|
|
167
199
|
* pipe(fromNullable(row), tap((r) => sink.push(r))); // curried — collect on Some
|
|
168
200
|
* ```
|
|
169
201
|
*/
|
|
170
|
-
declare
|
|
171
|
-
|
|
202
|
+
declare const tap: {
|
|
203
|
+
<T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T>;
|
|
204
|
+
<T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;
|
|
205
|
+
};
|
|
172
206
|
/**
|
|
173
207
|
* Run a side effect when the `Maybe` is `None`, pass it through unchanged.
|
|
174
208
|
* `Some` is a no-op. The `None`-side mirror of {@link tap}. Dual-form.
|
|
@@ -179,7 +213,9 @@ declare function tap<T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;
|
|
|
179
213
|
* pipe(fromNullable(hit), tapNone(() => metrics.miss())); // curried
|
|
180
214
|
* ```
|
|
181
215
|
*/
|
|
182
|
-
declare
|
|
183
|
-
|
|
216
|
+
declare const tapNone: {
|
|
217
|
+
<T>(maybe: Maybe<T>, fn: () => void): Maybe<T>;
|
|
218
|
+
<T>(fn: () => void): (maybe: Maybe<T>) => Maybe<T>;
|
|
219
|
+
};
|
|
184
220
|
|
|
185
|
-
export { Maybe, None, Some, andThen, compact, compactMap, flatMap, fromNullable,
|
|
221
|
+
export { type Maybe, type None, type Some, andThen, compact, compactMap, flatMap, fromNullable, isNone, isSome, map, match, none, optional, some, tap, tapNone, unwrap, unwrapOr };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tagged optional value — a discriminated union over `_tag` where absence
|
|
3
|
+
* (`None`) is an expected outcome, not a failure. Construct with {@link some} /
|
|
4
|
+
* {@link none}, narrow with {@link isSome} / {@link isNone}, and collapse with
|
|
5
|
+
* {@link match}.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* function findUser(id: string): Maybe<User> {
|
|
10
|
+
* return fromNullable(db.users.get(id));
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
type Maybe<T> = {
|
|
15
|
+
readonly _tag: "Some";
|
|
16
|
+
readonly value: T;
|
|
17
|
+
} | {
|
|
18
|
+
readonly _tag: "None";
|
|
19
|
+
};
|
|
20
|
+
/** The `Some` branch of a {@link Maybe} — a present value tagged `"Some"`. */
|
|
21
|
+
type Some<T> = Extract<Maybe<T>, {
|
|
22
|
+
_tag: "Some";
|
|
23
|
+
}>;
|
|
24
|
+
/** The `None` branch of a {@link Maybe} — expected absence, tagged `"None"`. */
|
|
25
|
+
type None = Extract<Maybe<never>, {
|
|
26
|
+
_tag: "None";
|
|
27
|
+
}>;
|
|
3
28
|
/**
|
|
4
29
|
* Lifts a value into the `Some` branch.
|
|
5
30
|
*
|
|
@@ -9,11 +34,6 @@ import { M as Maybe, N as None, S as Some } from './types-vkiMAo4w.js';
|
|
|
9
34
|
* ```
|
|
10
35
|
*/
|
|
11
36
|
declare const some: <T>(value: T) => Maybe<T>;
|
|
12
|
-
/**
|
|
13
|
-
* Fantasy Land `of` — alias of {@link some}.
|
|
14
|
-
* @deprecated Use {@link some} instead.
|
|
15
|
-
*/
|
|
16
|
-
declare const of: <T>(value: T) => Maybe<T>;
|
|
17
37
|
/**
|
|
18
38
|
* The `None` value — represents expected absence. Not a failure.
|
|
19
39
|
*
|
|
@@ -36,6 +56,13 @@ declare const none: <T = never>() => Maybe<T>;
|
|
|
36
56
|
declare const isSome: <T>(maybe: Maybe<T>) => maybe is Some<T>;
|
|
37
57
|
/**
|
|
38
58
|
* Type-narrowing predicate: returns `true` when the value is `None`.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* if (isNone(fromNullable(user))) {
|
|
63
|
+
* return redirect("/login"); // narrowed to None branch
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
39
66
|
*/
|
|
40
67
|
declare const isNone: <T>(maybe: Maybe<T>) => maybe is None;
|
|
41
68
|
/**
|
|
@@ -44,7 +71,7 @@ declare const isNone: <T>(maybe: Maybe<T>) => maybe is None;
|
|
|
44
71
|
*
|
|
45
72
|
* @example
|
|
46
73
|
* ```ts
|
|
47
|
-
* fromNullable(
|
|
74
|
+
* fromNullable(cache.get(key)); // Maybe<V>
|
|
48
75
|
* fromNullable(null); // None
|
|
49
76
|
* ```
|
|
50
77
|
*/
|
|
@@ -58,8 +85,10 @@ declare const fromNullable: <T>(value: T | null | undefined) => Maybe<T>;
|
|
|
58
85
|
* pipe(fromNullable(input), map((s) => s.trim())); // curried
|
|
59
86
|
* ```
|
|
60
87
|
*/
|
|
61
|
-
declare
|
|
62
|
-
|
|
88
|
+
declare const map: {
|
|
89
|
+
<T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U>;
|
|
90
|
+
<T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;
|
|
91
|
+
};
|
|
63
92
|
/**
|
|
64
93
|
* Canonical bind for `Maybe`. Chains a `Maybe`-returning step;
|
|
65
94
|
* short-circuits on `None`. Dual-form.
|
|
@@ -71,16 +100,33 @@ declare function map<T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;
|
|
|
71
100
|
* );
|
|
72
101
|
* ```
|
|
73
102
|
*/
|
|
74
|
-
declare
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
103
|
+
declare const flatMap: {
|
|
104
|
+
<T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;
|
|
105
|
+
<T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Neverthrow-compat alias of {@link flatMap} — same dual-form shape. Prefer
|
|
109
|
+
* {@link flatMap} as the canonical name; reach for `andThen` only when porting
|
|
110
|
+
* neverthrow code or matching its vocabulary.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* andThen(fromNullable(user), (u) =>
|
|
115
|
+
* u.active ? some(u) : none(),
|
|
116
|
+
* );
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
declare const andThen: {
|
|
120
|
+
<T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;
|
|
121
|
+
<T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;
|
|
122
|
+
};
|
|
78
123
|
/**
|
|
79
124
|
* Terminal collapse — fold both branches into a single value. Positional,
|
|
80
125
|
* dual-form. Same shape as `Result.match` for sibling consistency.
|
|
81
126
|
*
|
|
82
|
-
* For files that also import `match` from `ts-pattern`,
|
|
83
|
-
*
|
|
127
|
+
* For files that also import `match` from `ts-pattern`, use a namespace
|
|
128
|
+
* import (`import * as Maybe from "@onrails/maybe"` → `Maybe.match`) to
|
|
129
|
+
* dissolve the collision.
|
|
84
130
|
*
|
|
85
131
|
* @example
|
|
86
132
|
* ```ts
|
|
@@ -91,28 +137,14 @@ declare const andThen: typeof flatMap;
|
|
|
91
137
|
* );
|
|
92
138
|
* ```
|
|
93
139
|
*/
|
|
94
|
-
declare
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* Returns the `Some` value, or `defaultValue` when the value is `None`.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* unwrapOr(fromNullable(user), { name: "guest" });
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
declare const unwrapOr: <T>(maybe: Maybe<T>, defaultValue: T) => T;
|
|
110
|
-
/**
|
|
111
|
-
* Returns the `Some` value, or `defaultValue` when the value is `None`.
|
|
112
|
-
*
|
|
113
|
-
* @deprecated Use canonical {@link unwrapOr} instead.
|
|
114
|
-
*/
|
|
115
|
-
declare const getOrElse: <T>(maybe: Maybe<T>, defaultValue: T) => T;
|
|
140
|
+
declare const match: {
|
|
141
|
+
<T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U;
|
|
142
|
+
<T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;
|
|
143
|
+
};
|
|
144
|
+
declare const unwrapOr: {
|
|
145
|
+
<T>(maybe: Maybe<T>, defaultValue: T): T;
|
|
146
|
+
<T>(defaultValue: T): (maybe: Maybe<T>) => T;
|
|
147
|
+
};
|
|
116
148
|
/**
|
|
117
149
|
* Unwraps the `Some` value or throws if the value is `None`.
|
|
118
150
|
*
|
|
@@ -167,8 +199,10 @@ declare const optional: <T, U>(value: T | null | undefined, fn: (value: T) => Ma
|
|
|
167
199
|
* pipe(fromNullable(row), tap((r) => sink.push(r))); // curried — collect on Some
|
|
168
200
|
* ```
|
|
169
201
|
*/
|
|
170
|
-
declare
|
|
171
|
-
|
|
202
|
+
declare const tap: {
|
|
203
|
+
<T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T>;
|
|
204
|
+
<T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;
|
|
205
|
+
};
|
|
172
206
|
/**
|
|
173
207
|
* Run a side effect when the `Maybe` is `None`, pass it through unchanged.
|
|
174
208
|
* `Some` is a no-op. The `None`-side mirror of {@link tap}. Dual-form.
|
|
@@ -179,7 +213,9 @@ declare function tap<T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;
|
|
|
179
213
|
* pipe(fromNullable(hit), tapNone(() => metrics.miss())); // curried
|
|
180
214
|
* ```
|
|
181
215
|
*/
|
|
182
|
-
declare
|
|
183
|
-
|
|
216
|
+
declare const tapNone: {
|
|
217
|
+
<T>(maybe: Maybe<T>, fn: () => void): Maybe<T>;
|
|
218
|
+
<T>(fn: () => void): (maybe: Maybe<T>) => Maybe<T>;
|
|
219
|
+
};
|
|
184
220
|
|
|
185
|
-
export { Maybe, None, Some, andThen, compact, compactMap, flatMap, fromNullable,
|
|
221
|
+
export { type Maybe, type None, type Some, andThen, compact, compactMap, flatMap, fromNullable, isNone, isSome, map, match, none, optional, some, tap, tapNone, unwrap, unwrapOr };
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,27 @@
|
|
|
1
|
+
// src/internal/dual.ts
|
|
2
|
+
function dual(arity, body) {
|
|
3
|
+
const dispatch = (...args) => {
|
|
4
|
+
if (args.length >= arity) return body(...args);
|
|
5
|
+
return (self) => body(self, ...args);
|
|
6
|
+
};
|
|
7
|
+
return dispatch;
|
|
8
|
+
}
|
|
9
|
+
|
|
1
10
|
// src/maybe.ts
|
|
2
11
|
var some = (value) => ({ _tag: "Some", value });
|
|
3
|
-
var of = some;
|
|
4
12
|
var none = () => ({ _tag: "None" });
|
|
5
13
|
var isSome = (maybe) => maybe._tag === "Some";
|
|
6
14
|
var isNone = (maybe) => maybe._tag === "None";
|
|
7
15
|
var fromNullable = (value) => value == null ? none() : some(value);
|
|
8
16
|
var mapImpl = (maybe, fn) => isSome(maybe) ? some(fn(maybe.value)) : none();
|
|
9
|
-
|
|
10
|
-
if (args.length === 2) return mapImpl(args[0], args[1]);
|
|
11
|
-
const fn = args[0];
|
|
12
|
-
return (maybe) => mapImpl(maybe, fn);
|
|
13
|
-
}
|
|
17
|
+
var map = dual(2, mapImpl);
|
|
14
18
|
var flatMapImpl = (maybe, fn) => isSome(maybe) ? fn(maybe.value) : none();
|
|
15
|
-
|
|
16
|
-
if (args.length === 2) return flatMapImpl(args[0], args[1]);
|
|
17
|
-
const fn = args[0];
|
|
18
|
-
return (maybe) => flatMapImpl(maybe, fn);
|
|
19
|
-
}
|
|
19
|
+
var flatMap = dual(2, flatMapImpl);
|
|
20
20
|
var andThen = flatMap;
|
|
21
21
|
var matchImpl = (maybe, onSome, onNone) => isSome(maybe) ? onSome(maybe.value) : onNone();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return (maybe) => matchImpl(maybe, onSome, onNone);
|
|
26
|
-
}
|
|
27
|
-
var matchMaybe = match;
|
|
28
|
-
var unwrapOr = (maybe, defaultValue) => isSome(maybe) ? maybe.value : defaultValue;
|
|
29
|
-
var getOrElse = unwrapOr;
|
|
22
|
+
var match = dual(3, matchImpl);
|
|
23
|
+
var unwrapOrImpl = (maybe, defaultValue) => isSome(maybe) ? maybe.value : defaultValue;
|
|
24
|
+
var unwrapOr = dual(2, unwrapOrImpl);
|
|
30
25
|
var unwrap = (maybe) => {
|
|
31
26
|
if (isNone(maybe)) {
|
|
32
27
|
throw new Error("Called unwrap on None");
|
|
@@ -48,21 +43,13 @@ var tapImpl = (maybe, fn) => {
|
|
|
48
43
|
if (isSome(maybe)) fn(maybe.value);
|
|
49
44
|
return maybe;
|
|
50
45
|
};
|
|
51
|
-
|
|
52
|
-
if (args.length === 2) return tapImpl(args[0], args[1]);
|
|
53
|
-
const fn = args[0];
|
|
54
|
-
return (maybe) => tapImpl(maybe, fn);
|
|
55
|
-
}
|
|
46
|
+
var tap = dual(2, tapImpl);
|
|
56
47
|
var tapNoneImpl = (maybe, fn) => {
|
|
57
48
|
if (isNone(maybe)) fn();
|
|
58
49
|
return maybe;
|
|
59
50
|
};
|
|
60
|
-
|
|
61
|
-
if (args.length === 2) return tapNoneImpl(args[0], args[1]);
|
|
62
|
-
const fn = args[0];
|
|
63
|
-
return (maybe) => tapNoneImpl(maybe, fn);
|
|
64
|
-
}
|
|
51
|
+
var tapNone = dual(2, tapNoneImpl);
|
|
65
52
|
|
|
66
|
-
export { andThen, compact, compactMap, flatMap, fromNullable,
|
|
53
|
+
export { andThen, compact, compactMap, flatMap, fromNullable, isNone, isSome, map, match, none, optional, some, tap, tapNone, unwrap, unwrapOr };
|
|
67
54
|
//# sourceMappingURL=index.js.map
|
|
68
55
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/maybe.ts"],"names":[],"mappings":"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","file":"index.js","sourcesContent":["import type { Maybe, None, Some } from \"./types.js\";\n\nexport type { Maybe, None, Some } from \"./types.js\";\n\n/**\n * Lifts a value into the `Some` branch.\n *\n * @example\n * ```ts\n * const m = some(1); // Maybe<number>\n * ```\n */\nexport const some = <T>(value: T): Maybe<T> => ({ _tag: \"Some\", value });\n\n/**\n * Fantasy Land `of` — alias of {@link some}.\n * @deprecated Use {@link some} instead.\n */\nexport const of = some;\n\n/**\n * The `None` value — represents expected absence. Not a failure.\n *\n * @example\n * ```ts\n * const cached: Maybe<User> = cache.has(id) ? some(cache.get(id)) : none();\n * ```\n */\nexport const none = <T = never>(): Maybe<T> => ({ _tag: \"None\" });\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `Some`.\n *\n * @example\n * ```ts\n * if (isSome(m)) {\n * console.log(m.value); // narrowed to Some branch\n * }\n * ```\n */\nexport const isSome = <T>(maybe: Maybe<T>): maybe is Some<T> => maybe._tag === \"Some\";\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `None`.\n */\nexport const isNone = <T>(maybe: Maybe<T>): maybe is None => maybe._tag === \"None\";\n\n/**\n * Lift a nullable value into `Maybe<T>` — `null` and `undefined` become\n * `None`; anything else becomes `Some`.\n *\n * @example\n * ```ts\n * fromNullable(map.get(key)); // Maybe<V>\n * fromNullable(null); // None\n * ```\n */\nexport const fromNullable = <T>(value: T | null | undefined): Maybe<T> =>\n value == null ? none<T>() : some(value);\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Dual-form helpers — each accepts either shape:\n// data-first: `map(maybe, fn)`\n// curried: `map(fn)(maybe)`\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst mapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U> =>\n isSome(maybe) ? some(fn(maybe.value)) : none<U>();\n\n/**\n * Transform the `Some` value, passing `None` through unchanged. Dual-form.\n *\n * @example\n * ```ts\n * map(some(2), (n) => n * 3); // Some 6 — data-first\n * pipe(fromNullable(input), map((s) => s.trim())); // curried\n * ```\n */\nexport function map<T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U>;\nexport function map<T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;\nexport function map(\n ...args: [Maybe<unknown>, (value: unknown) => unknown] | [(value: unknown) => unknown]\n): unknown {\n if (args.length === 2) return mapImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => mapImpl(maybe, fn);\n}\n\nconst flatMapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U> =>\n isSome(maybe) ? fn(maybe.value) : none<U>();\n\n/**\n * Canonical bind for `Maybe`. Chains a `Maybe`-returning step;\n * short-circuits on `None`. Dual-form.\n *\n * @example\n * ```ts\n * flatMap(fromNullable(user), (u) =>\n * u.active ? some(u) : none(),\n * );\n * ```\n */\nexport function flatMap<T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;\nexport function flatMap<T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;\nexport function flatMap(\n ...args:\n | [Maybe<unknown>, (value: unknown) => Maybe<unknown>]\n | [(value: unknown) => Maybe<unknown>]\n): unknown {\n if (args.length === 2) return flatMapImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => flatMapImpl(maybe, fn);\n}\n\n/** Alias for {@link flatMap} — same dual-form shape. */\nexport const andThen = flatMap;\n\nconst matchImpl = <T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U =>\n isSome(maybe) ? onSome(maybe.value) : onNone();\n\n/**\n * Terminal collapse — fold both branches into a single value. Positional,\n * dual-form. Same shape as `Result.match` for sibling consistency.\n *\n * For files that also import `match` from `ts-pattern`, the alias\n * {@link matchMaybe} is identical.\n *\n * @example\n * ```ts\n * const greeting = match(\n * fromNullable(user),\n * (u) => `hello ${u.name}`,\n * () => \"hello guest\",\n * );\n * ```\n */\nexport function match<T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U;\nexport function match<T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;\nexport function match(\n ...args:\n | [Maybe<unknown>, (value: unknown) => unknown, () => unknown]\n | [(value: unknown) => unknown, () => unknown]\n): unknown {\n if (args.length === 3) return matchImpl(args[0], args[1], args[2]);\n const [onSome, onNone] = args;\n return (maybe: Maybe<unknown>) => matchImpl(maybe, onSome, onNone);\n}\n\n/**\n * Collision-free alias — mirrors `matchResult` on `@onrails/result`.\n * @deprecated Namespace import carriers (e.g., `import * as Maybe from \"@onrails/maybe\"`) and use canonical {@link match} instead.\n */\nexport const matchMaybe = match;\n\n/**\n * Returns the `Some` value, or `defaultValue` when the value is `None`.\n *\n * @example\n * ```ts\n * unwrapOr(fromNullable(user), { name: \"guest\" });\n * ```\n */\nexport const unwrapOr = <T>(maybe: Maybe<T>, defaultValue: T): T =>\n isSome(maybe) ? maybe.value : defaultValue;\n\n/**\n * Returns the `Some` value, or `defaultValue` when the value is `None`.\n *\n * @deprecated Use canonical {@link unwrapOr} instead.\n */\nexport const getOrElse = unwrapOr;\n\n/**\n * Unwraps the `Some` value or throws if the value is `None`.\n *\n * @throws when the value is None — assertion-tier; use {@link match} / {@link unwrapOr} in business logic.\n * Allowed in `*.spec.ts` / `*.test.ts`; flagged elsewhere by the plugins.\n *\n * @example\n * ```ts\n * expect(unwrap(parseUser(raw))).toEqual(expected);\n * ```\n */\nexport const unwrap = <T>(maybe: Maybe<T>): T => {\n if (isNone(maybe)) {\n throw new Error(\"Called unwrap on None\");\n }\n return maybe.value;\n};\n\n/**\n * Drop every `None` and collect the remaining `Some` values into an array.\n *\n * @example\n * ```ts\n * compact([some(1), none(), some(2)]); // [1, 2]\n * ```\n */\nexport const compact = <T>(maybes: readonly Maybe<T>[]): T[] => {\n const out: T[] = [];\n for (const m of maybes) {\n if (isSome(m)) {\n out.push(m.value);\n }\n }\n return out;\n};\n\n/**\n * `compact(items.map(fn))` — map each item to `Maybe`, drop `None`,\n * collect the rest.\n *\n * @example\n * ```ts\n * compactMap(rawRows, (row) =>\n * row.deletedAt ? none() : some(row.id),\n * );\n * // string[]\n * ```\n */\nexport const compactMap = <T, U>(items: readonly T[], fn: (item: T) => Maybe<U>): U[] =>\n compact(items.map(fn));\n\n/**\n * `flatMap(fromNullable(value), fn)` — lift a nullable then bind through\n * a `Maybe`-returning step. Convenient for chaining lookups.\n *\n * @example\n * ```ts\n * optional(rawId, (id) => fromNullable(cache.get(id)));\n * ```\n */\nexport const optional = <T, U>(value: T | null | undefined, fn: (value: T) => Maybe<U>): Maybe<U> =>\n flatMapImpl(fromNullable(value), fn);\n\nconst tapImpl = <T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T> => {\n if (isSome(maybe)) fn(maybe.value);\n return maybe;\n};\n\n/**\n * Run a side effect on the `Some` value, pass the `Maybe` through unchanged.\n * `None` is a no-op. Mirrors `tap` from `@onrails/result`. Dual-form.\n *\n * @example\n * ```ts\n * tap(some(2), (n) => console.log(n)); // logs 2, returns Some 2\n * pipe(fromNullable(row), tap((r) => sink.push(r))); // curried — collect on Some\n * ```\n */\nexport function tap<T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T>;\nexport function tap<T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;\nexport function tap(\n ...args: [Maybe<unknown>, (value: unknown) => void] | [(value: unknown) => void]\n): unknown {\n if (args.length === 2) return tapImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => tapImpl(maybe, fn);\n}\n\nconst tapNoneImpl = <T>(maybe: Maybe<T>, fn: () => void): Maybe<T> => {\n if (isNone(maybe)) fn();\n return maybe;\n};\n\n/**\n * Run a side effect when the `Maybe` is `None`, pass it through unchanged.\n * `Some` is a no-op. The `None`-side mirror of {@link tap}. Dual-form.\n *\n * @example\n * ```ts\n * tapNone(none(), () => metrics.miss()); // runs effect, returns None\n * pipe(fromNullable(hit), tapNone(() => metrics.miss())); // curried\n * ```\n */\nexport function tapNone<T>(maybe: Maybe<T>, fn: () => void): Maybe<T>;\nexport function tapNone<T>(fn: () => void): (maybe: Maybe<T>) => Maybe<T>;\nexport function tapNone(...args: [Maybe<unknown>, () => void] | [() => void]): unknown {\n if (args.length === 2) return tapNoneImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => tapNoneImpl(maybe, fn);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/internal/dual.ts","../src/maybe.ts"],"names":[],"mappings":"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","file":"index.js","sourcesContent":["/**\n * Owning module for the dual-form call idiom used across `@onrails/maybe`:\n * a function accepts either data-first (`map(m, fn)`) or curried data-last\n * (`map(fn)(m)`) invocation, dispatching on argument count against a fixed\n * `arity`.\n *\n * This is a deliberate twin of `packages/result/src/internal/dual.ts` — keep\n * the two in sync. No cross-package imports are allowed in this repo, so the\n * helper is vendored independently in each package rather than shared.\n */\nexport function dual<F>(arity: number, body: (...args: never[]) => unknown): F {\n const dispatch = (...args: unknown[]): unknown => {\n if (args.length >= arity) return (body as (...a: unknown[]) => unknown)(...args);\n return (self: unknown) => (body as (...a: unknown[]) => unknown)(self, ...args);\n };\n // cast: the caller's overloaded type annotation supplies the real signature\n return dispatch as F;\n}\n","import { dual } from \"./internal/dual.js\";\n\n/**\n * Tagged optional value — a discriminated union over `_tag` where absence\n * (`None`) is an expected outcome, not a failure. Construct with {@link some} /\n * {@link none}, narrow with {@link isSome} / {@link isNone}, and collapse with\n * {@link match}.\n *\n * @example\n * ```ts\n * function findUser(id: string): Maybe<User> {\n * return fromNullable(db.users.get(id));\n * }\n * ```\n */\nexport type Maybe<T> = { readonly _tag: \"Some\"; readonly value: T } | { readonly _tag: \"None\" };\n\n/** The `Some` branch of a {@link Maybe} — a present value tagged `\"Some\"`. */\nexport type Some<T> = Extract<Maybe<T>, { _tag: \"Some\" }>;\n\n/** The `None` branch of a {@link Maybe} — expected absence, tagged `\"None\"`. */\nexport type None = Extract<Maybe<never>, { _tag: \"None\" }>;\n\n/**\n * Lifts a value into the `Some` branch.\n *\n * @example\n * ```ts\n * const m = some(1); // Maybe<number>\n * ```\n */\nexport const some = <T>(value: T): Maybe<T> => ({ _tag: \"Some\", value });\n\n/**\n * The `None` value — represents expected absence. Not a failure.\n *\n * @example\n * ```ts\n * const cached: Maybe<User> = cache.has(id) ? some(cache.get(id)) : none();\n * ```\n */\nexport const none = <T = never>(): Maybe<T> => ({ _tag: \"None\" });\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `Some`.\n *\n * @example\n * ```ts\n * if (isSome(m)) {\n * console.log(m.value); // narrowed to Some branch\n * }\n * ```\n */\nexport const isSome = <T>(maybe: Maybe<T>): maybe is Some<T> => maybe._tag === \"Some\";\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `None`.\n *\n * @example\n * ```ts\n * if (isNone(fromNullable(user))) {\n * return redirect(\"/login\"); // narrowed to None branch\n * }\n * ```\n */\nexport const isNone = <T>(maybe: Maybe<T>): maybe is None => maybe._tag === \"None\";\n\n/**\n * Lift a nullable value into `Maybe<T>` — `null` and `undefined` become\n * `None`; anything else becomes `Some`.\n *\n * @example\n * ```ts\n * fromNullable(cache.get(key)); // Maybe<V>\n * fromNullable(null); // None\n * ```\n */\nexport const fromNullable = <T>(value: T | null | undefined): Maybe<T> =>\n value == null ? none<T>() : some(value);\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Dual-form helpers — each accepts either shape:\n// data-first: `map(maybe, fn)`\n// curried: `map(fn)(maybe)`\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst mapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U> =>\n isSome(maybe) ? some(fn(maybe.value)) : none<U>();\n\n/**\n * Transform the `Some` value, passing `None` through unchanged. Dual-form.\n *\n * @example\n * ```ts\n * map(some(2), (n) => n * 3); // Some 6 — data-first\n * pipe(fromNullable(input), map((s) => s.trim())); // curried\n * ```\n */\nexport const map: {\n <T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U>;\n <T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;\n} = dual(2, mapImpl);\n\nconst flatMapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U> =>\n isSome(maybe) ? fn(maybe.value) : none<U>();\n\n/**\n * Canonical bind for `Maybe`. Chains a `Maybe`-returning step;\n * short-circuits on `None`. Dual-form.\n *\n * @example\n * ```ts\n * flatMap(fromNullable(user), (u) =>\n * u.active ? some(u) : none(),\n * );\n * ```\n */\nexport const flatMap: {\n <T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;\n <T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;\n} = dual(2, flatMapImpl);\n\n/**\n * Neverthrow-compat alias of {@link flatMap} — same dual-form shape. Prefer\n * {@link flatMap} as the canonical name; reach for `andThen` only when porting\n * neverthrow code or matching its vocabulary.\n *\n * @example\n * ```ts\n * andThen(fromNullable(user), (u) =>\n * u.active ? some(u) : none(),\n * );\n * ```\n */\nexport const andThen = flatMap;\n\nconst matchImpl = <T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U =>\n isSome(maybe) ? onSome(maybe.value) : onNone();\n\n/**\n * Terminal collapse — fold both branches into a single value. Positional,\n * dual-form. Same shape as `Result.match` for sibling consistency.\n *\n * For files that also import `match` from `ts-pattern`, use a namespace\n * import (`import * as Maybe from \"@onrails/maybe\"` → `Maybe.match`) to\n * dissolve the collision.\n *\n * @example\n * ```ts\n * const greeting = match(\n * fromNullable(user),\n * (u) => `hello ${u.name}`,\n * () => \"hello guest\",\n * );\n * ```\n */\nexport const match: {\n <T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U;\n <T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;\n} = dual(3, matchImpl);\n\nconst unwrapOrImpl = <T>(maybe: Maybe<T>, defaultValue: T): T =>\n isSome(maybe) ? maybe.value : defaultValue;\n\nexport const unwrapOr: {\n <T>(maybe: Maybe<T>, defaultValue: T): T;\n <T>(defaultValue: T): (maybe: Maybe<T>) => T;\n} = dual(2, unwrapOrImpl);\n\n/**\n * Unwraps the `Some` value or throws if the value is `None`.\n *\n * @throws when the value is None — assertion-tier; use {@link match} / {@link unwrapOr} in business logic.\n * Allowed in `*.spec.ts` / `*.test.ts`; flagged elsewhere by the plugins.\n *\n * @example\n * ```ts\n * expect(unwrap(parseUser(raw))).toEqual(expected);\n * ```\n */\nexport const unwrap = <T>(maybe: Maybe<T>): T => {\n if (isNone(maybe)) {\n throw new Error(\"Called unwrap on None\");\n }\n return maybe.value;\n};\n\n/**\n * Drop every `None` and collect the remaining `Some` values into an array.\n *\n * @example\n * ```ts\n * compact([some(1), none(), some(2)]); // [1, 2]\n * ```\n */\nexport const compact = <T>(maybes: readonly Maybe<T>[]): T[] => {\n const out: T[] = [];\n for (const m of maybes) {\n if (isSome(m)) {\n out.push(m.value);\n }\n }\n return out;\n};\n\n/**\n * `compact(items.map(fn))` — map each item to `Maybe`, drop `None`,\n * collect the rest.\n *\n * @example\n * ```ts\n * compactMap(rawRows, (row) =>\n * row.deletedAt ? none() : some(row.id),\n * );\n * // string[]\n * ```\n */\nexport const compactMap = <T, U>(items: readonly T[], fn: (item: T) => Maybe<U>): U[] =>\n compact(items.map(fn));\n\n/**\n * `flatMap(fromNullable(value), fn)` — lift a nullable then bind through\n * a `Maybe`-returning step. Convenient for chaining lookups.\n *\n * @example\n * ```ts\n * optional(rawId, (id) => fromNullable(cache.get(id)));\n * ```\n */\nexport const optional = <T, U>(value: T | null | undefined, fn: (value: T) => Maybe<U>): Maybe<U> =>\n flatMapImpl(fromNullable(value), fn);\n\nconst tapImpl = <T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T> => {\n if (isSome(maybe)) fn(maybe.value);\n return maybe;\n};\n\n/**\n * Run a side effect on the `Some` value, pass the `Maybe` through unchanged.\n * `None` is a no-op. Mirrors `tap` from `@onrails/result`. Dual-form.\n *\n * @example\n * ```ts\n * tap(some(2), (n) => console.log(n)); // logs 2, returns Some 2\n * pipe(fromNullable(row), tap((r) => sink.push(r))); // curried — collect on Some\n * ```\n */\nexport const tap: {\n <T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T>;\n <T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;\n} = dual(2, tapImpl);\n\nconst tapNoneImpl = <T>(maybe: Maybe<T>, fn: () => void): Maybe<T> => {\n if (isNone(maybe)) fn();\n return maybe;\n};\n\n/**\n * Run a side effect when the `Maybe` is `None`, pass it through unchanged.\n * `Some` is a no-op. The `None`-side mirror of {@link tap}. Dual-form.\n *\n * @example\n * ```ts\n * tapNone(none(), () => metrics.miss()); // runs effect, returns None\n * pipe(fromNullable(hit), tapNone(() => metrics.miss())); // curried\n * ```\n */\nexport const tapNone: {\n <T>(maybe: Maybe<T>, fn: () => void): Maybe<T>;\n <T>(fn: () => void): (maybe: Maybe<T>) => Maybe<T>;\n} = dual(2, tapNoneImpl);\n"]}
|
package/dist/interop.cjs
CHANGED
|
@@ -4,6 +4,15 @@ var result = require('@onrails/result');
|
|
|
4
4
|
|
|
5
5
|
// src/interop.ts
|
|
6
6
|
|
|
7
|
+
// src/internal/dual.ts
|
|
8
|
+
function dual(arity, body) {
|
|
9
|
+
const dispatch = (...args) => {
|
|
10
|
+
if (args.length >= arity) return body(...args);
|
|
11
|
+
return (self) => body(self, ...args);
|
|
12
|
+
};
|
|
13
|
+
return dispatch;
|
|
14
|
+
}
|
|
15
|
+
|
|
7
16
|
// src/maybe.ts
|
|
8
17
|
var some = (value) => ({ _tag: "Some", value });
|
|
9
18
|
var none = () => ({ _tag: "None" });
|
|
@@ -12,9 +21,12 @@ var fromNullable = (value) => value == null ? none() : some(value);
|
|
|
12
21
|
|
|
13
22
|
// src/interop.ts
|
|
14
23
|
var noneError = () => ({ _tag: "None" });
|
|
15
|
-
var
|
|
16
|
-
var
|
|
17
|
-
var
|
|
24
|
+
var toResultImpl = (maybe, onNone) => isSome(maybe) ? result.ok(maybe.value) : result.err(onNone());
|
|
25
|
+
var toResult = dual(2, toResultImpl);
|
|
26
|
+
var fromResultImpl = (result$1) => result.isOk(result$1) ? some(result$1.value) : none();
|
|
27
|
+
var fromResult = dual(1, fromResultImpl);
|
|
28
|
+
var nullableToResultImpl = (value, onNone) => toResultImpl(fromNullable(value), onNone);
|
|
29
|
+
var nullableToResult = dual(2, nullableToResultImpl);
|
|
18
30
|
var isNoneError = (result$1) => !result.isOk(result$1) && isNoneErrorValue(result$1.error);
|
|
19
31
|
var isNoneErrorValue = (error) => typeof error === "object" && error !== null && "_tag" in error && error._tag === "None";
|
|
20
32
|
|
package/dist/interop.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/maybe.ts","../src/interop.ts"],"names":["ok","err","result","isOk"],"mappings":";;;;;;;AAYO,IAAM,OAAO,CAAI,KAAA,MAAwB,EAAE,IAAA,EAAM,QAAQ,KAAA,EAAM,CAAA;AAgB/D,IAAM,IAAA,GAAO,OAA4B,EAAE,IAAA,EAAM,MAAA,EAAO,CAAA;AAYxD,IAAM,MAAA,GAAS,CAAI,KAAA,KAAsC,KAAA,CAAM,IAAA,KAAS,MAAA;AAiBxE,IAAM,YAAA,GAAe,CAAI,KAAA,KAC9B,KAAA,IAAS,OAAO,IAAA,EAAQ,GAAI,KAAK,KAAK,CAAA;;;ACpDjC,IAAM,SAAA,GAAY,OAAkB,EAAE,IAAA,EAAM,MAAA,EAAO;AAGnD,IAAM,QAAA,GAAW,CAAO,KAAA,EAAiB,MAAA,KAC9C,MAAA,CAAO,KAAK,CAAA,GAAIA,SAAA,CAAG,KAAA,CAAM,KAAK,CAAA,GAAIC,UAAA,CAAI,QAAQ;AAGzC,IAAM,UAAA,GAAa,CAAOC,QAAA,KAC/BC,WAAA,CAAKD,QAAM,IAAI,IAAA,CAAKA,QAAA,CAAO,KAAK,CAAA,GAAI,IAAA;AAG/B,IAAM,gBAAA,GAAmB,CAC9B,KAAA,EACA,MAAA,KACiB,SAAS,YAAA,CAAa,KAAK,GAAG,MAAM;AAGhD,IAAM,WAAA,GAAc,CAACA,QAAA,KAC1B,CAACC,YAAKD,QAAM,CAAA,IAAK,gBAAA,CAAiBA,QAAA,CAAO,KAAK;AAEhD,IAAM,gBAAA,GAAmB,CAAC,KAAA,KACxB,OAAO,KAAA,KAAU,QAAA,IACjB,KAAA,KAAU,IAAA,IACV,MAAA,IAAU,KAAA,IACT,KAAA,CAA4B,IAAA,KAAS,MAAA","file":"interop.cjs","sourcesContent":["import type { Maybe, None, Some } from \"./types.js\";\n\nexport type { Maybe, None, Some } from \"./types.js\";\n\n/**\n * Lifts a value into the `Some` branch.\n *\n * @example\n * ```ts\n * const m = some(1); // Maybe<number>\n * ```\n */\nexport const some = <T>(value: T): Maybe<T> => ({ _tag: \"Some\", value });\n\n/**\n * Fantasy Land `of` — alias of {@link some}.\n * @deprecated Use {@link some} instead.\n */\nexport const of = some;\n\n/**\n * The `None` value — represents expected absence. Not a failure.\n *\n * @example\n * ```ts\n * const cached: Maybe<User> = cache.has(id) ? some(cache.get(id)) : none();\n * ```\n */\nexport const none = <T = never>(): Maybe<T> => ({ _tag: \"None\" });\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `Some`.\n *\n * @example\n * ```ts\n * if (isSome(m)) {\n * console.log(m.value); // narrowed to Some branch\n * }\n * ```\n */\nexport const isSome = <T>(maybe: Maybe<T>): maybe is Some<T> => maybe._tag === \"Some\";\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `None`.\n */\nexport const isNone = <T>(maybe: Maybe<T>): maybe is None => maybe._tag === \"None\";\n\n/**\n * Lift a nullable value into `Maybe<T>` — `null` and `undefined` become\n * `None`; anything else becomes `Some`.\n *\n * @example\n * ```ts\n * fromNullable(map.get(key)); // Maybe<V>\n * fromNullable(null); // None\n * ```\n */\nexport const fromNullable = <T>(value: T | null | undefined): Maybe<T> =>\n value == null ? none<T>() : some(value);\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Dual-form helpers — each accepts either shape:\n// data-first: `map(maybe, fn)`\n// curried: `map(fn)(maybe)`\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst mapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U> =>\n isSome(maybe) ? some(fn(maybe.value)) : none<U>();\n\n/**\n * Transform the `Some` value, passing `None` through unchanged. Dual-form.\n *\n * @example\n * ```ts\n * map(some(2), (n) => n * 3); // Some 6 — data-first\n * pipe(fromNullable(input), map((s) => s.trim())); // curried\n * ```\n */\nexport function map<T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U>;\nexport function map<T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;\nexport function map(\n ...args: [Maybe<unknown>, (value: unknown) => unknown] | [(value: unknown) => unknown]\n): unknown {\n if (args.length === 2) return mapImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => mapImpl(maybe, fn);\n}\n\nconst flatMapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U> =>\n isSome(maybe) ? fn(maybe.value) : none<U>();\n\n/**\n * Canonical bind for `Maybe`. Chains a `Maybe`-returning step;\n * short-circuits on `None`. Dual-form.\n *\n * @example\n * ```ts\n * flatMap(fromNullable(user), (u) =>\n * u.active ? some(u) : none(),\n * );\n * ```\n */\nexport function flatMap<T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;\nexport function flatMap<T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;\nexport function flatMap(\n ...args:\n | [Maybe<unknown>, (value: unknown) => Maybe<unknown>]\n | [(value: unknown) => Maybe<unknown>]\n): unknown {\n if (args.length === 2) return flatMapImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => flatMapImpl(maybe, fn);\n}\n\n/** Alias for {@link flatMap} — same dual-form shape. */\nexport const andThen = flatMap;\n\nconst matchImpl = <T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U =>\n isSome(maybe) ? onSome(maybe.value) : onNone();\n\n/**\n * Terminal collapse — fold both branches into a single value. Positional,\n * dual-form. Same shape as `Result.match` for sibling consistency.\n *\n * For files that also import `match` from `ts-pattern`, the alias\n * {@link matchMaybe} is identical.\n *\n * @example\n * ```ts\n * const greeting = match(\n * fromNullable(user),\n * (u) => `hello ${u.name}`,\n * () => \"hello guest\",\n * );\n * ```\n */\nexport function match<T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U;\nexport function match<T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;\nexport function match(\n ...args:\n | [Maybe<unknown>, (value: unknown) => unknown, () => unknown]\n | [(value: unknown) => unknown, () => unknown]\n): unknown {\n if (args.length === 3) return matchImpl(args[0], args[1], args[2]);\n const [onSome, onNone] = args;\n return (maybe: Maybe<unknown>) => matchImpl(maybe, onSome, onNone);\n}\n\n/**\n * Collision-free alias — mirrors `matchResult` on `@onrails/result`.\n * @deprecated Namespace import carriers (e.g., `import * as Maybe from \"@onrails/maybe\"`) and use canonical {@link match} instead.\n */\nexport const matchMaybe = match;\n\n/**\n * Returns the `Some` value, or `defaultValue` when the value is `None`.\n *\n * @example\n * ```ts\n * unwrapOr(fromNullable(user), { name: \"guest\" });\n * ```\n */\nexport const unwrapOr = <T>(maybe: Maybe<T>, defaultValue: T): T =>\n isSome(maybe) ? maybe.value : defaultValue;\n\n/**\n * Returns the `Some` value, or `defaultValue` when the value is `None`.\n *\n * @deprecated Use canonical {@link unwrapOr} instead.\n */\nexport const getOrElse = unwrapOr;\n\n/**\n * Unwraps the `Some` value or throws if the value is `None`.\n *\n * @throws when the value is None — assertion-tier; use {@link match} / {@link unwrapOr} in business logic.\n * Allowed in `*.spec.ts` / `*.test.ts`; flagged elsewhere by the plugins.\n *\n * @example\n * ```ts\n * expect(unwrap(parseUser(raw))).toEqual(expected);\n * ```\n */\nexport const unwrap = <T>(maybe: Maybe<T>): T => {\n if (isNone(maybe)) {\n throw new Error(\"Called unwrap on None\");\n }\n return maybe.value;\n};\n\n/**\n * Drop every `None` and collect the remaining `Some` values into an array.\n *\n * @example\n * ```ts\n * compact([some(1), none(), some(2)]); // [1, 2]\n * ```\n */\nexport const compact = <T>(maybes: readonly Maybe<T>[]): T[] => {\n const out: T[] = [];\n for (const m of maybes) {\n if (isSome(m)) {\n out.push(m.value);\n }\n }\n return out;\n};\n\n/**\n * `compact(items.map(fn))` — map each item to `Maybe`, drop `None`,\n * collect the rest.\n *\n * @example\n * ```ts\n * compactMap(rawRows, (row) =>\n * row.deletedAt ? none() : some(row.id),\n * );\n * // string[]\n * ```\n */\nexport const compactMap = <T, U>(items: readonly T[], fn: (item: T) => Maybe<U>): U[] =>\n compact(items.map(fn));\n\n/**\n * `flatMap(fromNullable(value), fn)` — lift a nullable then bind through\n * a `Maybe`-returning step. Convenient for chaining lookups.\n *\n * @example\n * ```ts\n * optional(rawId, (id) => fromNullable(cache.get(id)));\n * ```\n */\nexport const optional = <T, U>(value: T | null | undefined, fn: (value: T) => Maybe<U>): Maybe<U> =>\n flatMapImpl(fromNullable(value), fn);\n\nconst tapImpl = <T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T> => {\n if (isSome(maybe)) fn(maybe.value);\n return maybe;\n};\n\n/**\n * Run a side effect on the `Some` value, pass the `Maybe` through unchanged.\n * `None` is a no-op. Mirrors `tap` from `@onrails/result`. Dual-form.\n *\n * @example\n * ```ts\n * tap(some(2), (n) => console.log(n)); // logs 2, returns Some 2\n * pipe(fromNullable(row), tap((r) => sink.push(r))); // curried — collect on Some\n * ```\n */\nexport function tap<T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T>;\nexport function tap<T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;\nexport function tap(\n ...args: [Maybe<unknown>, (value: unknown) => void] | [(value: unknown) => void]\n): unknown {\n if (args.length === 2) return tapImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => tapImpl(maybe, fn);\n}\n\nconst tapNoneImpl = <T>(maybe: Maybe<T>, fn: () => void): Maybe<T> => {\n if (isNone(maybe)) fn();\n return maybe;\n};\n\n/**\n * Run a side effect when the `Maybe` is `None`, pass it through unchanged.\n * `Some` is a no-op. The `None`-side mirror of {@link tap}. Dual-form.\n *\n * @example\n * ```ts\n * tapNone(none(), () => metrics.miss()); // runs effect, returns None\n * pipe(fromNullable(hit), tapNone(() => metrics.miss())); // curried\n * ```\n */\nexport function tapNone<T>(maybe: Maybe<T>, fn: () => void): Maybe<T>;\nexport function tapNone<T>(fn: () => void): (maybe: Maybe<T>) => Maybe<T>;\nexport function tapNone(...args: [Maybe<unknown>, () => void] | [() => void]): unknown {\n if (args.length === 2) return tapNoneImpl(args[0], args[1]);\n const fn = args[0];\n return (maybe: Maybe<unknown>) => tapNoneImpl(maybe, fn);\n}\n","import { err, isOk, ok, type Result } from \"@onrails/result\";\nimport { fromNullable, isSome, type Maybe, none, some } from \"./maybe.js\";\n\n/** Sentinel error tag — `Maybe<T>` bridges to `Result<T, NoneError>`. */\nexport type NoneError = { readonly _tag: \"None\" };\n\nexport const noneError = (): NoneError => ({ _tag: \"None\" });\n\n/** Lift absence into a domain error. */\nexport const toResult = <T, E>(maybe: Maybe<T>, onNone: () => E): Result<T, E> =>\n isSome(maybe) ? ok(maybe.value) : err(onNone());\n\n/** Ok values become Some; any Err becomes None (errors are not absence). */\nexport const fromResult = <T, E>(result: Result<T, E>): Maybe<T> =>\n isOk(result) ? some(result.value) : none<T>();\n\n/** Nullable edge → Maybe; explicit None error at the boundary. */\nexport const nullableToResult = <T, E>(\n value: T | null | undefined,\n onNone: () => E,\n): Result<T, E> => toResult(fromNullable(value), onNone);\n\n/** True when a Result failed specifically with {@link NoneError}. */\nexport const isNoneError = (result: Result<unknown, unknown>): result is Result<never, NoneError> =>\n !isOk(result) && isNoneErrorValue(result.error);\n\nconst isNoneErrorValue = (error: unknown): error is NoneError =>\n typeof error === \"object\" &&\n error !== null &&\n \"_tag\" in error &&\n (error as { _tag: unknown })._tag === \"None\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/internal/dual.ts","../src/maybe.ts","../src/interop.ts"],"names":["ok","err","result","isOk"],"mappings":";;;;;;;AAUO,SAAS,IAAA,CAAQ,OAAe,IAAA,EAAwC;AAC7E,EAAA,MAAM,QAAA,GAAW,IAAI,IAAA,KAA6B;AAChD,IAAA,IAAI,KAAK,MAAA,IAAU,KAAA,EAAO,OAAQ,IAAA,CAAsC,GAAG,IAAI,CAAA;AAC/E,IAAA,OAAO,CAAC,IAAA,KAAmB,IAAA,CAAsC,IAAA,EAAM,GAAG,IAAI,CAAA;AAAA,EAChF,CAAA;AAEA,EAAA,OAAO,QAAA;AACT;;;ACcO,IAAM,OAAO,CAAI,KAAA,MAAwB,EAAE,IAAA,EAAM,QAAQ,KAAA,EAAM,CAAA;AAU/D,IAAM,IAAA,GAAO,OAA4B,EAAE,IAAA,EAAM,MAAA,EAAO,CAAA;AAYxD,IAAM,MAAA,GAAS,CAAI,KAAA,KAAsC,KAAA,CAAM,IAAA,KAAS,MAAA;AAwBxE,IAAM,YAAA,GAAe,CAAI,KAAA,KAC9B,KAAA,IAAS,OAAO,IAAA,EAAQ,GAAI,KAAK,KAAK,CAAA;;;ACtEjC,IAAM,SAAA,GAAY,OAAkB,EAAE,IAAA,EAAM,MAAA,EAAO;AAE1D,IAAM,YAAA,GAAe,CAAO,KAAA,EAAiB,MAAA,KAC3C,MAAA,CAAO,KAAK,CAAA,GAAIA,SAAA,CAAG,KAAA,CAAM,KAAK,CAAA,GAAIC,UAAA,CAAI,QAAQ,CAAA;AAGzC,IAAM,QAAA,GAGT,IAAA,CAAK,CAAA,EAAG,YAAY;AAExB,IAAM,cAAA,GAAiB,CAAOC,QAAA,KAC5BC,WAAA,CAAKD,QAAM,IAAI,IAAA,CAAKA,QAAA,CAAO,KAAK,CAAA,GAAI,IAAA,EAAK;AAGpC,IAAM,UAAA,GAGT,IAAA,CAAK,CAAA,EAAG,cAAc;AAE1B,IAAM,oBAAA,GAAuB,CAAO,KAAA,EAA6B,MAAA,KAC/D,aAAa,YAAA,CAAa,KAAK,GAAG,MAAM,CAAA;AAGnC,IAAM,gBAAA,GAGT,IAAA,CAAK,CAAA,EAAG,oBAAoB;AAGzB,IAAM,WAAA,GAAc,CAACA,QAAA,KAC1B,CAACC,YAAKD,QAAM,CAAA,IAAK,gBAAA,CAAiBA,QAAA,CAAO,KAAK;AAEhD,IAAM,gBAAA,GAAmB,CAAC,KAAA,KACxB,OAAO,KAAA,KAAU,QAAA,IACjB,KAAA,KAAU,IAAA,IACV,MAAA,IAAU,KAAA,IACT,KAAA,CAA4B,IAAA,KAAS,MAAA","file":"interop.cjs","sourcesContent":["/**\n * Owning module for the dual-form call idiom used across `@onrails/maybe`:\n * a function accepts either data-first (`map(m, fn)`) or curried data-last\n * (`map(fn)(m)`) invocation, dispatching on argument count against a fixed\n * `arity`.\n *\n * This is a deliberate twin of `packages/result/src/internal/dual.ts` — keep\n * the two in sync. No cross-package imports are allowed in this repo, so the\n * helper is vendored independently in each package rather than shared.\n */\nexport function dual<F>(arity: number, body: (...args: never[]) => unknown): F {\n const dispatch = (...args: unknown[]): unknown => {\n if (args.length >= arity) return (body as (...a: unknown[]) => unknown)(...args);\n return (self: unknown) => (body as (...a: unknown[]) => unknown)(self, ...args);\n };\n // cast: the caller's overloaded type annotation supplies the real signature\n return dispatch as F;\n}\n","import { dual } from \"./internal/dual.js\";\n\n/**\n * Tagged optional value — a discriminated union over `_tag` where absence\n * (`None`) is an expected outcome, not a failure. Construct with {@link some} /\n * {@link none}, narrow with {@link isSome} / {@link isNone}, and collapse with\n * {@link match}.\n *\n * @example\n * ```ts\n * function findUser(id: string): Maybe<User> {\n * return fromNullable(db.users.get(id));\n * }\n * ```\n */\nexport type Maybe<T> = { readonly _tag: \"Some\"; readonly value: T } | { readonly _tag: \"None\" };\n\n/** The `Some` branch of a {@link Maybe} — a present value tagged `\"Some\"`. */\nexport type Some<T> = Extract<Maybe<T>, { _tag: \"Some\" }>;\n\n/** The `None` branch of a {@link Maybe} — expected absence, tagged `\"None\"`. */\nexport type None = Extract<Maybe<never>, { _tag: \"None\" }>;\n\n/**\n * Lifts a value into the `Some` branch.\n *\n * @example\n * ```ts\n * const m = some(1); // Maybe<number>\n * ```\n */\nexport const some = <T>(value: T): Maybe<T> => ({ _tag: \"Some\", value });\n\n/**\n * The `None` value — represents expected absence. Not a failure.\n *\n * @example\n * ```ts\n * const cached: Maybe<User> = cache.has(id) ? some(cache.get(id)) : none();\n * ```\n */\nexport const none = <T = never>(): Maybe<T> => ({ _tag: \"None\" });\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `Some`.\n *\n * @example\n * ```ts\n * if (isSome(m)) {\n * console.log(m.value); // narrowed to Some branch\n * }\n * ```\n */\nexport const isSome = <T>(maybe: Maybe<T>): maybe is Some<T> => maybe._tag === \"Some\";\n\n/**\n * Type-narrowing predicate: returns `true` when the value is `None`.\n *\n * @example\n * ```ts\n * if (isNone(fromNullable(user))) {\n * return redirect(\"/login\"); // narrowed to None branch\n * }\n * ```\n */\nexport const isNone = <T>(maybe: Maybe<T>): maybe is None => maybe._tag === \"None\";\n\n/**\n * Lift a nullable value into `Maybe<T>` — `null` and `undefined` become\n * `None`; anything else becomes `Some`.\n *\n * @example\n * ```ts\n * fromNullable(cache.get(key)); // Maybe<V>\n * fromNullable(null); // None\n * ```\n */\nexport const fromNullable = <T>(value: T | null | undefined): Maybe<T> =>\n value == null ? none<T>() : some(value);\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Dual-form helpers — each accepts either shape:\n// data-first: `map(maybe, fn)`\n// curried: `map(fn)(maybe)`\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst mapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U> =>\n isSome(maybe) ? some(fn(maybe.value)) : none<U>();\n\n/**\n * Transform the `Some` value, passing `None` through unchanged. Dual-form.\n *\n * @example\n * ```ts\n * map(some(2), (n) => n * 3); // Some 6 — data-first\n * pipe(fromNullable(input), map((s) => s.trim())); // curried\n * ```\n */\nexport const map: {\n <T, U>(maybe: Maybe<T>, fn: (value: T) => U): Maybe<U>;\n <T, U>(fn: (value: T) => U): (maybe: Maybe<T>) => Maybe<U>;\n} = dual(2, mapImpl);\n\nconst flatMapImpl = <T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U> =>\n isSome(maybe) ? fn(maybe.value) : none<U>();\n\n/**\n * Canonical bind for `Maybe`. Chains a `Maybe`-returning step;\n * short-circuits on `None`. Dual-form.\n *\n * @example\n * ```ts\n * flatMap(fromNullable(user), (u) =>\n * u.active ? some(u) : none(),\n * );\n * ```\n */\nexport const flatMap: {\n <T, U>(maybe: Maybe<T>, fn: (value: T) => Maybe<U>): Maybe<U>;\n <T, U>(fn: (value: T) => Maybe<U>): (maybe: Maybe<T>) => Maybe<U>;\n} = dual(2, flatMapImpl);\n\n/**\n * Neverthrow-compat alias of {@link flatMap} — same dual-form shape. Prefer\n * {@link flatMap} as the canonical name; reach for `andThen` only when porting\n * neverthrow code or matching its vocabulary.\n *\n * @example\n * ```ts\n * andThen(fromNullable(user), (u) =>\n * u.active ? some(u) : none(),\n * );\n * ```\n */\nexport const andThen = flatMap;\n\nconst matchImpl = <T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U =>\n isSome(maybe) ? onSome(maybe.value) : onNone();\n\n/**\n * Terminal collapse — fold both branches into a single value. Positional,\n * dual-form. Same shape as `Result.match` for sibling consistency.\n *\n * For files that also import `match` from `ts-pattern`, use a namespace\n * import (`import * as Maybe from \"@onrails/maybe\"` → `Maybe.match`) to\n * dissolve the collision.\n *\n * @example\n * ```ts\n * const greeting = match(\n * fromNullable(user),\n * (u) => `hello ${u.name}`,\n * () => \"hello guest\",\n * );\n * ```\n */\nexport const match: {\n <T, U>(maybe: Maybe<T>, onSome: (value: T) => U, onNone: () => U): U;\n <T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;\n} = dual(3, matchImpl);\n\nconst unwrapOrImpl = <T>(maybe: Maybe<T>, defaultValue: T): T =>\n isSome(maybe) ? maybe.value : defaultValue;\n\nexport const unwrapOr: {\n <T>(maybe: Maybe<T>, defaultValue: T): T;\n <T>(defaultValue: T): (maybe: Maybe<T>) => T;\n} = dual(2, unwrapOrImpl);\n\n/**\n * Unwraps the `Some` value or throws if the value is `None`.\n *\n * @throws when the value is None — assertion-tier; use {@link match} / {@link unwrapOr} in business logic.\n * Allowed in `*.spec.ts` / `*.test.ts`; flagged elsewhere by the plugins.\n *\n * @example\n * ```ts\n * expect(unwrap(parseUser(raw))).toEqual(expected);\n * ```\n */\nexport const unwrap = <T>(maybe: Maybe<T>): T => {\n if (isNone(maybe)) {\n throw new Error(\"Called unwrap on None\");\n }\n return maybe.value;\n};\n\n/**\n * Drop every `None` and collect the remaining `Some` values into an array.\n *\n * @example\n * ```ts\n * compact([some(1), none(), some(2)]); // [1, 2]\n * ```\n */\nexport const compact = <T>(maybes: readonly Maybe<T>[]): T[] => {\n const out: T[] = [];\n for (const m of maybes) {\n if (isSome(m)) {\n out.push(m.value);\n }\n }\n return out;\n};\n\n/**\n * `compact(items.map(fn))` — map each item to `Maybe`, drop `None`,\n * collect the rest.\n *\n * @example\n * ```ts\n * compactMap(rawRows, (row) =>\n * row.deletedAt ? none() : some(row.id),\n * );\n * // string[]\n * ```\n */\nexport const compactMap = <T, U>(items: readonly T[], fn: (item: T) => Maybe<U>): U[] =>\n compact(items.map(fn));\n\n/**\n * `flatMap(fromNullable(value), fn)` — lift a nullable then bind through\n * a `Maybe`-returning step. Convenient for chaining lookups.\n *\n * @example\n * ```ts\n * optional(rawId, (id) => fromNullable(cache.get(id)));\n * ```\n */\nexport const optional = <T, U>(value: T | null | undefined, fn: (value: T) => Maybe<U>): Maybe<U> =>\n flatMapImpl(fromNullable(value), fn);\n\nconst tapImpl = <T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T> => {\n if (isSome(maybe)) fn(maybe.value);\n return maybe;\n};\n\n/**\n * Run a side effect on the `Some` value, pass the `Maybe` through unchanged.\n * `None` is a no-op. Mirrors `tap` from `@onrails/result`. Dual-form.\n *\n * @example\n * ```ts\n * tap(some(2), (n) => console.log(n)); // logs 2, returns Some 2\n * pipe(fromNullable(row), tap((r) => sink.push(r))); // curried — collect on Some\n * ```\n */\nexport const tap: {\n <T>(maybe: Maybe<T>, fn: (value: T) => void): Maybe<T>;\n <T>(fn: (value: T) => void): (maybe: Maybe<T>) => Maybe<T>;\n} = dual(2, tapImpl);\n\nconst tapNoneImpl = <T>(maybe: Maybe<T>, fn: () => void): Maybe<T> => {\n if (isNone(maybe)) fn();\n return maybe;\n};\n\n/**\n * Run a side effect when the `Maybe` is `None`, pass it through unchanged.\n * `Some` is a no-op. The `None`-side mirror of {@link tap}. Dual-form.\n *\n * @example\n * ```ts\n * tapNone(none(), () => metrics.miss()); // runs effect, returns None\n * pipe(fromNullable(hit), tapNone(() => metrics.miss())); // curried\n * ```\n */\nexport const tapNone: {\n <T>(maybe: Maybe<T>, fn: () => void): Maybe<T>;\n <T>(fn: () => void): (maybe: Maybe<T>) => Maybe<T>;\n} = dual(2, tapNoneImpl);\n","import { err, isOk, ok, type Result } from \"@onrails/result\";\nimport { dual } from \"./internal/dual.js\";\nimport { fromNullable, isSome, type Maybe, none, some } from \"./maybe.js\";\n\n/** Sentinel error tag — `Maybe<T>` bridges to `Result<T, NoneError>`. */\nexport type NoneError = { readonly _tag: \"None\" };\n\n/** Construct the {@link NoneError} sentinel. */\nexport const noneError = (): NoneError => ({ _tag: \"None\" });\n\nconst toResultImpl = <T, E>(maybe: Maybe<T>, onNone: () => E): Result<T, E> =>\n isSome(maybe) ? ok(maybe.value) : err(onNone());\n\n/** Lift absence into a domain error. */\nexport const toResult: {\n <T, E>(maybe: Maybe<T>, onNone: () => E): Result<T, E>;\n <E>(onNone: () => E): <T>(maybe: Maybe<T>) => Result<T, E>;\n} = dual(2, toResultImpl);\n\nconst fromResultImpl = <T, E>(result: Result<T, E>): Maybe<T> =>\n isOk(result) ? some(result.value) : none();\n\n/** Ok values become Some; any Err becomes None (errors are not absence). */\nexport const fromResult: {\n <T, E>(result: Result<T, E>): Maybe<T>;\n <T, E>(): (result: Result<T, E>) => Maybe<T>;\n} = dual(1, fromResultImpl);\n\nconst nullableToResultImpl = <T, E>(value: T | null | undefined, onNone: () => E): Result<T, E> =>\n toResultImpl(fromNullable(value), onNone);\n\n/** Nullable edge → Maybe; explicit None error at the boundary. */\nexport const nullableToResult: {\n <T, E>(value: T | null | undefined, onNone: () => E): Result<T, E>;\n <E>(onNone: () => E): <T>(value: T | null | undefined) => Result<T, E>;\n} = dual(2, nullableToResultImpl);\n\n/** True when a Result failed specifically with {@link NoneError}. */\nexport const isNoneError = (result: Result<unknown, unknown>): result is Result<never, NoneError> =>\n !isOk(result) && isNoneErrorValue(result.error);\n\nconst isNoneErrorValue = (error: unknown): error is NoneError =>\n typeof error === \"object\" &&\n error !== null &&\n \"_tag\" in error &&\n (error as { _tag: unknown })._tag === \"None\";\n"]}
|
package/dist/interop.d.cts
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
import { Result } from '@onrails/result';
|
|
2
|
-
import {
|
|
2
|
+
import { Maybe } from './index.cjs';
|
|
3
3
|
|
|
4
4
|
/** Sentinel error tag — `Maybe<T>` bridges to `Result<T, NoneError>`. */
|
|
5
5
|
type NoneError = {
|
|
6
6
|
readonly _tag: "None";
|
|
7
7
|
};
|
|
8
|
+
/** Construct the {@link NoneError} sentinel. */
|
|
8
9
|
declare const noneError: () => NoneError;
|
|
9
10
|
/** Lift absence into a domain error. */
|
|
10
|
-
declare const toResult:
|
|
11
|
+
declare const toResult: {
|
|
12
|
+
<T, E>(maybe: Maybe<T>, onNone: () => E): Result<T, E>;
|
|
13
|
+
<E>(onNone: () => E): <T>(maybe: Maybe<T>) => Result<T, E>;
|
|
14
|
+
};
|
|
11
15
|
/** Ok values become Some; any Err becomes None (errors are not absence). */
|
|
12
|
-
declare const fromResult:
|
|
16
|
+
declare const fromResult: {
|
|
17
|
+
<T, E>(result: Result<T, E>): Maybe<T>;
|
|
18
|
+
<T, E>(): (result: Result<T, E>) => Maybe<T>;
|
|
19
|
+
};
|
|
13
20
|
/** Nullable edge → Maybe; explicit None error at the boundary. */
|
|
14
|
-
declare const nullableToResult:
|
|
21
|
+
declare const nullableToResult: {
|
|
22
|
+
<T, E>(value: T | null | undefined, onNone: () => E): Result<T, E>;
|
|
23
|
+
<E>(onNone: () => E): <T>(value: T | null | undefined) => Result<T, E>;
|
|
24
|
+
};
|
|
15
25
|
/** True when a Result failed specifically with {@link NoneError}. */
|
|
16
26
|
declare const isNoneError: (result: Result<unknown, unknown>) => result is Result<never, NoneError>;
|
|
17
27
|
|