documentcloud / underscore-contrib

The brass buckles on Underscore's utility belt
MIT License
621 stars 117 forks source link

_.chunk not working as excepted #195

Open dermellor opened 9 years ago

dermellor commented 9 years ago

The following statement

_.chunk(["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"],3,"-")

returns an array with 5 arrays, where the last one contains one item with the padding ("-"). This is obviously incorrect.

carltonf commented 9 years ago

I think it's somewhat by design, the way chunk handles empty array with padding is like this:

_.chunk([], 3, '-')  // => ['-'], maybe this should be []?

In your case, the leftover part after splitting is [], so you get ['-']. The current implementation of chunk actually needs clients check the length of array and chunk size beforehand, quite inconvenient. I'm not sure, need someone with more knowledge on this.