-
Notifications
You must be signed in to change notification settings - Fork 105
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
2ee6236
commit a2de818
Showing
8 changed files
with
419 additions
and
49 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 |
---|---|---|
|
@@ -6,5 +6,6 @@ module.exports = { | |
transform: { | ||
'^.+\\.ts$': 'ts-jest' | ||
}, | ||
verbose: true | ||
verbose: true, | ||
testTimeout: 9000 | ||
} |
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,48 @@ | ||
export interface Succeeded<T> { | ||
readonly succeeded: true | ||
readonly result: T | ||
} | ||
|
||
export interface Failed { | ||
readonly succeeded: false | ||
readonly error: string | ||
} | ||
|
||
export type Errorable<T> = Succeeded<T> | Failed | ||
|
||
export function succeeded<T>(e: Errorable<T>): e is Succeeded<T> { | ||
return e.succeeded | ||
} | ||
|
||
export function failed<T>(e: Errorable<T>): e is Failed { | ||
return !e.succeeded | ||
} | ||
|
||
export function map<T, U>(e: Errorable<T>, fn: (t: T) => U): Errorable<U> { | ||
if (failed(e)) { | ||
return {succeeded: false, error: e.error} | ||
} | ||
return {succeeded: true, result: fn(e.result)} | ||
} | ||
|
||
export function combine<T>(es: Errorable<T>[]): Errorable<T[]> { | ||
const failures = es.filter(failed) | ||
if (failures.length > 0) { | ||
return { | ||
succeeded: false, | ||
error: failures.map((f) => f.error).join('\n') | ||
} | ||
} | ||
|
||
return { | ||
succeeded: true, | ||
result: es.map((e) => (e as Succeeded<T>).result) | ||
} | ||
} | ||
|
||
export function getErrorMessage(error: unknown) { | ||
if (error instanceof Error) { | ||
return error.message | ||
} | ||
return String(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
Oops, something went wrong.