diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-09 19:44:35 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-09 19:44:35 +0100 |
commit | c45c1038f8b12acf7d2010eba070ad3dc54765a2 (patch) | |
tree | 39ceed83733a3c54ba4048122a5b4d0b8ce823ed /src/gleam_stdlib.js | |
parent | c5d415fdc206ae3403b2992c9a3e395188b45280 (diff) | |
download | gleam_stdlib-c45c1038f8b12acf7d2010eba070ad3dc54765a2.tar.gz gleam_stdlib-c45c1038f8b12acf7d2010eba070ad3dc54765a2.zip |
JS dynamic string
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); +} |