@helios-lang/type-utils
Advanced tools
Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "@helios-lang/type-utils", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Global utility types", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
declare global { | ||
type Only<T, C, R> = T extends C ? R : never | ||
type Option<T> = null | undefined | T | ||
type Either<L, R> = { left: L } | { right: R } // use right to represent a correct value, left for errors | ||
} | ||
@@ -10,1 +10,6 @@ | ||
export function isSome<T>(option: Option<T>): option is T | ||
export function expectLeft<L, R>(either: Either<L, R>, msg?: string): L | ||
export function expectRight<L, R>(either: Either<L, R>, msg?: string): R | ||
export function isLeft<L, R>(either: Either<L, R>): either is { left: L } | ||
export function isRight<L, R>(either: Either<L, R>): either is { right: R } |
@@ -12,3 +12,6 @@ /** | ||
*/ | ||
export function expectSome(option, msg = `unexpected ${option}`) { | ||
export function expectSome( | ||
option, | ||
msg = `expected Option.some, got ${option}` | ||
) { | ||
if (option !== null && option !== undefined) { | ||
@@ -38,1 +41,57 @@ return option | ||
} | ||
/** | ||
* @template L | ||
* @template R | ||
* @param {Either<L, R>} either | ||
* @param {string} msg | ||
* @returns {L} | ||
*/ | ||
export function expectLeft( | ||
either, | ||
msg = `expected Either.left, got ${either}` | ||
) { | ||
if ("left" in either) { | ||
return either.left | ||
} else { | ||
throw new Error(msg) | ||
} | ||
} | ||
/** | ||
* @template L | ||
* @template R | ||
* @param {Either<L, R>} either | ||
* @param {string} msg | ||
* @returns {R} | ||
*/ | ||
export function expectRight( | ||
either, | ||
msg = `expected Either.right, got ${either}` | ||
) { | ||
if ("right" in either) { | ||
return either.right | ||
} else { | ||
throw new Error(msg) | ||
} | ||
} | ||
/** | ||
* @template L | ||
* @template R | ||
* @param {Either<L, R>} either | ||
* @returns {either is {left: L}} | ||
*/ | ||
export function isLeft(either) { | ||
return "left" in either | ||
} | ||
/** | ||
* @template L | ||
* @template R | ||
* @param {Either<L, R>} either | ||
* @returns {either is {right: R}} | ||
*/ | ||
export function isRight(either) { | ||
return "right" in either | ||
} |
5105
116