@@ -5,8 +5,6 @@ import { classify } from "./gleam/dynamic.mjs";
55import { DecodeError } from "./gleam/dynamic/decode.mjs" ;
66
77export 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
4141export function list ( data , decode , pushPath , index , emptyList ) {
0 commit comments