Permalink
Browse files

refactor array/split

  • Loading branch information...
1 parent 8ceffe3 commit 99ef27c981e1a631a6976e912ac2a00c0cdbac1c @millermedeiros committed Aug 23, 2012
Showing with 4 additions and 3 deletions.
  1. +4 −3 src/array/split.js
View
@@ -12,10 +12,11 @@ define(function() {
remainder = array.length % segments,
start = 0,
i = 0,
- n = array.length;
+ n = array.length,
+ len;
- for (var i = 0; start < n; i++) {
- var len = i < remainder ? segmentLength + 1 : segmentLength;
+ while (start < n) {
+ len = i++ < remainder ? segmentLength + 1 : segmentLength;
output.push(array.slice(start, start + len));
start += len;
}

3 comments on commit 99ef27c

Contributor

conradz commented on 99ef27c Aug 23, 2012

Is this the code style you prefer for all contributions (defining all vars at the top of function)? (BTW, sorry for the duplicate i declaration, I thought I had checked over it before 😐 )

Yes, I'm manually hoisting all the vars because most JS style guides says it's a good practice. On my own apps I usually declare multiple vars since I think it's easier to reorder and avoid all the comma-first discussions (closure compiler is smart enough to produce small code).

I changed the for loop to while just for brevity, you can use whatever you feel more comfortable. Closure compiler and UglifyJS will produce small code anyway (for/while would result in the same output).

The repository has a .jshintrc file that can be used to validate the source code with node-jshint. Depending on which editor you are using it can be run automatically at each file save. But don't worry too much about it, unit tests and working code are way more important.

Thanks for all the contributions.

Contributor

conradz replied Aug 23, 2012

OK, thanks for explaining!

Please sign in to comment.