diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-09 20:30:04 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-09 20:30:04 +0100 |
commit | 70136ec67863b0c97b9845edc13c6841eee033fc (patch) | |
tree | 5b0d429aa415ad8321a2d0bdea01b6184f638f56 /src | |
parent | 749ac8290d1e3d09e2287056f618d6df91f4e01b (diff) | |
download | gleam_stdlib-70136ec67863b0c97b9845edc13c6841eee033fc.tar.gz gleam_stdlib-70136ec67863b0c97b9845edc13c6841eee033fc.zip |
JS dynamic bit string
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 33 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 9 |
2 files changed, 30 insertions, 12 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 86d1b6e..903341a 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -60,22 +60,31 @@ if javascript { "../gleam_stdlib.js" "identity" } +/// Checks to see whether a Dynamic value is a bit_string, and return the bit_string if +/// it is. +/// +/// ## Examples +/// +/// > bit_string(from("Hello")) == bit_string.from_string("Hello") +/// True +/// +/// > bit_string(from(123)) +/// Error(DecodeError(expected: "BitString", found: "Int")) +/// +pub fn bit_string(from data: Dynamic) -> Result(BitString, DecodeError) { + decode_bit_string(data) +} + if erlang { - /// Checks to see whether a Dynamic value is a bit_string, and return the bit_string if - /// it is. - /// - /// ## Examples - /// - /// > bit_string(from("Hello")) == bit_string.from_string("Hello") - /// True - /// - /// > bit_string(from(123)) - /// Error(DecodeError(expected: "BitString", found: "Int")) - /// - pub external fn bit_string(from: Dynamic) -> Result(BitString, DecodeError) = + external fn decode_bit_string(Dynamic) -> Result(BitString, DecodeError) = "gleam_stdlib" "decode_bit_string" } +if javascript { + external fn decode_bit_string(Dynamic) -> Result(BitString, DecodeError) = + "../gleam_stdlib.js" "decode_bit_string" +} + /// Checks to see whether a Dynamic value is a string, and return the string if /// it is. /// diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index b8feecf..0982f62 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -476,6 +476,8 @@ function classify_dynamic(data) { return "List"; } else if (Number.isInteger(data)) { return "Int"; + } else if (BitString.isBitString(data)) { + return "BitString"; } else if (typeof data === "number") { return "Float"; } else { @@ -501,6 +503,13 @@ export function decode_int(data) { export function decode_float(data) { return typeof data === "number" ? new Ok(data) : decoder_error("Float", data); } + export function decode_bool(data) { return typeof data === "boolean" ? new Ok(data) : decoder_error("Bool", data); } + +export function decode_bit_string(data) { + return BitString.isBitString(data) + ? new Ok(data) + : decoder_error("BitString", data); +} |