-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdc85ab
commit f20c67a
Showing
8 changed files
with
171 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,44 @@ | ||
// import { Result } from '@weis-guys/result' | ||
|
||
// const ok = Result.ok( 'good value' ) | ||
// // { type: 'ok', value: 'good value' } | ||
|
||
// const warning = Result.warning( 'some kind of warning' ) | ||
// // { type: 'warning', value: 'some kind of warning' } | ||
|
||
// const error = Result.error( 'some kind of error' ) | ||
// // { type: 'error', value: 'some kind of error' } | ||
|
||
// /** | ||
// * this function could return a value or an error | ||
// */ | ||
// function someFn () { | ||
// const items = [ ok, warning, error ] | ||
// return items[ Math.floor( Math.random() * items.length ) ] | ||
// } | ||
|
||
// const result = someFn() | ||
|
||
// switch ( result.type ) { | ||
// case 'ok': | ||
// console.log( result.value ) | ||
// // 'good value' | ||
// break | ||
// case 'warning': | ||
// console.warn( result.value ) | ||
// // 'some kind of warning' | ||
// break | ||
// case 'error': | ||
// console.error( result.value ) | ||
// // 'some kind of error' | ||
// break | ||
// } | ||
import Result from '@weis-guys/result' | ||
|
||
const okResult = Result.ok( 'good value' ) | ||
// { success: true, value: "good value" } | ||
|
||
const warningResult = Result.okWithWarning( 'good value', 'some kind of warning' ) | ||
// { success: true, value: "good value", warning: "some kind of warning" } | ||
|
||
const errorResult = Result.error( 'some kind of error' ) | ||
// { success: false, error: "some kind of error" } | ||
|
||
/** | ||
* this function could return a value, an error, or a value with a warning | ||
*/ | ||
function someFn () { | ||
const items = [ okResult, warningResult, errorResult ] | ||
return items[ Math.floor( Math.random() * items.length ) ] | ||
} | ||
|
||
const result = someFn() | ||
console.log( { result } ) | ||
|
||
if ( result.success ) { | ||
console.log( result.value ) | ||
// 'good value' | ||
|
||
result.warning && console.warn( result.warning ) | ||
// 'some kind of warning' | ||
} else { | ||
console.error( result.error ) | ||
// 'some kind of error' | ||
} | ||
|
||
const value = Result.getValue( result ) | ||
console.log( value ) | ||
// 'good value' | undefined | ||
|
||
const warning = Result.getWarning( result ) | ||
console.log( warning ) | ||
// 'some kind of warning' | undefined | ||
|
||
const error = Result.getError( result ) | ||
console.log( error ) | ||
// 'some kind of error' | undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export module Result { | ||
export type Ok<Value, Warning = undefined> = { | ||
success: true | ||
/** | ||
* an intended value | ||
*/ | ||
value: Value | ||
/** | ||
* a non-fatal error | ||
*/ | ||
warning?: Warning | ||
} | ||
|
||
export type Err<Error> = { | ||
success: false | ||
/** | ||
* a fatal error | ||
*/ | ||
error: Error | ||
} | ||
|
||
export type Any<Value = any, Error = any, Warning = any> = | ||
| Ok<Value, Warning> | ||
| Err<Error> | ||
|
||
/** | ||
* return a successful result | ||
*/ | ||
export function ok<const Value> ( x: Value ): Ok<Value> { | ||
return { success: true, value: x } | ||
} | ||
|
||
/** | ||
* return a successful result with a warning | ||
*/ | ||
export function okWithWarning<const Value, const Warning> ( x: Value, warning: Warning ): Ok<Value, Warning> { | ||
return { ...ok( x ), warning } | ||
} | ||
|
||
/** | ||
* return a failure result | ||
*/ | ||
export function error<const Error> ( x: Error ): Err<Error> { | ||
return { success: false, error: x } | ||
} | ||
|
||
/** | ||
* return only the value if the result is a success | ||
*/ | ||
export function getValue<Result extends Any> ( result: Result ) { | ||
return result.success ? result.value : undefined | ||
} | ||
|
||
/** | ||
* return only the warning if the result is a success | ||
*/ | ||
export function getWarning<Result extends Any> ( result: Result ) { | ||
return result.success ? result.warning : undefined | ||
} | ||
|
||
/** | ||
* return only the error if the result is a failure | ||
*/ | ||
export function getError<Result extends Any> ( result: Result ) { | ||
return result.success ? undefined : result.error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,37 @@ | ||
import { expect, test } from 'bun:test' | ||
import { Result } from './index' | ||
import Result from './index' | ||
|
||
const ok = Result.ok( 'good value' ) | ||
// { type: 'ok', value: 'good value' } | ||
const okResult = Result.ok( 'good value' ) | ||
// { success: true, value: "good value" } | ||
|
||
const warning = Result.warning( 'some kind of warning' ) | ||
// { type: 'warning', value: 'some kind of warning' } | ||
const warningResult = Result.okWithWarning( 'good value', 'some kind of warning' ) | ||
// { success: true, value: "good value", warning: "some kind of warning" } | ||
|
||
const error = Result.error( 'some kind of error' ) | ||
// { type: 'error', value: 'some kind of error' } | ||
const errorResult = Result.error( 'some kind of error' ) | ||
// { success: false, error: "some kind of error" } | ||
|
||
/** | ||
* this function could return a value or an error | ||
*/ | ||
function someFn () { | ||
const items = [ ok, warning, error ] | ||
const items = [ okResult, warningResult, errorResult ] | ||
return items[ Math.floor( Math.random() * items.length ) ] | ||
} | ||
|
||
const result = someFn() | ||
console.log( { result } ) | ||
|
||
const matched = Result.match( result )( { | ||
ok: value => 42, | ||
warning: warning => 'warning', | ||
error: error => 'error', | ||
test( 'Result.getValue', () => { | ||
expect( Result.getValue( result ) ) | ||
.toBe( result.success ? result.value : undefined ) | ||
} ) | ||
console.log( { matched } ) | ||
|
||
const matched2 = Result.match( result )( {} ) | ||
|
||
// switch ( result.type ) { | ||
// case 'ok': | ||
// console.log( result.value ) | ||
// // 'good value' | ||
// break | ||
// case 'warning': | ||
// console.warn( result.value ) | ||
// // 'some kind of warning' | ||
// break | ||
// case 'error': | ||
// console.error( result.value ) | ||
// // 'some kind of error' | ||
// break | ||
// } | ||
|
||
|
||
// test( 'result should make Result Type', () => { | ||
// expect( result ).toMatchObject( { | ||
// type: result.type, | ||
// value: result.value, | ||
// } ) | ||
// } ) | ||
|
||
// test( 'Result.match should allow ok and err callbacks', () => { | ||
// const foo = Result.match( result )( { | ||
// ok: value => value, | ||
// err: error => error, | ||
// } ) | ||
// if ( result.success ) expect( foo ).toBe( result.value ) | ||
// else expect( foo ).toBe( result.error ) | ||
// } ) | ||
|
||
// test( 'Result.match should allow just ok callback', () => { | ||
// const foo = Result.match( result )( { | ||
// ok: value => value, | ||
// } ) | ||
// if ( result.success ) expect( foo ).toBe( result.value ) | ||
// } ) | ||
test( 'Result.getWarning', () => { | ||
expect( Result.getWarning( result ) ) | ||
.toBe( result.success ? result.warning : undefined ) | ||
} ) | ||
|
||
// test( 'Result.match should allow just err callback', () => { | ||
// const foo = Result.match( result )( { | ||
// err: error => error, | ||
// } ) | ||
// if ( !result.success ) expect( foo ).toBe( result.error ) | ||
// } ) | ||
test( 'Result.getError', () => { | ||
expect( Result.getError( result ) ) | ||
.toBe( result.success ? undefined : result.error ) | ||
} ) |
Oops, something went wrong.