Skip to content

Commit 9cc2bf7

Browse files
inoaslpil
authored andcommitted
improve comment, remove double function call, move call to where it is first require
1 parent 48ac82b commit 9cc2bf7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/gleam_stdlib_decode_ffi.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { classify } from "./gleam/dynamic.mjs";
55
import { DecodeError } from "./gleam/dynamic/decode.mjs";
66

77
export function index(data, key) {
8-
const int = Number.isInteger(key);
9-
108
// Dictionaries and dictionary-like objects can be indexed
119
if (data instanceof Dict || data instanceof WeakMap || data instanceof Map) {
1210
const token = {};
@@ -15,8 +13,10 @@ export function index(data, key) {
1513
return new Ok(new Some(entry));
1614
}
1715

18-
// The first elements of lists can be indexed
19-
if (Number.isInteger(key) && key < 8 && data instanceof List) {
16+
const key_is_int = Number.isInteger(key);
17+
18+
// Only elements 0-7 of lists can be indexed
19+
if (key_is_int && key < 8 && data instanceof List) {
2020
let i = 0;
2121
for (const value of data) {
2222
if (i === key) return new Ok(new Some(value));
@@ -27,15 +27,15 @@ export function index(data, key) {
2727

2828
// Arrays and objects can be indexed
2929
if (
30-
(int && Array.isArray(data)) ||
30+
(key_is_int && Array.isArray(data)) ||
3131
(data && typeof data === "object") ||
3232
(data && Object.getPrototypeOf(data) === Object.prototype)
3333
) {
3434
if (key in data) return new Ok(new Some(data[key]));
3535
return new Ok(new None());
3636
}
3737

38-
return new Error(int ? "Indexable" : "Dict");
38+
return new Error(key_is_int ? "Indexable" : "Dict");
3939
}
4040

4141
export function list(data, decode, pushPath, index, emptyList) {

0 commit comments

Comments
 (0)