├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'stable' 5 | - '0.12' 6 | - '0.10' 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (input, maxLength) { 3 | if (!Array.isArray(input)) { 4 | throw new TypeError('Expected an array to split'); 5 | } 6 | 7 | if (typeof maxLength !== 'number') { 8 | throw new TypeError('Expected a number of groups to split the array in'); 9 | } 10 | 11 | var result = []; 12 | var part = []; 13 | 14 | for (var i = 0; i < input.length; i++) { 15 | part.push(input[i]); 16 | 17 | // check if we reached the maximum amount of items in a partial 18 | // or just if we reached the last item 19 | if (part.length === maxLength || i === input.length - 1) { 20 | result.push(part); 21 | part = []; 22 | } 23 | } 24 | 25 | return result; 26 | }; 27 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Arthur Verschaeve (arthurverschaeve.be) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "split-array", 3 | "version": "1.0.1", 4 | "description": "Split an array into arrays of a specific length", 5 | "license": "MIT", 6 | "repository": "arthurvr/split-array", 7 | "author": { 8 | "name": "Arthur Verschaeve", 9 | "email": "contact@arthurverschaeve.be", 10 | "url": "arthurverschaeve.be" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "split", 23 | "sub arrays", 24 | "array", 25 | "chunks", 26 | "partials" 27 | ], 28 | "dependencies": {}, 29 | "devDependencies": { 30 | "ava": "*", 31 | "xo": "*" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # split-array [![Build Status](https://travis-ci.org/arthurvr/split-array.svg?branch=master)](https://travis-ci.org/arthurvr/split-array) 2 | 3 | > Split an array into arrays of a specific length 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save split-array 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const splitArray = require('split-array'); 17 | 18 | splitArray(['a', 'b', 'c', 'd', 'e', 'f'], 2); 19 | //=> [['a', 'b'], ['c', 'd'], ['e', 'f']] 20 | 21 | splitArray(['a', 'b', 'c', 'd', 'e', 'f', 'foo'], 3); 22 | //=> [['a', 'b', 'c'], ['d', 'e', 'f'], ['foo']] 23 | ``` 24 | 25 | 26 | ## API 27 | 28 | ### splitArray(array, maxLength) 29 | 30 | #### array 31 | 32 | *Required* 33 | Type: `array` 34 | 35 | The array to split up. 36 | 37 | #### maxLength 38 | 39 | *Required* 40 | Type: `number` 41 | 42 | The maximum amount of items a partial can have 43 | 44 | 45 | ## License 46 | 47 | MIT © [Arthur Verschaeve](http://arthurverschaeve.be) 48 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import splitArray from './'; 3 | 4 | test('splits simple array', t => { 5 | t.same( 6 | splitArray(['a', 'b', 'c', 'd', 'e', 'f'], 2), 7 | [['a', 'b'], ['c', 'd'], ['e', 'f']] 8 | ); 9 | 10 | t.same( 11 | splitArray(['a', 'b', 'c', 'd', 'e', 'f'], 3), 12 | [['a', 'b', 'c'], ['d', 'e', 'f']] 13 | ); 14 | 15 | t.end(); 16 | }); 17 | 18 | test('handles the last item correctly', t => { 19 | t.same( 20 | splitArray(['a', 'b', 'c', 'd', 'e', 'f', 'foo'], 6), 21 | [['a', 'b', 'c', 'd', 'e', 'f'], ['foo']] 22 | ); 23 | 24 | t.end(); 25 | }); 26 | 27 | test('Throws on nonsense input', t => { 28 | ['foo', function () {}, null, {}].forEach(input => { 29 | t.throws(() => { 30 | splitArray(input, 1); 31 | }); 32 | }); 33 | 34 | t.end(); 35 | }); 36 | --------------------------------------------------------------------------------