retuple 0.0.1 → 1.0.0-next.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2025 Matthew Wilson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ ## Retuple
package/dist/index.cjs ADDED
@@ -0,0 +1,531 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _ResultAsync_inner;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Result = exports.RetupleThrownValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
16
+ exports.Ok = Ok;
17
+ exports.Err = Err;
18
+ exports.from = from;
19
+ exports.safe = safe;
20
+ exports.safeAsync = safeAsync;
21
+ exports.safePromise = safePromise;
22
+ class RetupleUnwrapFailed extends Error {
23
+ constructor(value, msg = "Unwrap failed") {
24
+ super(msg, value instanceof Error ? { cause: value } : undefined);
25
+ this.value = value;
26
+ }
27
+ }
28
+ exports.RetupleUnwrapFailed = RetupleUnwrapFailed;
29
+ class RetupleUnwrapErrFailed extends Error {
30
+ constructor(value, msg = "Unwrap error failed") {
31
+ super(msg);
32
+ this.value = value;
33
+ }
34
+ }
35
+ exports.RetupleUnwrapErrFailed = RetupleUnwrapErrFailed;
36
+ class RetupleExpectFailed extends Error {
37
+ constructor(value) {
38
+ super("Expect failed");
39
+ this.value = value;
40
+ }
41
+ }
42
+ exports.RetupleExpectFailed = RetupleExpectFailed;
43
+ class RetupleThrownValueError extends Error {
44
+ constructor(value) {
45
+ super("Caught value was not an instance of Error");
46
+ this.value = value;
47
+ }
48
+ }
49
+ exports.RetupleThrownValueError = RetupleThrownValueError;
50
+ /**
51
+ * ## Result
52
+ *
53
+ * @TODO
54
+ */
55
+ exports.Result = {
56
+ Ok,
57
+ Err,
58
+ from,
59
+ safe,
60
+ safeAsync,
61
+ safePromise,
62
+ };
63
+ exports.default = exports.Result;
64
+ function Ok(val) {
65
+ return new ResultOk(val);
66
+ }
67
+ function Err(err) {
68
+ return new ResultErr(err);
69
+ }
70
+ function from(value, error) {
71
+ if (value) {
72
+ return new ResultOk(value);
73
+ }
74
+ if (error) {
75
+ return new ResultErr(error());
76
+ }
77
+ return new ResultErr(true);
78
+ }
79
+ function safe(f, mapError = ensureError) {
80
+ try {
81
+ return Ok(f());
82
+ }
83
+ catch (err) {
84
+ return Err(mapError(err));
85
+ }
86
+ }
87
+ function safeAsync(f, mapError = ensureError) {
88
+ return new ResultAsync((async () => {
89
+ try {
90
+ return Ok(await f());
91
+ }
92
+ catch (err) {
93
+ return Err(await mapError(err));
94
+ }
95
+ })());
96
+ }
97
+ function safePromise(promise, mapError = ensureError) {
98
+ return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
99
+ }
100
+ /**
101
+ * ## Ok
102
+ *
103
+ * @TODO
104
+ */
105
+ class ResultOk extends Array {
106
+ constructor(value) {
107
+ super(2);
108
+ this[0] = undefined;
109
+ this[1] = value;
110
+ }
111
+ $value() {
112
+ return this[1];
113
+ }
114
+ $isOk() {
115
+ return true;
116
+ }
117
+ $isOkAnd(f) {
118
+ return f(this[1]);
119
+ }
120
+ $isErr() {
121
+ return false;
122
+ }
123
+ $isErrAnd() {
124
+ return false;
125
+ }
126
+ $expect() {
127
+ return this[1];
128
+ }
129
+ $unwrap() {
130
+ return this[1];
131
+ }
132
+ $unwrapErr(msg) {
133
+ throw new RetupleUnwrapErrFailed(this[1], msg);
134
+ }
135
+ $unwrapOr() {
136
+ return this[1];
137
+ }
138
+ $unwrapOrElse() {
139
+ return this[1];
140
+ }
141
+ $map(f) {
142
+ return new ResultOk(f(this[1]));
143
+ }
144
+ $mapErr() {
145
+ return this;
146
+ }
147
+ $mapOr(_def, f) {
148
+ return new ResultOk(f(this[1]));
149
+ }
150
+ $mapOrElse(_def, f) {
151
+ return new ResultOk(f(this[1]));
152
+ }
153
+ $or() {
154
+ return this;
155
+ }
156
+ $orElse() {
157
+ return this;
158
+ }
159
+ $orSafe() {
160
+ return this;
161
+ }
162
+ $and(and) {
163
+ return and;
164
+ }
165
+ $andThen(f) {
166
+ return f(this[1]);
167
+ }
168
+ $andThrough(f) {
169
+ const res = f(this[1]);
170
+ return res instanceof ResultErr ? res : this;
171
+ }
172
+ $andSafe(f, mapError = ensureError) {
173
+ try {
174
+ return new ResultOk(f(this[1]));
175
+ }
176
+ catch (err) {
177
+ return new ResultErr(mapError(err));
178
+ }
179
+ }
180
+ $peek(f) {
181
+ f(this);
182
+ return this;
183
+ }
184
+ $tap(f) {
185
+ f(this[1]);
186
+ return this;
187
+ }
188
+ $tapErr() {
189
+ return this;
190
+ }
191
+ $flatten() {
192
+ return this[1];
193
+ }
194
+ $async() {
195
+ return new ResultAsync(Promise.resolve(this));
196
+ }
197
+ $promise() {
198
+ return Promise.resolve(this);
199
+ }
200
+ }
201
+ /**
202
+ * ## Err
203
+ *
204
+ * @TODO
205
+ */
206
+ class ResultErr extends Array {
207
+ constructor(err) {
208
+ super(2);
209
+ this[0] = err;
210
+ this[1] = undefined;
211
+ }
212
+ $value() {
213
+ return this[0];
214
+ }
215
+ $isOk() {
216
+ return false;
217
+ }
218
+ $isOkAnd() {
219
+ return false;
220
+ }
221
+ $isErr() {
222
+ return true;
223
+ }
224
+ $isErrAnd(f) {
225
+ return f(this[0]);
226
+ }
227
+ $expect() {
228
+ if (this[0] instanceof Error) {
229
+ throw this[0];
230
+ }
231
+ throw new RetupleExpectFailed(this[0]);
232
+ }
233
+ $unwrap(msg) {
234
+ throw new RetupleUnwrapFailed(this[0], msg);
235
+ }
236
+ $unwrapErr() {
237
+ return this[0];
238
+ }
239
+ $unwrapOr(def) {
240
+ return def;
241
+ }
242
+ $unwrapOrElse(f) {
243
+ return f();
244
+ }
245
+ $map() {
246
+ return this;
247
+ }
248
+ $mapErr(f) {
249
+ return new ResultErr(f(this[0]));
250
+ }
251
+ $mapOr(def) {
252
+ return new ResultOk(def);
253
+ }
254
+ $mapOrElse(def) {
255
+ return new ResultOk(def(this[0]));
256
+ }
257
+ $or(or) {
258
+ return or;
259
+ }
260
+ $orElse(f) {
261
+ return f(this[0]);
262
+ }
263
+ $orSafe(f, mapError = ensureError) {
264
+ try {
265
+ return new ResultOk(f(this[0]));
266
+ }
267
+ catch (err) {
268
+ return new ResultErr(mapError(err));
269
+ }
270
+ }
271
+ $and() {
272
+ return this;
273
+ }
274
+ $andThen() {
275
+ return this;
276
+ }
277
+ $andThrough() {
278
+ return this;
279
+ }
280
+ $andSafe() {
281
+ return this;
282
+ }
283
+ $peek(f) {
284
+ f(this);
285
+ return this;
286
+ }
287
+ $tap() {
288
+ return this;
289
+ }
290
+ $tapErr(f) {
291
+ f(this[0]);
292
+ return this;
293
+ }
294
+ $flatten() {
295
+ return this;
296
+ }
297
+ $async() {
298
+ return new ResultAsync(Promise.resolve(this));
299
+ }
300
+ $promise() {
301
+ return Promise.resolve(this);
302
+ }
303
+ }
304
+ /**
305
+ * ## ResultAsync
306
+ *
307
+ * @TODO
308
+ */
309
+ class ResultAsync {
310
+ constructor(inner) {
311
+ _ResultAsync_inner.set(this, void 0);
312
+ __classPrivateFieldSet(this, _ResultAsync_inner, inner, "f");
313
+ }
314
+ then(onfulfilled, onrejected) {
315
+ return __classPrivateFieldGet(this, _ResultAsync_inner, "f").then(onfulfilled, onrejected);
316
+ }
317
+ /**
318
+ * @TODO
319
+ */
320
+ async $value() {
321
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$value();
322
+ }
323
+ /**
324
+ * @TODO
325
+ */
326
+ async $expect() {
327
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$expect();
328
+ }
329
+ /**
330
+ * @TODO
331
+ */
332
+ async $unwrap(msg) {
333
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrap(msg);
334
+ }
335
+ /**
336
+ * @TODO
337
+ */
338
+ async $unwrapErr(msg) {
339
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrapErr(msg);
340
+ }
341
+ /**
342
+ * @TODO
343
+ */
344
+ async $unwrapOr(def) {
345
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrapOr(def);
346
+ }
347
+ /**
348
+ * @TODO
349
+ */
350
+ async $unwrapOrElse(f) {
351
+ const res = await __classPrivateFieldGet(this, _ResultAsync_inner, "f");
352
+ return res instanceof ResultOk ? res[1] : f();
353
+ }
354
+ /**
355
+ * @TODO
356
+ */
357
+ $map(f) {
358
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
359
+ return res instanceof ResultOk
360
+ ? new ResultOk(f(res[1]))
361
+ : res;
362
+ }));
363
+ }
364
+ /**
365
+ * @TODO
366
+ */
367
+ $mapErr(f) {
368
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
369
+ return res instanceof ResultErr
370
+ ? new ResultErr(f(res[0]))
371
+ : res;
372
+ }));
373
+ }
374
+ /**
375
+ * @TODO
376
+ */
377
+ $mapOr(def, f) {
378
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
379
+ return res instanceof ResultOk
380
+ ? new ResultOk(f(res[1]))
381
+ : new ResultOk(def);
382
+ }));
383
+ }
384
+ /**
385
+ * @TODO
386
+ */
387
+ $mapOrElse(def, f) {
388
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
389
+ return res instanceof ResultOk
390
+ ? new ResultOk(f(res[1]))
391
+ : new ResultOk(def(res[0]));
392
+ }));
393
+ }
394
+ /**
395
+ * @TODO
396
+ */
397
+ $or(or) {
398
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
399
+ return res instanceof ResultErr ? await or : res;
400
+ }));
401
+ }
402
+ /**
403
+ * @TODO
404
+ */
405
+ $orElse(f) {
406
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
407
+ return res instanceof ResultErr ? await f(res[0]) : res;
408
+ }));
409
+ }
410
+ /**
411
+ * @TODO
412
+ */
413
+ $orSafe(f, mapError = ensureError) {
414
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
415
+ if (res instanceof ResultOk) {
416
+ return res;
417
+ }
418
+ try {
419
+ return new ResultOk(await f(res[0]));
420
+ }
421
+ catch (err) {
422
+ return new ResultErr(mapError(err));
423
+ }
424
+ }));
425
+ }
426
+ /**
427
+ * @TODO
428
+ */
429
+ $and(and) {
430
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
431
+ return res instanceof ResultOk ? await and : res;
432
+ }));
433
+ }
434
+ /**
435
+ * @TODO
436
+ */
437
+ $andThen(f) {
438
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
439
+ return res instanceof ResultOk ? await f(res[1]) : res;
440
+ }));
441
+ }
442
+ /**
443
+ * @TODO
444
+ */
445
+ $andThrough(f) {
446
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
447
+ if (res instanceof ResultOk) {
448
+ const through = await f(res[1]);
449
+ if (through instanceof ResultErr) {
450
+ return through;
451
+ }
452
+ }
453
+ return res;
454
+ }));
455
+ }
456
+ /**
457
+ * @TODO
458
+ */
459
+ $andSafe(f, mapError = ensureError) {
460
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
461
+ if (res instanceof ResultErr) {
462
+ return res;
463
+ }
464
+ try {
465
+ return new ResultOk(await f(res[1]));
466
+ }
467
+ catch (err) {
468
+ return new ResultErr(mapError(err));
469
+ }
470
+ }));
471
+ }
472
+ /**
473
+ * @TODO
474
+ */
475
+ $peek(f) {
476
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
477
+ await f(res);
478
+ return res;
479
+ }));
480
+ }
481
+ /**
482
+ * @TODO
483
+ */
484
+ $tap(f) {
485
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
486
+ if (res instanceof ResultOk) {
487
+ await f(res[1]);
488
+ }
489
+ return res;
490
+ }));
491
+ }
492
+ /**
493
+ * @TODO
494
+ */
495
+ $tapErr(f) {
496
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
497
+ if (res instanceof ResultErr) {
498
+ await f(res[0]);
499
+ }
500
+ return res;
501
+ }));
502
+ }
503
+ /**
504
+ * @TODO
505
+ */
506
+ $promise() {
507
+ return Promise.resolve(this);
508
+ }
509
+ }
510
+ _ResultAsync_inner = new WeakMap();
511
+ function ensureError(err) {
512
+ if (err instanceof Error) {
513
+ return err;
514
+ }
515
+ return new RetupleThrownValueError(err);
516
+ }
517
+ Object.freeze(exports.Result);
518
+ Object.freeze(ResultOk);
519
+ Object.freeze(ResultErr);
520
+ Object.freeze(ResultAsync);
521
+ Object.freeze(RetupleUnwrapFailed);
522
+ Object.freeze(RetupleUnwrapErrFailed);
523
+ Object.freeze(RetupleExpectFailed);
524
+ Object.freeze(RetupleThrownValueError);
525
+ Object.freeze(ResultOk.prototype);
526
+ Object.freeze(ResultErr.prototype);
527
+ Object.freeze(ResultAsync.prototype);
528
+ Object.freeze(RetupleUnwrapFailed.prototype);
529
+ Object.freeze(RetupleUnwrapErrFailed.prototype);
530
+ Object.freeze(RetupleExpectFailed.prototype);
531
+ Object.freeze(RetupleThrownValueError.prototype);