aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r--src/gleam_stdlib.js19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index 7ecbe11..b743c03 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -518,24 +518,27 @@ 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_element(data, index) {
- let error = (size) =>
- decoder_error(
- `Tuple of at least ${size} element${size == 1 ? "" : "s"}`,
- data
- );
- if (!Array.isArray(data)) return error(index < 0 ? 1 : index + 1);
+ 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 error(index + 1);
+ return decode_tuple_error(index + 1, data);
}
} else {
if (Math.abs(index) <= data.length) {
return new Ok(data[data.length + index]);
} else {
- return error(Math.abs(index));
+ return decode_tuple_error(Math.abs(index), data);
}
}
}