Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#1.21.0
- feat: add chunk

#1.20.0
- feat: add transformKeysToSnake
- feat: add sample
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Currently supported utils:
- `assignWith` - Assigns the object properties from the sources
- `camelize` - simple camel case
- `capitalize` - make first char uppercase
- `chunk` - chunk an array into chunks
- `clone` - Shallow clone the object
- `cloneDeep` - deep clone functionality for objects
- `difference` - creates an array of values from the first argument not included in the second argument
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export function assignInWith (obj: Object, ...sources: Object[]): Object
export function camelize (str: string): string
export function capitalize (str: string): string
export function chunk<T> (anArray: Array<T>, chunkSize: number): Array<Array<T>>
export function clone (obj: Object): Object
export function cloneDeep (obj: Object): Object
export function difference (array: Array<any>, values: Array<any>): Array<any>
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assignInWith = require('./src/assignWith')
const camelize = require('./src/camelize')
const capitalize = require('./src/capitalize')
const chunk = require('./src/chunk')
const clone = require('./src/clone')
const cloneDeep = require('./src/cloneDeep')
const difference = require('./src/difference')
Expand Down Expand Up @@ -56,6 +57,7 @@ module.exports = {
assignInWith,
camelize,
capitalize,
chunk,
clone,
cloneDeep,
difference,
Expand Down
73 changes: 32 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinexcom/lib-js-util-base",
"version": "1.20.0",
"version": "1.21.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions src/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const isNumber = require('./isNumber')

const chunk = (anArray, chunkSize) => {
if (!Array.isArray(anArray) || !isInteger(chunkSize)) return []
const chunks = []
for (let i = 0; i < anArray.length; i += chunkSize) {
chunks.push(anArray.slice(i, i + chunkSize))
}
return chunks
}

const isInteger = (x) => {
return isNumber(x) && Math.trunc(x) === x
}

module.exports = chunk
19 changes: 19 additions & 0 deletions test/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

/* eslint-env mocha */

const assert = require('assert')
const { chunk } = require('../index')

describe('chunk', () => {
it('should chunk anArray in smaller arrays', () => {
assert.deepEqual(chunk([1, 2], 2), [[1, 2]])
assert.deepEqual(chunk([1, 2], 1), [[1], [2]])
})

it('should return empty array for invalid arguments', () => {
assert.deepEqual(chunk('notAnArray', 1), [])
assert.deepEqual(chunk([1, 2], 1.1), [])
assert.deepEqual(chunk([1, 2], 'notANumber'), [])
})
})