File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ export declare class Result < T = unknown , E = Error > {
2+ readonly value ?: T ;
3+ readonly error ?: E ;
4+
5+ constructor ( params : { value ?: T ; error ?: E } ) ;
6+
7+ static create < T , E = Error > ( input : T | E ) : Result < T , E > ;
8+ static fromValue < T > ( value : T ) : Result < T , never > ;
9+ static fromError < E > ( error : E ) : Result < never , E > ;
10+
11+ isSuccess ( ) : boolean ;
12+ isError ( ) : boolean ;
13+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ class Result {
4+ constructor ( { value, error } ) {
5+ this . value = value ;
6+ this . error = error ;
7+ }
8+
9+ static create ( input ) {
10+ if ( input instanceof globalThis . Error ) {
11+ return new Result ( { value : undefined , error : input } ) ;
12+ }
13+ return new Result ( { value : input , error : undefined } ) ;
14+ }
15+
16+ static fromValue ( value ) {
17+ return new Result ( { value, error : undefined } ) ;
18+ }
19+
20+ static fromError ( error ) {
21+ return new Result ( { value : undefined , error } ) ;
22+ }
23+
24+ isSuccess ( ) {
25+ return this . error === undefined ;
26+ }
27+
28+ isError ( ) {
29+ return this . error !== undefined ;
30+ }
31+ }
32+
33+ // Usage
34+
35+ const success = Result . create ( 'Successfully received data' ) ;
36+ const failure = Result . create ( new Error ( 'Network error' ) ) ;
37+
38+ if ( success . isSuccess ( ) ) {
39+ console . log ( 'Success:' , success . value ) ;
40+ }
41+
42+ if ( failure . isError ( ) ) {
43+ console . log ( 'Failure:' , failure . error . message ) ;
44+ }
Original file line number Diff line number Diff line change 1+ export declare class Maybe < T = unknown > {
2+ constructor ( value ?: T ) ;
3+
4+ get value ( ) : T | undefined ;
5+
6+ isEmpty ( ) : boolean ;
7+
8+ match < R > (
9+ someFn : ( value : T ) => R ,
10+ noneFn : ( ) => R
11+ ) : R ;
12+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ class Maybe {
4+ #value;
5+
6+ constructor ( value ) {
7+ this . #value = value ;
8+ }
9+
10+ get value ( ) {
11+ return this . #value;
12+ }
13+
14+ isEmpty ( ) {
15+ return this . #value === undefined || this . #value === null ;
16+ }
17+
18+ match ( someFn , noneFn ) {
19+ return this . isEmpty ( ) ? noneFn ( ) : someFn ( this . #value) ;
20+ }
21+ }
22+
23+ // Usage
24+
25+ const some = new Maybe ( 42 ) ;
26+ const none = new Maybe ( ) ;
27+
28+ console . log ( some ) ;
29+ console . log ( none ) ;
30+
31+ console . log ( some . isEmpty ( ) ) ;
32+ console . log ( none . isEmpty ( ) ) ;
33+
34+ console . log ( some . value ) ;
35+ console . log ( none . value ) ;
36+
37+ const res1 = some . match (
38+ ( v ) => `Got value ${ v } ` ,
39+ ( ) => 'No value' ,
40+ ) ;
41+ console . log ( res1 ) ;
42+
43+ const res2 = none . match (
44+ ( v ) => `Got value ${ v } ` ,
45+ ( ) => 'No value' ,
46+ ) ;
47+ console . log ( res2 ) ;
You can’t perform that action at this time.
0 commit comments