I need to exercise some code in data mapping functions. For this, I need a proxy that returns an iterable array with one element (itself; an iterable array) when any property is requested. This will activate all loops and property checks in the data mapper layer (not shown here).
Something like this:
p = new Proxy([], {
get(obj, prop) {
if(prop === 'length') return obj.length
if(prop === 'push') return obj.push
if(p.length === 0) p.push(p)
return p
}}
)
This starts off well:
> p.any.length
1
but fails here:
> for(let v of p.any){console.log(v)}
Uncaught TypeError: p.any is not iterable
I need the loop to iterate and console.log one element:
> p.any
Proxy [
<ref *1> [ Proxy [ [Circular *1], [Object] ] ],
{ get: [Function: get] }
]
Some proxy docs and guides cover the in and object but do not cover the of and array.
[]and not an object{}. I'm not sure what to do given those links. For the use-case: during unit testing, instead of sending live data in I send a proxy in to get an output object (convert to). It is just a bunch of functions that take an object and return an object (tree).