@helios-lang/type-utils
Advanced tools
Comparing version 0.1.11 to 0.1.13
{ | ||
"name": "@helios-lang/type-utils", | ||
"version": "0.1.11", | ||
"version": "0.1.13", | ||
"description": "Global utility types", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -21,2 +21,11 @@ declare global { | ||
type EnumFromAsserts<A extends Record<string, Assert>, K> = K extends string | ||
? { [K_ in K]: A[K] extends Assert<any, infer O> ? O : never } | ||
: never | ||
export function expectEnum<A extends Record<string, Assert>>( | ||
input: unknown, | ||
assertT: A, | ||
msg: string | undefined = undefined | ||
): asserts input is EnumFromAsserts<A, keyof A> | ||
/** | ||
@@ -23,0 +32,0 @@ * Must be imported explicitly in order to override default JSON namespace |
108
src/index.js
export { JSONSafe as JSON } from "./json.js" | ||
/** | ||
* @type {null} | ||
*/ | ||
export const None = null | ||
/** | ||
* @template T | ||
* @param {Option<T>[]} list | ||
* @returns {Option<T[]>} | ||
*/ | ||
export function allOrNone(list) { | ||
return list.every(isSome) ? /** @type {any} */ (list) : None | ||
} | ||
/** | ||
* @template T | ||
* @param {Option<T>} option | ||
* @param {string} msg | ||
* @returns {T} | ||
*/ | ||
export function expectSome( | ||
option, | ||
msg = `expected Option.some, got ${option}` | ||
) { | ||
if (option !== null && option !== undefined) { | ||
return option | ||
} else { | ||
throw new Error(msg) | ||
} | ||
} | ||
/** | ||
* @template T | ||
* @param {Option<T>} option | ||
* @returns {opt is T} | ||
*/ | ||
export function isSome(option) { | ||
return option !== null && option !== undefined | ||
} | ||
/** | ||
* @template T | ||
* @param {Option<T>} option | ||
* @returns {opt is (null | undefined)} | ||
*/ | ||
export function isNone(option) { | ||
return option === null || option === undefined | ||
} | ||
/** | ||
* @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 ${JSON.stringify(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 | ||
} | ||
export * from "./either.js" | ||
export { expectEnum } from "./enum.js" | ||
export * from "./option.js" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7658
10
199
1