From ba5f9b8c97ff95eb03304ff42628c5af77d56d95 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 9 Sep 2021 22:44:35 +0100 Subject: Move tuple decoding into Gleam --- src/gleam_stdlib.js | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'src/gleam_stdlib.js') 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); } -- cgit v1.2.3