@helios-lang/type-utils
Advanced tools
Comparing version 0.1.14 to 0.1.15
{ | ||
"name": "@helios-lang/type-utils", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "Global utility types", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -48,1 +48,7 @@ declare global { | ||
} | ||
export function isJsonSafe(input: unknown): input is JsonSafe | ||
export function expectJsonSafe( | ||
input: unknown, | ||
msg?: string | undefined | ||
): asserts input is JsonSafe |
@@ -1,4 +0,4 @@ | ||
export { JSONSafe as JSON } from "./json.js" | ||
export { JSONSafe as JSON, isJsonSafe, expectJsonSafe } from "./json.js" | ||
export * from "./either.js" | ||
export { expectEnum } from "./enum.js" | ||
export * from "./option.js" |
@@ -5,1 +5,36 @@ export const JSONSafe = { | ||
} | ||
/** | ||
* @param {unknown} input | ||
* @returns {input is JsonSafe} | ||
*/ | ||
export function isJsonSafe(input) { | ||
if (typeof input == "number") { | ||
return true | ||
} else if (typeof input == "string") { | ||
return true | ||
} else if (typeof input == "boolean") { | ||
return true | ||
} else if (input === null) { | ||
return true | ||
} else if (Array.isArray(input)) { | ||
return input.every(isJsonSafe) | ||
} else if (typeof input == "object") { | ||
return Object.keys(input).every((key) => isJsonSafe(input[key])) | ||
} else { | ||
return false | ||
} | ||
} | ||
/** | ||
* @param {unknown} input | ||
* @param {string | undefined} msg | ||
* @returns {asserts input is JsonSafe} | ||
*/ | ||
export function expectJsonSafe(input, msg = undefined) { | ||
if (isJsonSafe(input)) { | ||
return | ||
} else { | ||
throw new TypeError(msg ?? "invalid JsonSafe value") | ||
} | ||
} |
8824
241