jsrepo 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,736 @@
1
+ /*
2
+ jsrepo 1.0.0-next.18
3
+ Installed from github/ieedan/std
4
+ 11-18-2024
5
+ */
6
+
7
+ /** This is just a helper type used only within this file */
8
+ type _Result<T, E> = { ok: true; val: T } | { ok: false; err: E };
9
+
10
+ /** Result allows you to show to a consumer that a function might throw and force them to handle it.
11
+ *
12
+ * `T` Value type
13
+ *
14
+ * `E` Error type
15
+ *
16
+ * ## Usage
17
+ *
18
+ * ```ts
19
+ * function functionThatMightFail(): Result<T, E>;
20
+ * ```
21
+ *
22
+ * ## Examples
23
+ *
24
+ * ```ts
25
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");
26
+ *
27
+ * const result = functionThatMightFail();
28
+ *
29
+ * console.log(result.unwrap()); // "Hello, World!"
30
+ * ```
31
+ */
32
+ class Result<T, E> {
33
+ private readonly _result: _Result<T, E>;
34
+
35
+ constructor(result: _Result<T, E>) {
36
+ this._result = result;
37
+ }
38
+
39
+ /** Allows you to run callbacks based on the result.
40
+ *
41
+ * @param success callback to be run when result is success
42
+ * @param failure callback to be run when result is failure
43
+ * @returns
44
+ *
45
+ * ## Usage
46
+ *
47
+ * ```ts
48
+ * result.match(
49
+ * (val) => val,
50
+ * () => {
51
+ * throw new Error('oops!')
52
+ * }
53
+ * );
54
+ * ```
55
+ *
56
+ * ## Examples
57
+ *
58
+ * ```ts
59
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");
60
+ *
61
+ * const result = functionThatMightFail();
62
+ *
63
+ * const val = result.match(
64
+ * (val) => val,
65
+ * () => {
66
+ * throw new Error('oops!')
67
+ * }
68
+ * );
69
+ *
70
+ * console.log(val); // "Hello, World!"
71
+ * ```
72
+ */
73
+ match<A, B = A>(success: (val: T) => A, failure: (err: E) => B): A | B {
74
+ if (!this._result.ok) {
75
+ return failure(this._result.err);
76
+ }
77
+
78
+ return success(this._result.val);
79
+ }
80
+
81
+ /** Maps `Result<T, E>` to `Result<A, E>` using the passed mapping function
82
+ *
83
+ * @param fn Mapping function
84
+ * @returns
85
+ *
86
+ * ## Usage
87
+ *
88
+ * ```ts
89
+ * result.map((val) => val.length);
90
+ * ```
91
+ *
92
+ * ## Examples
93
+ *
94
+ * ```ts
95
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");
96
+ *
97
+ * const result = functionThatMightFail();
98
+ *
99
+ * const hello = result.map((val) => val.slice(0, 5));
100
+ *
101
+ * console.log(hello.unwrap()); // "Hello"
102
+ * ```
103
+ */
104
+ map<A>(fn: (val: T) => A): Result<A, E> {
105
+ return this.match(
106
+ (val) => Ok(fn(val)),
107
+ (err) => Err(err)
108
+ );
109
+ }
110
+
111
+ /** In the `Ok` case returns the mapped value using the function else returns `defaultVal`
112
+ *
113
+ * @param defaultVal Value to be returned when `Err`
114
+ * @param fn Mapping function to map in case of `Ok`
115
+ * @returns
116
+ *
117
+ * ## Usage
118
+ *
119
+ * ```ts
120
+ * result.mapOr(1, (val) => val.length);
121
+ * ```
122
+ *
123
+ * ## Examples
124
+ *
125
+ * ### When `Ok`
126
+ *
127
+ * ```ts
128
+ * const functionThatMightFail = (): Result<string, string> => Ok("foo");
129
+ *
130
+ * const result = functionThatMightFail();
131
+ *
132
+ * const length = result.mapOr(1, (val) => val.length);
133
+ *
134
+ * console.log(length); // 3
135
+ * ```
136
+ *
137
+ * ### When `Err`
138
+ *
139
+ * ```ts
140
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
141
+ *
142
+ * const result = functionThatMightFail();
143
+ *
144
+ * const length = result.mapOr(1, (val) => val.length);
145
+ *
146
+ * console.log(length); // 1
147
+ * ```
148
+ */
149
+ mapOr<A>(defaultVal: A, fn: (val: T) => A): A {
150
+ return this.match(
151
+ (val) => fn(val),
152
+ (_) => defaultVal
153
+ );
154
+ }
155
+
156
+ /** In the `Ok` case returns the mapped value using `fn` else returns value of `def`
157
+ *
158
+ * @param def Mapping function called when `Err`
159
+ * @param fn Mapping function called when `Ok`
160
+ * @returns
161
+ *
162
+ * ## Usage
163
+ *
164
+ * ```ts
165
+ * result.mapOrElse(() => 1, (val) => val.length);
166
+ * ```
167
+ *
168
+ * ## Examples
169
+ *
170
+ * ### When `Ok`
171
+ *
172
+ * ```ts
173
+ * const functionThatMightFail = (): Result<string, string> => Ok("foo");
174
+ *
175
+ * const result = functionThatMightFail();
176
+ *
177
+ * const length = result.mapOrElse(() => 1, (val) => val.length);
178
+ *
179
+ * console.log(length); // 3
180
+ * ```
181
+ *
182
+ * ### When `Err`
183
+ *
184
+ * ```ts
185
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
186
+ *
187
+ * const result = functionThatMightFail();
188
+ *
189
+ * const length = result.mapOr(() => 1, (val) => val.length);
190
+ *
191
+ * console.log(length); // 1
192
+ * ```
193
+ */
194
+ mapOrElse<A>(def: (err: E) => A, fn: (val: T) => A): A {
195
+ return this.match(
196
+ (val) => fn(val),
197
+ (err) => def(err)
198
+ );
199
+ }
200
+
201
+ /** Maps `Result<T, E>` to `Result<T, A>` using the passed mapping function
202
+ *
203
+ * @param fn Mapping function
204
+ * @returns
205
+ *
206
+ * ## Usage
207
+ *
208
+ * ```ts
209
+ * result.mapErr((err) => getCodeMsg(err));
210
+ * ```
211
+ *
212
+ * ## Examples
213
+ *
214
+ * ```ts
215
+ * const functionThatMightFail = (): Result<string, string> => Err(10);
216
+ *
217
+ * const result = functionThatMightFail();
218
+ *
219
+ * const message = result.mapErr(() => "Error");
220
+ *
221
+ * console.log(message); // "Error"
222
+ * ```
223
+ */
224
+ mapErr<A>(fn: (err: E) => A): Result<T, A> {
225
+ return this.match(
226
+ (val) => Ok(val),
227
+ (err) => Err(fn(err))
228
+ );
229
+ }
230
+
231
+ /** In the `Err` case returns the mapped value using the function else returns `defaultVal`
232
+ *
233
+ * @param defaultVal Value to be returned when `Ok`
234
+ * @param fn Mapping function to map in case of `Err`
235
+ * @returns
236
+ *
237
+ * ## Usage
238
+ *
239
+ * ```ts
240
+ * result.mapErrOr("Should've been error", (err) => getCodeMsg(err));
241
+ * ```
242
+ *
243
+ * ## Examples
244
+ *
245
+ * ### When `Ok`
246
+ *
247
+ * ```ts
248
+ * const functionThatMightFail = (): Result<string, string> => Ok("foo");
249
+ *
250
+ * const result = functionThatMightFail();
251
+ *
252
+ * const message = result.mapErrOr("Should've been error", () => "Error");
253
+ *
254
+ * console.log(message); // "Should've been error"
255
+ * ```
256
+ *
257
+ * ### When `Err`
258
+ *
259
+ * ```ts
260
+ * const functionThatMightFail = (): Result<string, string> => Err(10);
261
+ *
262
+ * const result = functionThatMightFail();
263
+ *
264
+ * const message = result.mapErrOr("Should've been error", () => "Error");
265
+ *
266
+ * console.log(message); // "Error"
267
+ * ```
268
+ */
269
+ mapErrOr<A>(defaultVal: A, fn: (err: E) => A): A {
270
+ return this.match(
271
+ (_) => defaultVal,
272
+ (err) => fn(err)
273
+ );
274
+ }
275
+
276
+ /** In the `Err` case returns the mapped value using the function else returns value of `def`
277
+ *
278
+ * @param def Mapping function called when `Ok`
279
+ * @param fn Mapping function called when `Err`
280
+ * @returns
281
+ *
282
+ * ## Usage
283
+ *
284
+ * ```ts
285
+ * result.mapErrOrElse(() => "Value", (_) => "Error!");
286
+ * ```
287
+ *
288
+ * ## Examples
289
+ *
290
+ * ### When `Ok`
291
+ *
292
+ * ```ts
293
+ * const functionThatMightFail = (): Result<string, string> => Ok("foo");
294
+ *
295
+ * const result = functionThatMightFail();
296
+ *
297
+ * const length = result.mapErrOrElse(() => 1, (val) => val.length);
298
+ *
299
+ * console.log(length); // 1
300
+ * ```
301
+ *
302
+ * ### When `Err`
303
+ *
304
+ * ```ts
305
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
306
+ *
307
+ * const result = functionThatMightFail();
308
+ *
309
+ * const length = result.mapOr(() => 1, (val) => val.length);
310
+ *
311
+ * console.log(length); // 4
312
+ * ```
313
+ */
314
+ mapErrOrElse<A>(def: (val: T) => A, fn: (err: E) => A): A {
315
+ return this.match(
316
+ (val) => def(val),
317
+ (err) => fn(err)
318
+ );
319
+ }
320
+
321
+ /** Returns true if result is `Ok`
322
+ *
323
+ * @returns
324
+ *
325
+ * ## Usage
326
+ *
327
+ * ```ts
328
+ * result.isOk();
329
+ * ```
330
+ */
331
+ isOk(): boolean {
332
+ return this.match(
333
+ () => true,
334
+ () => false
335
+ );
336
+ }
337
+
338
+ /** Returns true if result is `Err`
339
+ *
340
+ * @returns
341
+ *
342
+ * ## Usage
343
+ *
344
+ * ```ts
345
+ * result.isErr();
346
+ * ```
347
+ */
348
+ isErr(): boolean {
349
+ return this.match(
350
+ () => false,
351
+ () => true
352
+ );
353
+ }
354
+
355
+ /** Tries to return value if value is `Err` throws generic error message.
356
+ *
357
+ * @returns
358
+ *
359
+ * ## Usage
360
+ *
361
+ * ```ts
362
+ * result.unwrap();
363
+ * ```
364
+ *
365
+ * ## Examples
366
+ *
367
+ * ### When `Ok`
368
+ *
369
+ * ```ts
370
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
371
+ *
372
+ * const result = functionThatMightFail();
373
+ *
374
+ * console.log(result.unwrap()); // "Hello!"
375
+ * ```
376
+ *
377
+ * ### When `Err`
378
+ *
379
+ * ```ts
380
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
381
+ *
382
+ * const result = functionThatMightFail();
383
+ *
384
+ * result.unwrap(); // Error: Attempted to call `.unwrap()` on a non `Ok` value.
385
+ * ```
386
+ */
387
+ unwrap(): T {
388
+ return this.match(
389
+ (val) => val,
390
+ () => {
391
+ throw new Error('Attempted to call `.unwrap()` on a non `Ok` value.');
392
+ }
393
+ );
394
+ }
395
+
396
+ /** Tries to return err if value is `Ok` throws generic error message.
397
+ *
398
+ * @returns
399
+ *
400
+ * ## Usage
401
+ *
402
+ * ```ts
403
+ * result.unwrapErr();
404
+ * ```
405
+ *
406
+ * ## Examples
407
+ *
408
+ * ### When `Ok`
409
+ *
410
+ * ```ts
411
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
412
+ *
413
+ * const result = functionThatMightFail();
414
+ *
415
+ * result.unwrapErr(); // Error: Attempted to call `.unwrapErr()` on a non `Err` value.
416
+ * ```
417
+ *
418
+ * ### When `Err`
419
+ *
420
+ * ```ts
421
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
422
+ *
423
+ * const result = functionThatMightFail();
424
+ *
425
+ * console.log(result.unwrapErr()); // "oops!"
426
+ * ```
427
+ */
428
+ unwrapErr(): E {
429
+ return this.match(
430
+ () => {
431
+ throw new Error('Attempted to call `.unwrapErr()` on a non `Err` value.');
432
+ },
433
+ (err) => err
434
+ );
435
+ }
436
+
437
+ /** Tries to unwrap the value if value is `Err` returns `defaultVal`
438
+ *
439
+ * @param defaultVal Value to be returned if `Err`
440
+ * @returns
441
+ *
442
+ * ## Usage
443
+ *
444
+ * ```ts
445
+ * result.unwrapOr(7);
446
+ * ```
447
+ *
448
+ * ## Examples
449
+ *
450
+ * ### When `Ok`
451
+ *
452
+ * ```ts
453
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
454
+ *
455
+ * const result = functionThatMightFail();
456
+ *
457
+ * console.log(result.unwrapOr("Yellow!")); // "Hello!"
458
+ * ```
459
+ *
460
+ * ### When `Err`
461
+ *
462
+ * ```ts
463
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
464
+ *
465
+ * const result = functionThatMightFail();
466
+ *
467
+ * console.log(result.unwrapOr("Yellow!")); // "Yellow!"
468
+ * ```
469
+ */
470
+ unwrapOr(defaultVal: T): T {
471
+ return this.match(
472
+ (val) => val,
473
+ (_) => defaultVal
474
+ );
475
+ }
476
+
477
+ /** Tries to unwrap the error if vale is `Ok` returns `defaultVal`
478
+ *
479
+ * @param defaultVal
480
+ * @returns
481
+ *
482
+ * ## Usage
483
+ *
484
+ * ```ts
485
+ * result.unwrapErrOr("Error");
486
+ * ```
487
+ *
488
+ * ## Examples
489
+ *
490
+ * ### When `Ok`
491
+ *
492
+ * ```ts
493
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
494
+ *
495
+ * const result = functionThatMightFail();
496
+ *
497
+ * console.log(result.unwrapErrOr("Yellow!")); // "Yellow!"
498
+ * ```
499
+ *
500
+ * ### When `Err`
501
+ *
502
+ * ```ts
503
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
504
+ *
505
+ * const result = functionThatMightFail();
506
+ *
507
+ * console.log(result.unwrapErrOr("Yellow!")); // "oops!"
508
+ * ```
509
+ */
510
+ unwrapErrOr(defaultVal: E): E {
511
+ return this.match(
512
+ () => defaultVal,
513
+ (err) => err
514
+ );
515
+ }
516
+
517
+ /** Tries to return the value if value is `Err` calls `fn`
518
+ *
519
+ * @param fn Function called if `Err`
520
+ *
521
+ * ## Usage
522
+ *
523
+ * ```ts
524
+ * result.unwrapOrElse(() => "Hello!");
525
+ * ```
526
+ *
527
+ * ## Examples
528
+ *
529
+ * ### When `Ok`
530
+ *
531
+ * ```ts
532
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
533
+ *
534
+ * const result = functionThatMightFail();
535
+ *
536
+ * console.log(result.unwrapOrElse(() => "oops!")); // "Hello!"
537
+ * ```
538
+ *
539
+ * ### When `Err`
540
+ *
541
+ * ```ts
542
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
543
+ *
544
+ * const result = functionThatMightFail();
545
+ *
546
+ * console.log(result.unwrapOrElse(() => "Hello!")); // "Hello!"
547
+ * ```
548
+ *
549
+ */
550
+ unwrapOrElse(fn: (err: E) => T): T {
551
+ return this.match(
552
+ (val) => val,
553
+ (err) => fn(err)
554
+ );
555
+ }
556
+
557
+ /** Tries to return the error if value is `Ok` calls `fn`
558
+ *
559
+ * @param fn Function called if `Ok`
560
+ *
561
+ * ## Usage
562
+ *
563
+ * ```ts
564
+ * result.unwrapErrOrElse(() => "Error!");
565
+ * ```
566
+ *
567
+ * ## Examples
568
+ *
569
+ * ### When `Ok`
570
+ *
571
+ * ```ts
572
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
573
+ *
574
+ * const result = functionThatMightFail();
575
+ *
576
+ * console.log(result.unwrapErrOrElse(() => "oops!")); // "oops!"
577
+ * ```
578
+ *
579
+ * ### When `Err`
580
+ *
581
+ * ```ts
582
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
583
+ *
584
+ * const result = functionThatMightFail();
585
+ *
586
+ * console.log(result.unwrapErrOrElse(() => "Hello!")); // "oops!"
587
+ * ```
588
+ *
589
+ */
590
+ unwrapErrOrElse(fn: (val: T) => E): E {
591
+ return this.match(
592
+ (val) => fn(val),
593
+ (err) => err
594
+ );
595
+ }
596
+
597
+ /** Tries to return value if value is `Err` throws custom error message.
598
+ *
599
+ * @param message Message to show when value is `Err`
600
+ * @returns
601
+ *
602
+ * ## Usage
603
+ *
604
+ * ```ts
605
+ * result.expect("Custom message");
606
+ * ```
607
+ *
608
+ * ## Examples
609
+ *
610
+ * ### When `Ok`
611
+ *
612
+ * ```ts
613
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
614
+ *
615
+ * const result = functionThatMightFail();
616
+ *
617
+ * console.log(result.expect("I failed!")); // "Hello!"
618
+ * ```
619
+ *
620
+ * ### When `Err`
621
+ *
622
+ * ```ts
623
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
624
+ *
625
+ * const result = functionThatMightFail();
626
+ *
627
+ * result.expect("I failed!"); // Error: I failed!
628
+ * ```
629
+ */
630
+ expect(message: string): T {
631
+ return this.match(
632
+ (val) => val,
633
+ () => {
634
+ throw new Error(message);
635
+ }
636
+ );
637
+ }
638
+
639
+ /** Tries to return error value if value is `Ok` throws custom error message
640
+ *
641
+ * @param message
642
+ * @returns
643
+ *
644
+ * ## Usage
645
+ *
646
+ * ```ts
647
+ * result.expectErr("Custom message");
648
+ * ```
649
+ *
650
+ * ## Examples
651
+ *
652
+ * ### When `Ok`
653
+ *
654
+ * ```ts
655
+ * const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
656
+ *
657
+ * const result = functionThatMightFail();
658
+ *
659
+ * console.log(result.expectErr("I failed!")); // Error: I failed!
660
+ * ```
661
+ *
662
+ * ### When `Err`
663
+ *
664
+ * ```ts
665
+ * const functionThatMightFail = (): Result<string, string> => Err("oops!");
666
+ *
667
+ * const result = functionThatMightFail();
668
+ *
669
+ * console.log(result.expectErr("I failed!")); // "oops!"
670
+ * ```
671
+ */
672
+ expectErr(message: string): E {
673
+ return this.match(
674
+ () => {
675
+ throw new Error(message);
676
+ },
677
+ (err) => err
678
+ );
679
+ }
680
+ }
681
+
682
+ /** Returns a new `Ok` result type with the provided value
683
+ *
684
+ * @param val Value of the result
685
+ * @returns
686
+ *
687
+ * ## Usage
688
+ *
689
+ * ```ts
690
+ * Ok(true);
691
+ * ```
692
+ *
693
+ * ## Examples
694
+ *
695
+ * ```ts
696
+ * const functionThatCanFail = (condition) => {
697
+ * if (condition) {
698
+ * Ok("Success")
699
+ * }
700
+ *
701
+ * return Err("Failure");
702
+ * }
703
+ * ```
704
+ */
705
+ const Ok = <T>(val: T): Result<T, never> => {
706
+ return new Result<T, never>({ ok: true, val });
707
+ };
708
+
709
+ /** Returns a new `Err` result type with the provided error
710
+ *
711
+ * @param err Error of the result
712
+ * @returns
713
+ *
714
+ * ## Usage
715
+ *
716
+ * ```ts
717
+ * Err("I failed!");
718
+ * ```
719
+ *
720
+ * ## Examples
721
+ *
722
+ * ```ts
723
+ * const functionThatCanFail = (condition) => {
724
+ * if (condition) {
725
+ * Ok("Success")
726
+ * }
727
+ *
728
+ * return Err("Failure");
729
+ * }
730
+ * ```
731
+ */
732
+ const Err = <E>(err: E): Result<never, E> => {
733
+ return new Result<never, E>({ ok: false, err });
734
+ };
735
+
736
+ export { type Result, Ok, Err };