nalloc 0.0.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/README.md +282 -0
- package/build/devtools.cjs +79 -0
- package/build/devtools.cjs.map +1 -0
- package/build/devtools.d.ts +82 -0
- package/build/devtools.js +43 -0
- package/build/devtools.js.map +1 -0
- package/build/index.cjs +76 -0
- package/build/index.cjs.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/option.cjs +279 -0
- package/build/option.cjs.map +1 -0
- package/build/option.d.ts +356 -0
- package/build/option.js +157 -0
- package/build/option.js.map +1 -0
- package/build/result.cjs +381 -0
- package/build/result.cjs.map +1 -0
- package/build/result.d.ts +442 -0
- package/build/result.js +229 -0
- package/build/result.js.map +1 -0
- package/build/safe.cjs +88 -0
- package/build/safe.cjs.map +1 -0
- package/build/safe.d.ts +29 -0
- package/build/safe.js +18 -0
- package/build/safe.js.map +1 -0
- package/build/testing.cjs +111 -0
- package/build/testing.cjs.map +1 -0
- package/build/testing.d.ts +85 -0
- package/build/testing.js +81 -0
- package/build/testing.js.map +1 -0
- package/build/types.cjs +78 -0
- package/build/types.cjs.map +1 -0
- package/build/types.d.ts +137 -0
- package/build/types.js +36 -0
- package/build/types.js.map +1 -0
- package/build/unsafe.cjs +82 -0
- package/build/unsafe.cjs.map +1 -0
- package/build/unsafe.d.ts +27 -0
- package/build/unsafe.js +11 -0
- package/build/unsafe.js.map +1 -0
- package/package.json +93 -0
- package/src/__tests__/index.ts +13 -0
- package/src/__tests__/option.ts +610 -0
- package/src/__tests__/option.types.ts +120 -0
- package/src/__tests__/result.ts +721 -0
- package/src/__tests__/result.types.ts +249 -0
- package/src/__tests__/safe.ts +24 -0
- package/src/__tests__/tooling.ts +86 -0
- package/src/__tests__/unsafe.ts +24 -0
- package/src/devtools.ts +97 -0
- package/src/index.ts +18 -0
- package/src/option.ts +510 -0
- package/src/result.ts +676 -0
- package/src/safe.ts +58 -0
- package/src/testing.ts +159 -0
- package/src/types.ts +201 -0
- package/src/unsafe.ts +47 -0
package/build/option.cjs
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get and () {
|
|
13
|
+
return and;
|
|
14
|
+
},
|
|
15
|
+
get andThen () {
|
|
16
|
+
return andThen;
|
|
17
|
+
},
|
|
18
|
+
get assertSome () {
|
|
19
|
+
return assertSome;
|
|
20
|
+
},
|
|
21
|
+
get contains () {
|
|
22
|
+
return contains;
|
|
23
|
+
},
|
|
24
|
+
get expect () {
|
|
25
|
+
return expect;
|
|
26
|
+
},
|
|
27
|
+
get filter () {
|
|
28
|
+
return filter;
|
|
29
|
+
},
|
|
30
|
+
get filterMap () {
|
|
31
|
+
return filterMap;
|
|
32
|
+
},
|
|
33
|
+
get flatMap () {
|
|
34
|
+
return flatMap;
|
|
35
|
+
},
|
|
36
|
+
get flatten () {
|
|
37
|
+
return flatten;
|
|
38
|
+
},
|
|
39
|
+
get fromNullable () {
|
|
40
|
+
return fromNullable;
|
|
41
|
+
},
|
|
42
|
+
get fromPromise () {
|
|
43
|
+
return fromPromise;
|
|
44
|
+
},
|
|
45
|
+
get isNone () {
|
|
46
|
+
return _typescjs.isNone;
|
|
47
|
+
},
|
|
48
|
+
get isSome () {
|
|
49
|
+
return _typescjs.isSome;
|
|
50
|
+
},
|
|
51
|
+
get isSomeAnd () {
|
|
52
|
+
return isSomeAnd;
|
|
53
|
+
},
|
|
54
|
+
get map () {
|
|
55
|
+
return map;
|
|
56
|
+
},
|
|
57
|
+
get mapOr () {
|
|
58
|
+
return mapOr;
|
|
59
|
+
},
|
|
60
|
+
get mapOrElse () {
|
|
61
|
+
return mapOrElse;
|
|
62
|
+
},
|
|
63
|
+
get match () {
|
|
64
|
+
return match;
|
|
65
|
+
},
|
|
66
|
+
get of () {
|
|
67
|
+
return _typescjs.optionOf;
|
|
68
|
+
},
|
|
69
|
+
get ofErr () {
|
|
70
|
+
return ofErr;
|
|
71
|
+
},
|
|
72
|
+
get ofOk () {
|
|
73
|
+
return ofOk;
|
|
74
|
+
},
|
|
75
|
+
get okOr () {
|
|
76
|
+
return okOr;
|
|
77
|
+
},
|
|
78
|
+
get okOrElse () {
|
|
79
|
+
return okOrElse;
|
|
80
|
+
},
|
|
81
|
+
get or () {
|
|
82
|
+
return or;
|
|
83
|
+
},
|
|
84
|
+
get orElse () {
|
|
85
|
+
return orElse;
|
|
86
|
+
},
|
|
87
|
+
get satisfiesOption () {
|
|
88
|
+
return satisfiesOption;
|
|
89
|
+
},
|
|
90
|
+
get tap () {
|
|
91
|
+
return tap;
|
|
92
|
+
},
|
|
93
|
+
get toArray () {
|
|
94
|
+
return toArray;
|
|
95
|
+
},
|
|
96
|
+
get toNullable () {
|
|
97
|
+
return toNullable;
|
|
98
|
+
},
|
|
99
|
+
get toUndefined () {
|
|
100
|
+
return toUndefined;
|
|
101
|
+
},
|
|
102
|
+
get unwrap () {
|
|
103
|
+
return unwrap;
|
|
104
|
+
},
|
|
105
|
+
get unwrapOr () {
|
|
106
|
+
return unwrapOr;
|
|
107
|
+
},
|
|
108
|
+
get unwrapOrElse () {
|
|
109
|
+
return unwrapOrElse;
|
|
110
|
+
},
|
|
111
|
+
get unwrapOrReturn () {
|
|
112
|
+
return unwrapOrReturn;
|
|
113
|
+
},
|
|
114
|
+
get unzip () {
|
|
115
|
+
return unzip;
|
|
116
|
+
},
|
|
117
|
+
get xor () {
|
|
118
|
+
return xor;
|
|
119
|
+
},
|
|
120
|
+
get zip () {
|
|
121
|
+
return zip;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
const _typescjs = require("./types.cjs");
|
|
125
|
+
function fromNullable(value) {
|
|
126
|
+
return (0, _typescjs.optionOf)(value);
|
|
127
|
+
}
|
|
128
|
+
async function fromPromise(promise, onRejected) {
|
|
129
|
+
try {
|
|
130
|
+
const value = await promise;
|
|
131
|
+
return (0, _typescjs.optionOf)(value);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (!onRejected) {
|
|
134
|
+
return _typescjs.NONE;
|
|
135
|
+
}
|
|
136
|
+
return (0, _typescjs.optionOf)(onRejected(error));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function unwrapOrReturn(opt, onNone) {
|
|
140
|
+
return (0, _typescjs.isSome)(opt) ? opt : onNone();
|
|
141
|
+
}
|
|
142
|
+
function assertSome(opt, message) {
|
|
143
|
+
if ((0, _typescjs.isNone)(opt)) {
|
|
144
|
+
throw new Error(message ?? 'Expected Option to contain a value');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function satisfiesOption(_) {}
|
|
148
|
+
function filterMap(values, fn) {
|
|
149
|
+
const collected = [];
|
|
150
|
+
for (const value of values){
|
|
151
|
+
const mapped = fn(value);
|
|
152
|
+
if ((0, _typescjs.isSome)(mapped)) {
|
|
153
|
+
collected.push(mapped);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return collected;
|
|
157
|
+
}
|
|
158
|
+
function map(opt, fn) {
|
|
159
|
+
if ((0, _typescjs.isNone)(opt)) return _typescjs.NONE;
|
|
160
|
+
const result = fn(opt);
|
|
161
|
+
return result === null || result === undefined ? _typescjs.NONE : result;
|
|
162
|
+
}
|
|
163
|
+
function flatMap(opt, fn) {
|
|
164
|
+
return (0, _typescjs.isNone)(opt) ? _typescjs.NONE : fn(opt);
|
|
165
|
+
}
|
|
166
|
+
function andThen(opt, fn) {
|
|
167
|
+
return flatMap(opt, fn);
|
|
168
|
+
}
|
|
169
|
+
function tap(opt, fn) {
|
|
170
|
+
if ((0, _typescjs.isSome)(opt)) {
|
|
171
|
+
fn(opt);
|
|
172
|
+
}
|
|
173
|
+
return opt;
|
|
174
|
+
}
|
|
175
|
+
function filter(opt, predicate) {
|
|
176
|
+
return (0, _typescjs.isSome)(opt) && predicate(opt) ? opt : _typescjs.NONE;
|
|
177
|
+
}
|
|
178
|
+
function unwrap(opt) {
|
|
179
|
+
if ((0, _typescjs.isNone)(opt)) {
|
|
180
|
+
throw new Error('Called unwrap on None');
|
|
181
|
+
}
|
|
182
|
+
return opt;
|
|
183
|
+
}
|
|
184
|
+
function unwrapOr(opt, defaultValue) {
|
|
185
|
+
return (0, _typescjs.isSome)(opt) ? opt : defaultValue;
|
|
186
|
+
}
|
|
187
|
+
function unwrapOrElse(opt, fn) {
|
|
188
|
+
return (0, _typescjs.isSome)(opt) ? opt : fn();
|
|
189
|
+
}
|
|
190
|
+
function expect(opt, message) {
|
|
191
|
+
if ((0, _typescjs.isNone)(opt)) {
|
|
192
|
+
throw new Error(message);
|
|
193
|
+
}
|
|
194
|
+
return opt;
|
|
195
|
+
}
|
|
196
|
+
function or(opt, optb) {
|
|
197
|
+
return (0, _typescjs.isSome)(opt) ? opt : optb;
|
|
198
|
+
}
|
|
199
|
+
function orElse(opt, fn) {
|
|
200
|
+
return (0, _typescjs.isSome)(opt) ? opt : fn();
|
|
201
|
+
}
|
|
202
|
+
function xor(opt, optb) {
|
|
203
|
+
if ((0, _typescjs.isSome)(opt) && (0, _typescjs.isNone)(optb)) return opt;
|
|
204
|
+
if ((0, _typescjs.isNone)(opt) && (0, _typescjs.isSome)(optb)) return optb;
|
|
205
|
+
return _typescjs.NONE;
|
|
206
|
+
}
|
|
207
|
+
function and(opt, optb) {
|
|
208
|
+
return (0, _typescjs.isSome)(opt) ? optb : _typescjs.NONE;
|
|
209
|
+
}
|
|
210
|
+
function zip(opt, other) {
|
|
211
|
+
return (0, _typescjs.isSome)(opt) && (0, _typescjs.isSome)(other) ? [
|
|
212
|
+
opt,
|
|
213
|
+
other
|
|
214
|
+
] : _typescjs.NONE;
|
|
215
|
+
}
|
|
216
|
+
function unzip(opt) {
|
|
217
|
+
if ((0, _typescjs.isNone)(opt)) return [
|
|
218
|
+
_typescjs.NONE,
|
|
219
|
+
_typescjs.NONE
|
|
220
|
+
];
|
|
221
|
+
const [a, b] = opt;
|
|
222
|
+
return [
|
|
223
|
+
(0, _typescjs.optionOf)(a),
|
|
224
|
+
(0, _typescjs.optionOf)(b)
|
|
225
|
+
];
|
|
226
|
+
}
|
|
227
|
+
function mapOr(opt, defaultValue, fn) {
|
|
228
|
+
return (0, _typescjs.isSome)(opt) ? fn(opt) : defaultValue;
|
|
229
|
+
}
|
|
230
|
+
function mapOrElse(opt, defaultFn, fn) {
|
|
231
|
+
return (0, _typescjs.isSome)(opt) ? fn(opt) : defaultFn();
|
|
232
|
+
}
|
|
233
|
+
function flatten(opt) {
|
|
234
|
+
return (0, _typescjs.isNone)(opt) ? _typescjs.NONE : opt;
|
|
235
|
+
}
|
|
236
|
+
function contains(opt, value) {
|
|
237
|
+
return (0, _typescjs.isSome)(opt) && opt === value;
|
|
238
|
+
}
|
|
239
|
+
function isSomeAnd(opt, predicate) {
|
|
240
|
+
return (0, _typescjs.isSome)(opt) && predicate(opt);
|
|
241
|
+
}
|
|
242
|
+
function toArray(opt) {
|
|
243
|
+
return (0, _typescjs.isSome)(opt) ? [
|
|
244
|
+
opt
|
|
245
|
+
] : [];
|
|
246
|
+
}
|
|
247
|
+
function toNullable(opt) {
|
|
248
|
+
return (0, _typescjs.isSome)(opt) ? opt : null;
|
|
249
|
+
}
|
|
250
|
+
function toUndefined(opt) {
|
|
251
|
+
return (0, _typescjs.isSome)(opt) ? opt : undefined;
|
|
252
|
+
}
|
|
253
|
+
function match(opt, onSome, onNone) {
|
|
254
|
+
return (0, _typescjs.isSome)(opt) ? onSome(opt) : onNone();
|
|
255
|
+
}
|
|
256
|
+
function okOr(opt, error) {
|
|
257
|
+
return (0, _typescjs.isSome)(opt) ? opt : (0, _typescjs.err)(error);
|
|
258
|
+
}
|
|
259
|
+
function okOrElse(opt, fn) {
|
|
260
|
+
return (0, _typescjs.isSome)(opt) ? opt : (0, _typescjs.err)(fn());
|
|
261
|
+
}
|
|
262
|
+
function ofOk(result) {
|
|
263
|
+
if (!(0, _typescjs.isOk)(result) || !(0, _typescjs.isSome)(result)) {
|
|
264
|
+
return _typescjs.NONE;
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
function ofErr(result) {
|
|
269
|
+
if (!(0, _typescjs.isErr)(result)) {
|
|
270
|
+
return _typescjs.NONE;
|
|
271
|
+
}
|
|
272
|
+
const error = result.error;
|
|
273
|
+
if (!(0, _typescjs.isSome)(error)) {
|
|
274
|
+
return _typescjs.NONE;
|
|
275
|
+
}
|
|
276
|
+
return error;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
//# sourceMappingURL=option.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/option.ts"],"sourcesContent":["import { NONE, isSome, isNone, optionOf as of, err, isOk, isErr } from './types.js';\nimport type { Some, None, Option, NoneValueType, ValueType, Result, Ok, Widen } from './types.js';\n\nexport type { Some, None, Option };\nexport { isSome, isNone, of };\n\n/**\n * Creates an Option from a nullable value with widened types.\n * @param value - The value to wrap\n * @returns Some(value) if non-null, None otherwise\n * @example\n * fromNullable(42) // Some(42) with type Option<number>\n * fromNullable(null) // None\n */\nexport function fromNullable(value: null): None;\nexport function fromNullable(value: undefined): None;\nexport function fromNullable<T>(value: T | NoneValueType): Option<Widen<T>>;\nexport function fromNullable<T>(value: T | NoneValueType): Option<Widen<T>> {\n return of(value) as Option<Widen<T>>;\n}\n\n/**\n * Creates an Option from a Promise. Resolves to Some if successful, None on rejection.\n * @param promise - The promise to convert\n * @param onRejected - Optional handler for rejected promises\n * @returns Promise resolving to Some(value) or None\n * @example\n * await fromPromise(Promise.resolve(42)) // Some(42)\n * await fromPromise(Promise.reject('error')) // None\n */\nexport async function fromPromise<T>(promise: Promise<T | NoneValueType>, onRejected?: (error: unknown) => T | NoneValueType): Promise<Option<T>> {\n try {\n const value = await promise;\n return of(value as T);\n } catch (error) {\n if (!onRejected) {\n return NONE;\n }\n return of(onRejected(error));\n }\n}\n\n/**\n * Unwraps an Option or returns a computed value if None.\n * @param opt - The Option to unwrap\n * @param onNone - Function called if opt is None\n * @returns The value if Some, or the result of onNone()\n * @example\n * unwrapOrReturn(some(42), () => 0) // 42\n * unwrapOrReturn(none, () => 0) // 0\n */\nexport function unwrapOrReturn<T, R>(opt: Option<T>, onNone: () => R): Widen<T> | R {\n return isSome(opt) ? (opt as Widen<T>) : onNone();\n}\n\n/**\n * Asserts that an Option is Some, throwing if None.\n * @param opt - The Option to assert\n * @param message - Custom error message\n * @throws Error if opt is None\n * @example\n * assertSome(some(42)) // passes\n * assertSome(none) // throws Error\n */\nexport function assertSome<T>(opt: Option<T>, message?: string): asserts opt is Some<ValueType<T>> {\n if (isNone(opt)) {\n throw new Error(message ?? 'Expected Option to contain a value');\n }\n}\n\n/**\n * Compile-time type assertion helper to satisfy Option type constraints.\n *\n * WARNING: This function performs NO runtime validation. It is a no-op at\n * runtime to preserve zero-allocation semantics. Use assertSome() if you\n * need runtime validation that a value is Some.\n *\n * @param _ - The value to assert as Option (not validated at runtime)\n * @example\n * const value: number | null = getValue();\n * satisfiesOption(value); // Compiles, but no runtime check\n * // value is now typed as Option<number>\n */\nexport function satisfiesOption<T>(_: Option<T> | T): asserts _ is Option<T> {\n // Compile-time only - no runtime validation to preserve zero-allocation semantics.\n}\n\n/**\n * Maps and filters an iterable, collecting only Some values.\n * @param values - The iterable to process\n * @param fn - Function that returns Option for each value\n * @returns Array of unwrapped Some values\n * @example\n * filterMap([1, 2, 3], n => n > 1 ? some(n * 2) : none) // [4, 6]\n */\nexport function filterMap<T, U>(values: Iterable<T>, fn: (value: T) => Option<U>): U[] {\n const collected: U[] = [];\n for (const value of values) {\n const mapped = fn(value);\n if (isSome(mapped)) {\n collected.push(mapped);\n }\n }\n return collected;\n}\n\n/**\n * Transforms the value inside a Some, or returns None.\n * @param opt - The Option to map\n * @param fn - Transform function\n * @returns Some(fn(value)) if Some, None otherwise\n * @example\n * map(some(2), x => x * 2) // Some(4)\n * map(none, x => x * 2) // None\n */\nexport function map<T, U>(opt: None, fn: (value: T) => U): None;\nexport function map<T, U>(opt: Option<T>, fn: (value: T) => U | NoneValueType): Option<U>;\nexport function map<T, U>(opt: Option<T>, fn: (value: T) => U | NoneValueType): Option<U> {\n if (isNone(opt)) return NONE;\n const result = fn(opt);\n return result === null || result === undefined ? NONE : (result as Some<ValueType<U>>);\n}\n\n/**\n * Chains Option-returning functions. Returns None if the input is None.\n * @param opt - The Option to chain\n * @param fn - Function returning an Option\n * @returns The result of fn(value) if Some, None otherwise\n * @example\n * flatMap(some(2), x => some(x * 2)) // Some(4)\n * flatMap(some(2), x => none) // None\n * flatMap(none, x => some(x * 2)) // None\n */\nexport function flatMap<T, U>(opt: None, fn: (value: T) => Option<U>): None;\nexport function flatMap<T, U>(opt: Option<T>, fn: (value: T) => Option<U>): Option<U>;\nexport function flatMap<T, U>(opt: Option<T>, fn: (value: T) => Option<U>): Option<U> {\n return isNone(opt) ? NONE : fn(opt);\n}\n\n/**\n * Alias for flatMap. Chains Option-returning functions.\n * @param opt - The Option to chain\n * @param fn - Function returning an Option\n * @returns The result of fn(value) if Some, None otherwise\n */\nexport function andThen<T, U>(opt: None, fn: (value: T) => Option<U>): None;\nexport function andThen<T, U>(opt: Option<T>, fn: (value: T) => Option<U>): Option<U>;\nexport function andThen<T, U>(opt: Option<T>, fn: (value: T) => Option<U>): Option<U> {\n return flatMap(opt, fn);\n}\n\n/**\n * Executes a side effect if Some, then returns the original Option.\n * @param opt - The Option to tap\n * @param fn - Side effect function\n * @returns The original Option unchanged\n * @example\n * tap(some(42), x => console.log(x)) // logs 42, returns Some(42)\n */\nexport function tap<T>(opt: None, fn: (value: T) => void): None;\nexport function tap<T>(opt: Some<T>, fn: (value: T) => void): Some<T>;\nexport function tap<T>(opt: Option<T>, fn: (value: T) => void): Option<T>;\nexport function tap<T>(opt: Option<T>, fn: (value: T) => void): Option<T> {\n if (isSome(opt)) {\n fn(opt);\n }\n return opt;\n}\n\n/**\n * Returns Some if the value passes the predicate, None otherwise.\n * @param opt - The Option to filter\n * @param predicate - Test function\n * @returns Some if predicate returns true, None otherwise\n * @example\n * filter(some(4), x => x > 2) // Some(4)\n * filter(some(1), x => x > 2) // None\n */\nexport function filter<T>(opt: None, predicate: (value: T) => boolean): None;\nexport function filter<T>(opt: Option<T>, predicate: (value: T) => boolean): Option<T>;\nexport function filter<T>(opt: Option<T>, predicate: (value: T) => boolean): Option<T> {\n return isSome(opt) && predicate(opt) ? opt : NONE;\n}\n\n/**\n * Extracts the value from Some, throws if None.\n * @param opt - The Option to unwrap\n * @returns The contained value\n * @throws Error if opt is None\n * @example\n * unwrap(some(42)) // 42\n * unwrap(none) // throws Error\n */\nexport function unwrap<T>(opt: Option<T>): T {\n if (isNone(opt)) {\n throw new Error('Called unwrap on None');\n }\n return opt;\n}\n\n/**\n * Extracts the value from Some, or returns a default value.\n * @param opt - The Option to unwrap\n * @param defaultValue - Value to return if None\n * @returns The contained value or defaultValue\n * @example\n * unwrapOr(some(42), 0) // 42\n * unwrapOr(none, 0) // 0\n */\nexport function unwrapOr<T>(opt: Option<T>, defaultValue: T): T {\n return isSome(opt) ? opt : defaultValue;\n}\n\n/**\n * Extracts the value from Some, or computes a default.\n * @param opt - The Option to unwrap\n * @param fn - Function to compute default value\n * @returns The contained value or fn()\n * @example\n * unwrapOrElse(some(42), () => 0) // 42\n * unwrapOrElse(none, () => 0) // 0\n */\nexport function unwrapOrElse<T>(opt: Option<T>, fn: () => T): T {\n return isSome(opt) ? opt : fn();\n}\n\n/**\n * Extracts the value from Some, throws with custom message if None.\n * @param opt - The Option to unwrap\n * @param message - Error message if None\n * @returns The contained value\n * @throws Error with message if opt is None\n * @example\n * expect(some(42), 'missing value') // 42\n * expect(none, 'missing value') // throws Error('missing value')\n */\nexport function expect<T>(opt: Option<T>, message: string): T {\n if (isNone(opt)) {\n throw new Error(message);\n }\n return opt;\n}\n\n/**\n * Returns the first Some, or the second Option if the first is None.\n * @param opt - First Option\n * @param optb - Fallback Option\n * @returns opt if Some, optb otherwise\n * @example\n * or(some(1), some(2)) // Some(1)\n * or(none, some(2)) // Some(2)\n */\nexport function or<T>(opt: Some<T>, optb: Option<T>): Some<T>;\nexport function or<T>(opt: Option<T>, optb: Option<T>): Option<T>;\nexport function or<T>(opt: Option<T>, optb: Option<T>): Option<T> {\n return isSome(opt) ? opt : optb;\n}\n\n/**\n * Returns opt if Some, otherwise computes a fallback Option.\n * @param opt - First Option\n * @param fn - Function to compute fallback\n * @returns opt if Some, fn() otherwise\n * @example\n * orElse(some(1), () => some(2)) // Some(1)\n * orElse(none, () => some(2)) // Some(2)\n */\nexport function orElse<T>(opt: Some<T>, fn: () => Option<T>): Some<T>;\nexport function orElse<T>(opt: Option<T>, fn: () => Option<T>): Option<T>;\nexport function orElse<T>(opt: Option<T>, fn: () => Option<T>): Option<T> {\n return isSome(opt) ? opt : fn();\n}\n\n/**\n * Returns Some if exactly one of the Options is Some.\n * @param opt - First Option\n * @param optb - Second Option\n * @returns Some if exactly one is Some, None otherwise\n * @example\n * xor(some(1), none) // Some(1)\n * xor(none, some(2)) // Some(2)\n * xor(some(1), some(2)) // None\n * xor(none, none) // None\n */\nexport function xor<T>(opt: Option<T>, optb: Option<T>): Option<T> {\n if (isSome(opt) && isNone(optb)) return opt;\n if (isNone(opt) && isSome(optb)) return optb;\n return NONE;\n}\n\n/**\n * Returns optb if opt is Some, None otherwise.\n * @param opt - First Option\n * @param optb - Second Option\n * @returns optb if opt is Some, None otherwise\n * @example\n * and(some(1), some(2)) // Some(2)\n * and(none, some(2)) // None\n */\nexport function and<U>(opt: None, optb: Option<U>): None;\nexport function and<T, U>(opt: Option<T>, optb: Option<U>): Option<U>;\nexport function and<T, U>(opt: Option<T>, optb: Option<U>): Option<U> {\n return isSome(opt) ? optb : NONE;\n}\n\n/**\n * Combines two Options into an Option of a tuple.\n * @param opt - First Option\n * @param other - Second Option\n * @returns Some([a, b]) if both are Some, None otherwise\n * @example\n * zip(some(1), some('a')) // Some([1, 'a'])\n * zip(some(1), none) // None\n */\nexport function zip<T, U>(opt: Option<T>, other: Option<U>): Option<[T, U]> {\n return isSome(opt) && isSome(other) ? ([opt, other] as Some<[T, U]>) : NONE;\n}\n\n/**\n * Splits an Option of a tuple into a tuple of Options.\n * @param opt - Option containing a tuple\n * @returns Tuple of Options\n * @example\n * unzip(some([1, 'a'])) // [Some(1), Some('a')]\n * unzip(none) // [None, None]\n */\nexport function unzip<T, U>(opt: Option<[T, U]>): [Option<T>, Option<U>] {\n if (isNone(opt)) return [NONE, NONE];\n const [a, b] = opt;\n return [of(a), of(b)];\n}\n\n/**\n * Maps the value and returns it, or returns a default.\n * @param opt - The Option to map\n * @param defaultValue - Value if None\n * @param fn - Transform function\n * @returns fn(value) if Some, defaultValue otherwise\n * @example\n * mapOr(some(2), 0, x => x * 2) // 4\n * mapOr(none, 0, x => x * 2) // 0\n */\nexport function mapOr<T, U>(opt: Option<T>, defaultValue: U, fn: (value: T) => U): U {\n return isSome(opt) ? fn(opt) : defaultValue;\n}\n\n/**\n * Maps the value and returns it, or computes a default.\n * @param opt - The Option to map\n * @param defaultFn - Function to compute default\n * @param fn - Transform function\n * @returns fn(value) if Some, defaultFn() otherwise\n * @example\n * mapOrElse(some(2), () => 0, x => x * 2) // 4\n * mapOrElse(none, () => 0, x => x * 2) // 0\n */\nexport function mapOrElse<T, U>(opt: Option<T>, defaultFn: () => U, fn: (value: T) => U): U {\n return isSome(opt) ? fn(opt) : defaultFn();\n}\n\n/**\n * Flattens a nested Option.\n * @param opt - Option containing an Option\n * @returns The inner Option\n * @example\n * flatten(some(some(42))) // Some(42)\n * flatten(some(none)) // None\n * flatten(none) // None\n */\nexport function flatten<T>(opt: Option<Option<T>>): Option<T> {\n return isNone(opt) ? NONE : (opt as Option<T>);\n}\n\n/**\n * Checks if the Option contains a specific value (using ===).\n * @param opt - The Option to check\n * @param value - The value to compare\n * @returns true if Some and value matches\n * @example\n * contains(some(42), 42) // true\n * contains(some(42), 0) // false\n * contains(none, 42) // false\n */\nexport function contains<T>(opt: Option<T>, value: T): boolean {\n return isSome(opt) && opt === value;\n}\n\n/**\n * Checks if Some and the value satisfies a predicate.\n * @param opt - The Option to check\n * @param predicate - Test function\n * @returns true if Some and predicate returns true\n * @example\n * isSomeAnd(some(4), x => x > 2) // true\n * isSomeAnd(some(1), x => x > 2) // false\n * isSomeAnd(none, x => x > 2) // false\n */\nexport function isSomeAnd<T>(opt: Option<T>, predicate: (value: T) => boolean): boolean {\n return isSome(opt) && predicate(opt);\n}\n\n/**\n * Converts an Option to an array.\n * @param opt - The Option to convert\n * @returns [value] if Some, [] if None\n * @example\n * toArray(some(42)) // [42]\n * toArray(none) // []\n */\nexport function toArray<T>(opt: Option<T>): T[] {\n return isSome(opt) ? [opt] : [];\n}\n\n/**\n * Converts an Option to a nullable value.\n * @param opt - The Option to convert\n * @returns The value if Some, null if None\n * @example\n * toNullable(some(42)) // 42\n * toNullable(none) // null\n */\nexport function toNullable<T>(opt: Option<T>): T | null {\n return isSome(opt) ? opt : null;\n}\n\n/**\n * Converts an Option to an undefined-able value.\n * @param opt - The Option to convert\n * @returns The value if Some, undefined if None\n * @example\n * toUndefined(some(42)) // 42\n * toUndefined(none) // undefined\n */\nexport function toUndefined<T>(opt: Option<T>): T | undefined {\n return isSome(opt) ? opt : undefined;\n}\n\n/**\n * Pattern matches on an Option, handling both Some and None cases.\n * @param opt - The Option to match\n * @param onSome - Handler for Some case\n * @param onNone - Handler for None case\n * @returns Result of the matching handler\n * @example\n * match(some(42), x => x * 2, () => 0) // 84\n * match(none, x => x * 2, () => 0) // 0\n */\nexport function match<T, U>(opt: Option<T>, onSome: (value: T) => U, onNone: () => U): U {\n return isSome(opt) ? onSome(opt) : onNone();\n}\n\n/**\n * Converts an Option to a Result, using a provided error if None.\n * @param opt - The Option to convert\n * @param error - Error value if None\n * @returns Ok(value) if Some, Err(error) if None\n * @example\n * okOr(some(42), 'missing') // Ok(42)\n * okOr(none, 'missing') // Err('missing')\n */\nexport function okOr<T, E>(opt: Option<T>, error: E): Result<T, E> {\n return isSome(opt) ? (opt as unknown as Ok<T>) : err(error);\n}\n\n/**\n * Converts an Option to a Result, computing the error if None.\n * @param opt - The Option to convert\n * @param fn - Function to compute error\n * @returns Ok(value) if Some, Err(fn()) if None\n * @example\n * okOrElse(some(42), () => 'missing') // Ok(42)\n * okOrElse(none, () => 'missing') // Err('missing')\n */\nexport function okOrElse<T, E>(opt: Option<T>, fn: () => E): Result<T, E> {\n return isSome(opt) ? (opt as unknown as Ok<T>) : err(fn());\n}\n\n/**\n * Extracts the Ok value from a Result as an Option.\n * @param result - The Result to convert\n * @returns Some(value) if Ok, None if Err\n * @example\n * ofOk(ok(42)) // Some(42)\n * ofOk(err('failed')) // None\n */\nexport function ofOk<T, E>(result: Result<T, E>): Option<T> {\n if (!isOk(result) || !isSome(result)) {\n return NONE;\n }\n return result as Some<T>;\n}\n\n/**\n * Extracts the Err value from a Result as an Option.\n * @param result - The Result to convert\n * @returns Some(error) if Err, None if Ok\n * @example\n * ofErr(err('failed')) // Some('failed')\n * ofErr(ok(42)) // None\n */\nexport function ofErr<T, E>(result: Result<T, E>): Option<E> {\n if (!isErr(result)) {\n return NONE;\n }\n const error = (result as { error: E }).error;\n if (!isSome(error)) {\n return NONE;\n }\n return error as Some<E>;\n}\n"],"names":["and","andThen","assertSome","contains","expect","filter","filterMap","flatMap","flatten","fromNullable","fromPromise","isNone","isSome","isSomeAnd","map","mapOr","mapOrElse","match","of","ofErr","ofOk","okOr","okOrElse","or","orElse","satisfiesOption","tap","toArray","toNullable","toUndefined","unwrap","unwrapOr","unwrapOrElse","unwrapOrReturn","unzip","xor","zip","value","promise","onRejected","error","NONE","opt","onNone","message","Error","_","values","fn","collected","mapped","push","result","undefined","predicate","defaultValue","optb","other","a","b","defaultFn","onSome","err","isOk","isErr"],"mappings":";;;;;;;;;;;QA6SgBA;eAAAA;;QA1JAC;eAAAA;;QAnFAC;eAAAA;;QA+TAC;eAAAA;;QAnJAC;eAAAA;;QAxDAC;eAAAA;;QArFAC;eAAAA;;QAwCAC;eAAAA;;QA0OAC;eAAAA;;QAhWAC;eAAAA;;QAaMC;eAAAA;;QA1BLC;eAAAA,gBAAM;;QAAdC;eAAAA,gBAAM;;QAyYCC;eAAAA;;QAxRAC;eAAAA;;QAiOAC;eAAAA;;QAcAC;eAAAA;;QA2FAC;eAAAA;;QA3bSC;eAAAA,kBAAE;;QAgfXC;eAAAA;;QAfAC;eAAAA;;QAzBAC;eAAAA;;QAaAC;eAAAA;;QA3NAC;eAAAA;;QAeAC;eAAAA;;QA1LAC;eAAAA;;QA+EAC;eAAAA;;QAuPAC;eAAAA;;QAYAC;eAAAA;;QAYAC;eAAAA;;QAhPAC;eAAAA;;QAgBAC;eAAAA;;QAaAC;eAAAA;;QA3KAC;eAAAA;;QAmRAC;eAAAA;;QA1CAC;eAAAA;;QA8BAC;eAAAA;;;0BA1TuD;AAiBhE,SAAS3B,aAAgB4B,KAAwB;IACtD,OAAOnB,IAAAA,kBAAE,EAACmB;AACZ;AAWO,eAAe3B,YAAe4B,OAAmC,EAAEC,UAAkD;IAC1H,IAAI;QACF,MAAMF,QAAQ,MAAMC;QACpB,OAAOpB,IAAAA,kBAAE,EAACmB;IACZ,EAAE,OAAOG,OAAO;QACd,IAAI,CAACD,YAAY;YACf,OAAOE,cAAI;QACb;QACA,OAAOvB,IAAAA,kBAAE,EAACqB,WAAWC;IACvB;AACF;AAWO,SAASP,eAAqBS,GAAc,EAAEC,MAAe;IAClE,OAAO/B,IAAAA,gBAAM,EAAC8B,OAAQA,MAAmBC;AAC3C;AAWO,SAASzC,WAAcwC,GAAc,EAAEE,OAAgB;IAC5D,IAAIjC,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;IAC9C,IAAI,CAACY,IAAAA,eAAK,EAACZ,SAAS;QAClB,OAAOX,cAAI;IACb;IACA,MAAMD,QAAQ,AAACY,OAAwBZ,KAAK;IAC5C,IAAI,CAAC5B,IAAAA,gBAAM,EAAC4B,QAAQ;QAClB,OAAOC,cAAI;IACb;IACA,OAAOD;AACT"}
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { isSome, isNone, optionOf as of } from './types.js';
|
|
2
|
+
import type { Some, None, Option, NoneValueType, ValueType, Result, Widen } from './types.js';
|
|
3
|
+
export type { Some, None, Option };
|
|
4
|
+
export { isSome, isNone, of };
|
|
5
|
+
/**
|
|
6
|
+
* Creates an Option from a nullable value with widened types.
|
|
7
|
+
* @param value - The value to wrap
|
|
8
|
+
* @returns Some(value) if non-null, None otherwise
|
|
9
|
+
* @example
|
|
10
|
+
* fromNullable(42) // Some(42) with type Option<number>
|
|
11
|
+
* fromNullable(null) // None
|
|
12
|
+
*/
|
|
13
|
+
export declare function fromNullable(value: null): None;
|
|
14
|
+
export declare function fromNullable(value: undefined): None;
|
|
15
|
+
export declare function fromNullable<T>(value: T | NoneValueType): Option<Widen<T>>;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an Option from a Promise. Resolves to Some if successful, None on rejection.
|
|
18
|
+
* @param promise - The promise to convert
|
|
19
|
+
* @param onRejected - Optional handler for rejected promises
|
|
20
|
+
* @returns Promise resolving to Some(value) or None
|
|
21
|
+
* @example
|
|
22
|
+
* await fromPromise(Promise.resolve(42)) // Some(42)
|
|
23
|
+
* await fromPromise(Promise.reject('error')) // None
|
|
24
|
+
*/
|
|
25
|
+
export declare function fromPromise<T>(promise: Promise<T | NoneValueType>, onRejected?: (error: unknown) => T | NoneValueType): Promise<Option<T>>;
|
|
26
|
+
/**
|
|
27
|
+
* Unwraps an Option or returns a computed value if None.
|
|
28
|
+
* @param opt - The Option to unwrap
|
|
29
|
+
* @param onNone - Function called if opt is None
|
|
30
|
+
* @returns The value if Some, or the result of onNone()
|
|
31
|
+
* @example
|
|
32
|
+
* unwrapOrReturn(some(42), () => 0) // 42
|
|
33
|
+
* unwrapOrReturn(none, () => 0) // 0
|
|
34
|
+
*/
|
|
35
|
+
export declare function unwrapOrReturn<T, R>(opt: Option<T>, onNone: () => R): Widen<T> | R;
|
|
36
|
+
/**
|
|
37
|
+
* Asserts that an Option is Some, throwing if None.
|
|
38
|
+
* @param opt - The Option to assert
|
|
39
|
+
* @param message - Custom error message
|
|
40
|
+
* @throws Error if opt is None
|
|
41
|
+
* @example
|
|
42
|
+
* assertSome(some(42)) // passes
|
|
43
|
+
* assertSome(none) // throws Error
|
|
44
|
+
*/
|
|
45
|
+
export declare function assertSome<T>(opt: Option<T>, message?: string): asserts opt is Some<ValueType<T>>;
|
|
46
|
+
/**
|
|
47
|
+
* Compile-time type assertion helper to satisfy Option type constraints.
|
|
48
|
+
*
|
|
49
|
+
* WARNING: This function performs NO runtime validation. It is a no-op at
|
|
50
|
+
* runtime to preserve zero-allocation semantics. Use assertSome() if you
|
|
51
|
+
* need runtime validation that a value is Some.
|
|
52
|
+
*
|
|
53
|
+
* @param _ - The value to assert as Option (not validated at runtime)
|
|
54
|
+
* @example
|
|
55
|
+
* const value: number | null = getValue();
|
|
56
|
+
* satisfiesOption(value); // Compiles, but no runtime check
|
|
57
|
+
* // value is now typed as Option<number>
|
|
58
|
+
*/
|
|
59
|
+
export declare function satisfiesOption<T>(_: Option<T> | T): asserts _ is Option<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Maps and filters an iterable, collecting only Some values.
|
|
62
|
+
* @param values - The iterable to process
|
|
63
|
+
* @param fn - Function that returns Option for each value
|
|
64
|
+
* @returns Array of unwrapped Some values
|
|
65
|
+
* @example
|
|
66
|
+
* filterMap([1, 2, 3], n => n > 1 ? some(n * 2) : none) // [4, 6]
|
|
67
|
+
*/
|
|
68
|
+
export declare function filterMap<T, U>(values: Iterable<T>, fn: (value: T) => Option<U>): U[];
|
|
69
|
+
/**
|
|
70
|
+
* Transforms the value inside a Some, or returns None.
|
|
71
|
+
* @param opt - The Option to map
|
|
72
|
+
* @param fn - Transform function
|
|
73
|
+
* @returns Some(fn(value)) if Some, None otherwise
|
|
74
|
+
* @example
|
|
75
|
+
* map(some(2), x => x * 2) // Some(4)
|
|
76
|
+
* map(none, x => x * 2) // None
|
|
77
|
+
*/
|
|
78
|
+
export declare function map<T, U>(opt: None, fn: (value: T) => U): None;
|
|
79
|
+
export declare function map<T, U>(opt: Option<T>, fn: (value: T) => U | NoneValueType): Option<U>;
|
|
80
|
+
/**
|
|
81
|
+
* Chains Option-returning functions. Returns None if the input is None.
|
|
82
|
+
* @param opt - The Option to chain
|
|
83
|
+
* @param fn - Function returning an Option
|
|
84
|
+
* @returns The result of fn(value) if Some, None otherwise
|
|
85
|
+
* @example
|
|
86
|
+
* flatMap(some(2), x => some(x * 2)) // Some(4)
|
|
87
|
+
* flatMap(some(2), x => none) // None
|
|
88
|
+
* flatMap(none, x => some(x * 2)) // None
|
|
89
|
+
*/
|
|
90
|
+
export declare function flatMap<T, U>(opt: None, fn: (value: T) => Option<U>): None;
|
|
91
|
+
export declare function flatMap<T, U>(opt: Option<T>, fn: (value: T) => Option<U>): Option<U>;
|
|
92
|
+
/**
|
|
93
|
+
* Alias for flatMap. Chains Option-returning functions.
|
|
94
|
+
* @param opt - The Option to chain
|
|
95
|
+
* @param fn - Function returning an Option
|
|
96
|
+
* @returns The result of fn(value) if Some, None otherwise
|
|
97
|
+
*/
|
|
98
|
+
export declare function andThen<T, U>(opt: None, fn: (value: T) => Option<U>): None;
|
|
99
|
+
export declare function andThen<T, U>(opt: Option<T>, fn: (value: T) => Option<U>): Option<U>;
|
|
100
|
+
/**
|
|
101
|
+
* Executes a side effect if Some, then returns the original Option.
|
|
102
|
+
* @param opt - The Option to tap
|
|
103
|
+
* @param fn - Side effect function
|
|
104
|
+
* @returns The original Option unchanged
|
|
105
|
+
* @example
|
|
106
|
+
* tap(some(42), x => console.log(x)) // logs 42, returns Some(42)
|
|
107
|
+
*/
|
|
108
|
+
export declare function tap<T>(opt: None, fn: (value: T) => void): None;
|
|
109
|
+
export declare function tap<T>(opt: Some<T>, fn: (value: T) => void): Some<T>;
|
|
110
|
+
export declare function tap<T>(opt: Option<T>, fn: (value: T) => void): Option<T>;
|
|
111
|
+
/**
|
|
112
|
+
* Returns Some if the value passes the predicate, None otherwise.
|
|
113
|
+
* @param opt - The Option to filter
|
|
114
|
+
* @param predicate - Test function
|
|
115
|
+
* @returns Some if predicate returns true, None otherwise
|
|
116
|
+
* @example
|
|
117
|
+
* filter(some(4), x => x > 2) // Some(4)
|
|
118
|
+
* filter(some(1), x => x > 2) // None
|
|
119
|
+
*/
|
|
120
|
+
export declare function filter<T>(opt: None, predicate: (value: T) => boolean): None;
|
|
121
|
+
export declare function filter<T>(opt: Option<T>, predicate: (value: T) => boolean): Option<T>;
|
|
122
|
+
/**
|
|
123
|
+
* Extracts the value from Some, throws if None.
|
|
124
|
+
* @param opt - The Option to unwrap
|
|
125
|
+
* @returns The contained value
|
|
126
|
+
* @throws Error if opt is None
|
|
127
|
+
* @example
|
|
128
|
+
* unwrap(some(42)) // 42
|
|
129
|
+
* unwrap(none) // throws Error
|
|
130
|
+
*/
|
|
131
|
+
export declare function unwrap<T>(opt: Option<T>): T;
|
|
132
|
+
/**
|
|
133
|
+
* Extracts the value from Some, or returns a default value.
|
|
134
|
+
* @param opt - The Option to unwrap
|
|
135
|
+
* @param defaultValue - Value to return if None
|
|
136
|
+
* @returns The contained value or defaultValue
|
|
137
|
+
* @example
|
|
138
|
+
* unwrapOr(some(42), 0) // 42
|
|
139
|
+
* unwrapOr(none, 0) // 0
|
|
140
|
+
*/
|
|
141
|
+
export declare function unwrapOr<T>(opt: Option<T>, defaultValue: T): T;
|
|
142
|
+
/**
|
|
143
|
+
* Extracts the value from Some, or computes a default.
|
|
144
|
+
* @param opt - The Option to unwrap
|
|
145
|
+
* @param fn - Function to compute default value
|
|
146
|
+
* @returns The contained value or fn()
|
|
147
|
+
* @example
|
|
148
|
+
* unwrapOrElse(some(42), () => 0) // 42
|
|
149
|
+
* unwrapOrElse(none, () => 0) // 0
|
|
150
|
+
*/
|
|
151
|
+
export declare function unwrapOrElse<T>(opt: Option<T>, fn: () => T): T;
|
|
152
|
+
/**
|
|
153
|
+
* Extracts the value from Some, throws with custom message if None.
|
|
154
|
+
* @param opt - The Option to unwrap
|
|
155
|
+
* @param message - Error message if None
|
|
156
|
+
* @returns The contained value
|
|
157
|
+
* @throws Error with message if opt is None
|
|
158
|
+
* @example
|
|
159
|
+
* expect(some(42), 'missing value') // 42
|
|
160
|
+
* expect(none, 'missing value') // throws Error('missing value')
|
|
161
|
+
*/
|
|
162
|
+
export declare function expect<T>(opt: Option<T>, message: string): T;
|
|
163
|
+
/**
|
|
164
|
+
* Returns the first Some, or the second Option if the first is None.
|
|
165
|
+
* @param opt - First Option
|
|
166
|
+
* @param optb - Fallback Option
|
|
167
|
+
* @returns opt if Some, optb otherwise
|
|
168
|
+
* @example
|
|
169
|
+
* or(some(1), some(2)) // Some(1)
|
|
170
|
+
* or(none, some(2)) // Some(2)
|
|
171
|
+
*/
|
|
172
|
+
export declare function or<T>(opt: Some<T>, optb: Option<T>): Some<T>;
|
|
173
|
+
export declare function or<T>(opt: Option<T>, optb: Option<T>): Option<T>;
|
|
174
|
+
/**
|
|
175
|
+
* Returns opt if Some, otherwise computes a fallback Option.
|
|
176
|
+
* @param opt - First Option
|
|
177
|
+
* @param fn - Function to compute fallback
|
|
178
|
+
* @returns opt if Some, fn() otherwise
|
|
179
|
+
* @example
|
|
180
|
+
* orElse(some(1), () => some(2)) // Some(1)
|
|
181
|
+
* orElse(none, () => some(2)) // Some(2)
|
|
182
|
+
*/
|
|
183
|
+
export declare function orElse<T>(opt: Some<T>, fn: () => Option<T>): Some<T>;
|
|
184
|
+
export declare function orElse<T>(opt: Option<T>, fn: () => Option<T>): Option<T>;
|
|
185
|
+
/**
|
|
186
|
+
* Returns Some if exactly one of the Options is Some.
|
|
187
|
+
* @param opt - First Option
|
|
188
|
+
* @param optb - Second Option
|
|
189
|
+
* @returns Some if exactly one is Some, None otherwise
|
|
190
|
+
* @example
|
|
191
|
+
* xor(some(1), none) // Some(1)
|
|
192
|
+
* xor(none, some(2)) // Some(2)
|
|
193
|
+
* xor(some(1), some(2)) // None
|
|
194
|
+
* xor(none, none) // None
|
|
195
|
+
*/
|
|
196
|
+
export declare function xor<T>(opt: Option<T>, optb: Option<T>): Option<T>;
|
|
197
|
+
/**
|
|
198
|
+
* Returns optb if opt is Some, None otherwise.
|
|
199
|
+
* @param opt - First Option
|
|
200
|
+
* @param optb - Second Option
|
|
201
|
+
* @returns optb if opt is Some, None otherwise
|
|
202
|
+
* @example
|
|
203
|
+
* and(some(1), some(2)) // Some(2)
|
|
204
|
+
* and(none, some(2)) // None
|
|
205
|
+
*/
|
|
206
|
+
export declare function and<U>(opt: None, optb: Option<U>): None;
|
|
207
|
+
export declare function and<T, U>(opt: Option<T>, optb: Option<U>): Option<U>;
|
|
208
|
+
/**
|
|
209
|
+
* Combines two Options into an Option of a tuple.
|
|
210
|
+
* @param opt - First Option
|
|
211
|
+
* @param other - Second Option
|
|
212
|
+
* @returns Some([a, b]) if both are Some, None otherwise
|
|
213
|
+
* @example
|
|
214
|
+
* zip(some(1), some('a')) // Some([1, 'a'])
|
|
215
|
+
* zip(some(1), none) // None
|
|
216
|
+
*/
|
|
217
|
+
export declare function zip<T, U>(opt: Option<T>, other: Option<U>): Option<[T, U]>;
|
|
218
|
+
/**
|
|
219
|
+
* Splits an Option of a tuple into a tuple of Options.
|
|
220
|
+
* @param opt - Option containing a tuple
|
|
221
|
+
* @returns Tuple of Options
|
|
222
|
+
* @example
|
|
223
|
+
* unzip(some([1, 'a'])) // [Some(1), Some('a')]
|
|
224
|
+
* unzip(none) // [None, None]
|
|
225
|
+
*/
|
|
226
|
+
export declare function unzip<T, U>(opt: Option<[T, U]>): [Option<T>, Option<U>];
|
|
227
|
+
/**
|
|
228
|
+
* Maps the value and returns it, or returns a default.
|
|
229
|
+
* @param opt - The Option to map
|
|
230
|
+
* @param defaultValue - Value if None
|
|
231
|
+
* @param fn - Transform function
|
|
232
|
+
* @returns fn(value) if Some, defaultValue otherwise
|
|
233
|
+
* @example
|
|
234
|
+
* mapOr(some(2), 0, x => x * 2) // 4
|
|
235
|
+
* mapOr(none, 0, x => x * 2) // 0
|
|
236
|
+
*/
|
|
237
|
+
export declare function mapOr<T, U>(opt: Option<T>, defaultValue: U, fn: (value: T) => U): U;
|
|
238
|
+
/**
|
|
239
|
+
* Maps the value and returns it, or computes a default.
|
|
240
|
+
* @param opt - The Option to map
|
|
241
|
+
* @param defaultFn - Function to compute default
|
|
242
|
+
* @param fn - Transform function
|
|
243
|
+
* @returns fn(value) if Some, defaultFn() otherwise
|
|
244
|
+
* @example
|
|
245
|
+
* mapOrElse(some(2), () => 0, x => x * 2) // 4
|
|
246
|
+
* mapOrElse(none, () => 0, x => x * 2) // 0
|
|
247
|
+
*/
|
|
248
|
+
export declare function mapOrElse<T, U>(opt: Option<T>, defaultFn: () => U, fn: (value: T) => U): U;
|
|
249
|
+
/**
|
|
250
|
+
* Flattens a nested Option.
|
|
251
|
+
* @param opt - Option containing an Option
|
|
252
|
+
* @returns The inner Option
|
|
253
|
+
* @example
|
|
254
|
+
* flatten(some(some(42))) // Some(42)
|
|
255
|
+
* flatten(some(none)) // None
|
|
256
|
+
* flatten(none) // None
|
|
257
|
+
*/
|
|
258
|
+
export declare function flatten<T>(opt: Option<Option<T>>): Option<T>;
|
|
259
|
+
/**
|
|
260
|
+
* Checks if the Option contains a specific value (using ===).
|
|
261
|
+
* @param opt - The Option to check
|
|
262
|
+
* @param value - The value to compare
|
|
263
|
+
* @returns true if Some and value matches
|
|
264
|
+
* @example
|
|
265
|
+
* contains(some(42), 42) // true
|
|
266
|
+
* contains(some(42), 0) // false
|
|
267
|
+
* contains(none, 42) // false
|
|
268
|
+
*/
|
|
269
|
+
export declare function contains<T>(opt: Option<T>, value: T): boolean;
|
|
270
|
+
/**
|
|
271
|
+
* Checks if Some and the value satisfies a predicate.
|
|
272
|
+
* @param opt - The Option to check
|
|
273
|
+
* @param predicate - Test function
|
|
274
|
+
* @returns true if Some and predicate returns true
|
|
275
|
+
* @example
|
|
276
|
+
* isSomeAnd(some(4), x => x > 2) // true
|
|
277
|
+
* isSomeAnd(some(1), x => x > 2) // false
|
|
278
|
+
* isSomeAnd(none, x => x > 2) // false
|
|
279
|
+
*/
|
|
280
|
+
export declare function isSomeAnd<T>(opt: Option<T>, predicate: (value: T) => boolean): boolean;
|
|
281
|
+
/**
|
|
282
|
+
* Converts an Option to an array.
|
|
283
|
+
* @param opt - The Option to convert
|
|
284
|
+
* @returns [value] if Some, [] if None
|
|
285
|
+
* @example
|
|
286
|
+
* toArray(some(42)) // [42]
|
|
287
|
+
* toArray(none) // []
|
|
288
|
+
*/
|
|
289
|
+
export declare function toArray<T>(opt: Option<T>): T[];
|
|
290
|
+
/**
|
|
291
|
+
* Converts an Option to a nullable value.
|
|
292
|
+
* @param opt - The Option to convert
|
|
293
|
+
* @returns The value if Some, null if None
|
|
294
|
+
* @example
|
|
295
|
+
* toNullable(some(42)) // 42
|
|
296
|
+
* toNullable(none) // null
|
|
297
|
+
*/
|
|
298
|
+
export declare function toNullable<T>(opt: Option<T>): T | null;
|
|
299
|
+
/**
|
|
300
|
+
* Converts an Option to an undefined-able value.
|
|
301
|
+
* @param opt - The Option to convert
|
|
302
|
+
* @returns The value if Some, undefined if None
|
|
303
|
+
* @example
|
|
304
|
+
* toUndefined(some(42)) // 42
|
|
305
|
+
* toUndefined(none) // undefined
|
|
306
|
+
*/
|
|
307
|
+
export declare function toUndefined<T>(opt: Option<T>): T | undefined;
|
|
308
|
+
/**
|
|
309
|
+
* Pattern matches on an Option, handling both Some and None cases.
|
|
310
|
+
* @param opt - The Option to match
|
|
311
|
+
* @param onSome - Handler for Some case
|
|
312
|
+
* @param onNone - Handler for None case
|
|
313
|
+
* @returns Result of the matching handler
|
|
314
|
+
* @example
|
|
315
|
+
* match(some(42), x => x * 2, () => 0) // 84
|
|
316
|
+
* match(none, x => x * 2, () => 0) // 0
|
|
317
|
+
*/
|
|
318
|
+
export declare function match<T, U>(opt: Option<T>, onSome: (value: T) => U, onNone: () => U): U;
|
|
319
|
+
/**
|
|
320
|
+
* Converts an Option to a Result, using a provided error if None.
|
|
321
|
+
* @param opt - The Option to convert
|
|
322
|
+
* @param error - Error value if None
|
|
323
|
+
* @returns Ok(value) if Some, Err(error) if None
|
|
324
|
+
* @example
|
|
325
|
+
* okOr(some(42), 'missing') // Ok(42)
|
|
326
|
+
* okOr(none, 'missing') // Err('missing')
|
|
327
|
+
*/
|
|
328
|
+
export declare function okOr<T, E>(opt: Option<T>, error: E): Result<T, E>;
|
|
329
|
+
/**
|
|
330
|
+
* Converts an Option to a Result, computing the error if None.
|
|
331
|
+
* @param opt - The Option to convert
|
|
332
|
+
* @param fn - Function to compute error
|
|
333
|
+
* @returns Ok(value) if Some, Err(fn()) if None
|
|
334
|
+
* @example
|
|
335
|
+
* okOrElse(some(42), () => 'missing') // Ok(42)
|
|
336
|
+
* okOrElse(none, () => 'missing') // Err('missing')
|
|
337
|
+
*/
|
|
338
|
+
export declare function okOrElse<T, E>(opt: Option<T>, fn: () => E): Result<T, E>;
|
|
339
|
+
/**
|
|
340
|
+
* Extracts the Ok value from a Result as an Option.
|
|
341
|
+
* @param result - The Result to convert
|
|
342
|
+
* @returns Some(value) if Ok, None if Err
|
|
343
|
+
* @example
|
|
344
|
+
* ofOk(ok(42)) // Some(42)
|
|
345
|
+
* ofOk(err('failed')) // None
|
|
346
|
+
*/
|
|
347
|
+
export declare function ofOk<T, E>(result: Result<T, E>): Option<T>;
|
|
348
|
+
/**
|
|
349
|
+
* Extracts the Err value from a Result as an Option.
|
|
350
|
+
* @param result - The Result to convert
|
|
351
|
+
* @returns Some(error) if Err, None if Ok
|
|
352
|
+
* @example
|
|
353
|
+
* ofErr(err('failed')) // Some('failed')
|
|
354
|
+
* ofErr(ok(42)) // None
|
|
355
|
+
*/
|
|
356
|
+
export declare function ofErr<T, E>(result: Result<T, E>): Option<E>;
|