(Search didn't turn up anything useful)
Buble doesn't currently support Array.fill
as a feature, I'm curious as to the rationale behind this? I recently ran into an issue where my code broke because of Array.fill not being transpiled into compatible code with my browser environment.
My code looked like this:
// constant to show example
const count = 10;
const segments = Array(count - 1).fill(0);
Obviously my usage of fill here is to populate a dynamically determined array length with dummy values so I can eventually populate it using map
, as sparse arrays are ignored by array methods.
Turns out, this actually worked with buble in its place
const count = 10
const segments = Array(...Array(count - 1)).map(() => 0);
Which is effectively Array.apply(null, Array(count -1)).map(function(){ return 0 });
I understand it's an additional compilation step if you were to transpile it from fill(0)
to use the spread operator, so I'm aware that that might be a limitation.
There might be something I'm missing here, but is there a reason we can't statically analyze Array.fill
calls to map to an Array.apply
call followed by .map()
which would pass in the current array being worked on?
e.g. if I have some arbitrary array of values and I want to call fill
[1, 2, true, false, "lol"].fill(2) // -> [2, 2, 2, 2, 2]
couldn't it just transpile into this?
let a = [1, 2, true, false, "lol"]
Array.apply(a, Array(a.length)).map(() => 2);
This is a fill that should work even for sparse arrays.
Note: The longer I write this the more I become aware that yes, fill
doesn't always have an arity of one argument, so supporting filling subsections of the array might take more finesse or be more complicated, but I figured it was worth starting a dialog.