diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-10 20:32:50 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-10 20:32:50 +0100 |
commit | a6b75f614c97f7cf2bede89b5769bfcd13c31571 (patch) | |
tree | c6db67376e64f69759051488134f7c8dc8267d65 /src/gleam_stdlib.js | |
parent | 72889a32caa561ffe083a7c921f856c23c0ee5f6 (diff) | |
download | gleam_stdlib-a6b75f614c97f7cf2bede89b5769bfcd13c31571.tar.gz gleam_stdlib-a6b75f614c97f7cf2bede89b5769bfcd13c31571.zip |
Optional
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r-- | src/gleam_stdlib.js | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 4c52daf..37f071a 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -540,3 +540,15 @@ export function decode_result(data) { export function decode_map(data) { return data instanceof Map ? new Ok(data) : decoder_error("Map", data); } + +export function decode_option(data, decoder) { + if (data === null || data === undefined || data instanceof None) + return new Ok(new None()); + if (data instanceof Some) data = data[0]; + let result = decoder(data); + if (result.isOk()) { + return new Ok(new Some(result[0])); + } else { + return result; + } +} |