forked from ReactiveDB/core
-
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.
fix(utils/assert): evaluate exception lazily
...and utilize typescript 3.0 for accurate `rest parameter` typing.
- Loading branch information
Showing
8 changed files
with
62 additions
and
33 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
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
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,24 +1,44 @@ | ||
import { Truthy } from './truthy' | ||
|
||
export function assert(condition: boolean, error: Error | string): void { | ||
truthyOrThrow(condition, error) | ||
type FailureHandler<U extends any[]> = (...args: U) => Error | ||
|
||
export function assert(condition: boolean, failureMsg: string): void | ||
export function assert<U extends any[]>( | ||
condition: boolean, | ||
failure: FailureHandler<U>, | ||
...failureArgs: U | ||
): void | ||
export function assert<U extends any[]>( | ||
condition: boolean, | ||
failure: FailureHandler<U> | string, | ||
...failureArgs: U | ||
): void { | ||
truthyOrThrow(condition, failure, ...failureArgs) | ||
} | ||
|
||
export function assertValue<T>( | ||
export function assertValue<T>(value: T, falsyValueMsg: string): Truthy<T> | never | ||
export function assertValue<T, U extends any[]>( | ||
value: T, | ||
failure: FailureHandler<U>, | ||
...failureArgs: U | ||
): Truthy<T> | never | ||
export function assertValue<T, U extends any[]>( | ||
value: T, | ||
error: Error | string | ||
failure: FailureHandler<U> | string, | ||
...failureArgs: U | ||
): Truthy<T> | never { | ||
return truthyOrThrow(value, error) | ||
return truthyOrThrow(value, failure, ...failureArgs) | ||
} | ||
|
||
function truthyOrThrow<T>(x: T, error: Error | string): Truthy<T> | never { | ||
function truthyOrThrow<T, U extends any[]>( | ||
x: T, | ||
failure: FailureHandler<U> | string, | ||
...failureArgs: U | ||
): Truthy<T> | never { | ||
if (x) { | ||
return x as Truthy<T> | ||
} | ||
|
||
if (error instanceof Error) { | ||
throw error | ||
} else { | ||
throw new Error(error) | ||
} | ||
const error = typeof failure === 'string' ? new Error(failure) : failure(...failureArgs) | ||
throw 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
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