Fork me on GitHub
#squint
<
2022-08-31
>
Prabhjot Singh06:08:03

In Javascript there is a concept of ArrayLike

Array.from({"0": "a", "1": "b"})
// []

Array.from({"0": "a", "1": "b", "length": 2})
// ['a', 'b']

Prabhjot Singh06:08:04

Is that something which makes sense for clava

Prabhjot Singh06:08:49

I mean, do we want to recognise array like instead of treating them as maps

Prabhjot Singh07:08:28

It was useful for things like document.getElementsByTagName("li") and arguments inside a function. But now a days they all support Symbol.iterator so they are not a problem.

borkdude07:08:56

Can you give an example of what currently doesn't work in Clava?

Prabhjot Singh08:08:44

for example

(vec {"0" 1 "length" 1})
=> [["0 1] ["length" 1]]

(js/Array.from {"0" 1 "length" 1})
=> [1]

Prabhjot Singh08:08:42

clava handles js objects as maps so the above is as expected. I am only asking whether it would make sense to treat ArrayLike as iterable. The workaround is also very simple. Just use Array.from

borkdude08:08:51

What makes something an ArrayLike? The presence of the "length" field?

Prabhjot Singh09:08:14

yes, and numbers as keys

borkdude09:08:24

is there a way to distinguish array-like objects from other objects via a JS API?

Prabhjot Singh09:08:30

I beleive there is no official spec of ArrayLike. In the past there were certain objects which are not exactly array but have arraylike properties. For example HTMLCollection . It has a length property but it is not an array. But now I noticed that it has [Symbol.iterator] on it so it still works with clava as expected.

borkdude09:08:24

hm ok. if there is not a clean API for detecting Array-like I prefer to handle it manually else it would be a performance cost to inspect all the keys, etc

Prabhjot Singh09:08:38

yes I agree. Also Array.from handles it so it is fine.

🙌 1