@@ -5,8 +5,6 @@ import { classify } from "./gleam/dynamic.mjs";
5
5
import { DecodeError } from "./gleam/dynamic/decode.mjs" ;
6
6
7
7
export function index ( data , key ) {
8
- const int = Number . isInteger ( key ) ;
9
-
10
8
// Dictionaries and dictionary-like objects can be indexed
11
9
if ( data instanceof Dict || data instanceof WeakMap || data instanceof Map ) {
12
10
const token = { } ;
@@ -15,8 +13,10 @@ export function index(data, key) {
15
13
return new Ok ( new Some ( entry ) ) ;
16
14
}
17
15
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 ) {
20
20
let i = 0 ;
21
21
for ( const value of data ) {
22
22
if ( i === key ) return new Ok ( new Some ( value ) ) ;
@@ -27,15 +27,15 @@ export function index(data, key) {
27
27
28
28
// Arrays and objects can be indexed
29
29
if (
30
- ( int && Array . isArray ( data ) ) ||
30
+ ( key_is_int && Array . isArray ( data ) ) ||
31
31
( data && typeof data === "object" ) ||
32
32
( data && Object . getPrototypeOf ( data ) === Object . prototype )
33
33
) {
34
34
if ( key in data ) return new Ok ( new Some ( data [ key ] ) ) ;
35
35
return new Ok ( new None ( ) ) ;
36
36
}
37
37
38
- return new Error ( int ? "Indexable" : "Dict" ) ;
38
+ return new Error ( key_is_int ? "Indexable" : "Dict" ) ;
39
39
}
40
40
41
41
export function list ( data , decode , pushPath , index , emptyList ) {
0 commit comments