Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor array/split

  • Loading branch information...
commit 99ef27c981e1a631a6976e912ac2a00c0cdbac1c 1 parent 8ceffe3
@millermedeiros authored
Showing with 4 additions and 3 deletions.
  1. +4 −3 src/array/split.js
View
7 src/array/split.js
@@ -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

@conradz

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 :neutral_face: )

@millermedeiros

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.

@conradz

OK, thanks for explaining!

Please sign in to comment.
Something went wrong with that request. Please try again.