underscore icon indicating copy to clipboard operation
underscore copied to clipboard

_.range throws error on Infinite values (1.9.0 regression)

Open lubelski opened this issue 8 years ago • 3 comments

1.8.3

_.range(Infinity, -Infinity); // =>  []

1.9.0

_.range(Infinity, -Infinity); // =>  throws error on array construction 

the code that caught us with this change was something like...

const items = [];
const render_range = _.range(_.min(items), _.max(items)); 

where previously this would return an empty array. now it throws. we put a guard in for items being empty but we use range all over.

It looks like the old assumption that step was positive would implicitly prevent the computed array length from being nonsensical.

1.8.3

https://github.com/jashkenas/underscore/blob/e4743ab712b8ab42ad4ccb48b155034d02394e4d/underscore.js#L683-L685

1.9.0

https://github.com/jashkenas/underscore/blob/master/underscore.js#L726-L730

should it just guard for finite values? any non-finite value passed in for stop or start seems wrong, and I'd want it to return [] in that case

lubelski avatar Apr 23 '18 22:04 lubelski

Hmm, I'm not sure. Since _.range constructs eager arrays of numbers, and not lazy ones — it seems like infinite ranges ought to throw an error instead of returning an empty range.

What do other folks think?

jashkenas avatar Apr 24 '18 19:04 jashkenas

I would agree that this should throw.

captbaritone avatar May 10 '18 18:05 captbaritone

Then let's leave this alone for now. Happy to add more test cases to assert that it's behaving properly, if anyone has any.

jashkenas avatar May 31 '18 19:05 jashkenas