diff options
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r-- | src/gleam_stdlib.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index bb63cbb..75b0882 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -12,6 +12,7 @@ import { CompileError as RegexCompileError, Match as RegexMatch, } from "./gleam/regex.js"; +import { DecodeError } from "./gleam/dynamic.js"; import { Some, None } from "./gleam/option.js"; const HASHCODE_CACHE = new WeakMap(); @@ -467,3 +468,25 @@ export function decode64(sBase64) { return new Ok(new BitString(taBytes)); } + +function classify_dynamic(data) { + if (typeof data === "string") { + return "String"; + } else if (List.isList(data)) { + return "List"; + } else if (Number.isInteger(data)) { + return "Int"; + } else { + return typeof data; + } +} + +function decoder_error(expected, got) { + return new Error(new DecodeError(expected, classify_dynamic(got))); +} + +export function decode_string(data) { + return typeof data === "string" + ? new Ok(data) + : decoder_error("String", data); +} |