Skip to content

Commit fdc85ab

Browse files
changes
1 parent d6be891 commit fdc85ab

File tree

8 files changed

+468
-172
lines changed

8 files changed

+468
-172
lines changed

docs/examples.ts

+35-67
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,35 @@
1-
import { Result } from '@weis-guys/result'
2-
3-
4-
const happy = Result.ok( 'good value' )
5-
console.log( happy )
6-
// { success: true, value: "good value" }
7-
8-
9-
const sad = Result.err( 'some kind of error' )
10-
console.log( sad )
11-
// { success: false, error: "some kind of error" }
12-
13-
14-
/**
15-
* this function could return a value or an error
16-
*/
17-
function someFn () {
18-
return Math.random() > 0.5 ? happy : sad
19-
}
20-
21-
22-
const result = someFn()
23-
24-
Result.match( result )( {
25-
ok: value => console.log( value ), // do something with the value
26-
err: error => console.error( error ), // do something with the error
27-
} )
28-
29-
const valueOrError = Result.match( result )( {
30-
// optionally perform a typesafe transformation to the value before returning it
31-
ok: value => value,
32-
33-
// optionally perform a typesafe transformation to the error before returning it
34-
err: error => error,
35-
} )
36-
37-
console.log( 'valueOrError:', valueOrError )
38-
// valueOrError: "good value" | "some kind of error"
39-
40-
type ValueOrError = typeof valueOrError
41-
// type ValueOrError = "good value" | "some kind of error"
42-
43-
44-
const justValue = Result.match( result )( { ok: value => value } )
45-
console.log( 'justValue:', justValue )
46-
// justValue: "good value" | undefined
47-
48-
type JustValue = typeof justValue
49-
// type JustValue = "good value" | undefined
50-
51-
52-
const justError = Result.match( result )( { err: error => error } )
53-
console.log( 'justError:', justError )
54-
// justError: "some kind of error" | undefined
55-
56-
type JustError = typeof justError
57-
// type JustError = "some kind of error" | undefined
58-
59-
60-
// you can also handle both cases with a normal if/else statement
61-
if ( result.success ) {
62-
console.log( 'result.value:', result.value )
63-
// result.value: "good value"
64-
} else {
65-
console.log( 'result.error', result.error )
66-
// result.error: "some kind of error"
67-
}
1+
// import { Result } from '@weis-guys/result'
2+
3+
// const ok = Result.ok( 'good value' )
4+
// // { type: 'ok', value: 'good value' }
5+
6+
// const warning = Result.warning( 'some kind of warning' )
7+
// // { type: 'warning', value: 'some kind of warning' }
8+
9+
// const error = Result.error( 'some kind of error' )
10+
// // { type: 'error', value: 'some kind of error' }
11+
12+
// /**
13+
// * this function could return a value or an error
14+
// */
15+
// function someFn () {
16+
// const items = [ ok, warning, error ]
17+
// return items[ Math.floor( Math.random() * items.length ) ]
18+
// }
19+
20+
// const result = someFn()
21+
22+
// switch ( result.type ) {
23+
// case 'ok':
24+
// console.log( result.value )
25+
// // 'good value'
26+
// break
27+
// case 'warning':
28+
// console.warn( result.value )
29+
// // 'some kind of warning'
30+
// break
31+
// case 'error':
32+
// console.error( result.value )
33+
// // 'some kind of error'
34+
// break
35+
// }

0 commit comments

Comments
 (0)