aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-09-09 22:44:35 +0100
committerLouis Pilfold <louis@lpil.uk>2021-09-09 22:44:35 +0100
commitba5f9b8c97ff95eb03304ff42628c5af77d56d95 (patch)
treede165c53b3b3003e3bc2d1bf8aee8a6f5f64f25c /src/gleam_stdlib.js
parenta595d4be50fe7e4e5a946bf5ed5878fa7bd42b64 (diff)
downloadgleam_stdlib-ba5f9b8c97ff95eb03304ff42628c5af77d56d95.tar.gz
gleam_stdlib-ba5f9b8c97ff95eb03304ff42628c5af77d56d95.zip
Move tuple decoding into Gleam
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r--src/gleam_stdlib.js29
1 files changed, 7 insertions, 22 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index b743c03..47801ce 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -469,7 +469,7 @@ export function decode64(sBase64) {
return new Ok(new BitString(taBytes));
}
-function classify_dynamic(data) {
+export function classify_dynamic(data) {
if (typeof data === "string") {
return "String";
} else if (List.isList(data)) {
@@ -518,27 +518,12 @@ export function decode_bit_string(data) {
: decoder_error("BitString", data);
}
-function decode_tuple_error(size, data) {
- return decoder_error(
- `Tuple of at least ${size} element${size == 1 ? "" : "s"}`,
- data
- );
+export function decode_tuple(data) {
+ return Array.isArray(data) ? new Ok(data) : decoder_error("Tuple", data);
}
-export function decode_element(data, index) {
- if (!Array.isArray(data))
- return decode_tuple_error(index < 0 ? Math.abs(index) : index + 1, data);
- if (index >= 0) {
- if (index < data.length) {
- return new Ok(data[index]);
- } else {
- return decode_tuple_error(index + 1, data);
- }
- } else {
- if (Math.abs(index) <= data.length) {
- return new Ok(data[data.length + index]);
- } else {
- return decode_tuple_error(Math.abs(index), data);
- }
- }
+export function tuple_get(data, index) {
+ return index >= 0 && data.length > index
+ ? new Ok(data[index])
+ : new Error(Nil);
}