Skip to content

Commit 4cb9909

Browse files
committed
chunk method udpated
1 parent b96ba04 commit 4cb9909

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Following API examples are shown based on the sample JSON data given [here](exam
168168
* [sortBy](#sortbyproperty-order)
169169
* [reset](#resetdata)
170170
* [copy](#copy)
171-
* [chunk](#chunk)
171+
* [chunk](#chunksize)
172172

173173
### `fetch()`
174174

@@ -524,9 +524,11 @@ It will return a complete clone of the Object instance.
524524

525525
See a detail example [here](examples/copy.js).
526526

527-
### `chunk(size)`
527+
### `chunk(size, fn)`
528528

529529
It will return a complete new array after chunking your array with specific size.
530+
If you want to transform each of the chunk based on any specific logic, pass a
531+
function containing that transformation as the second parameter of the `chunk()` method.
530532

531533
See a detail example [here](examples/chunk.js).
532534

@@ -544,5 +546,5 @@ Speical thanks to [Nahid Bin Azhar](https://github.com/nahid) for the inspiratio
544546

545547
## Contributions
546548

547-
If your PR is successfully merged to this project, feel free to add yourself in the list of contributors.
549+
If your PR is successfully merged to this project, feel free to add yourself in the list of contributors.
548550
See all the [contributors](CONTRIBUTORS.md).

examples/chunk.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ const chunking = Q.from('users')
1313

1414
console.log('-------- Printing Result of chunk ---------');
1515
console.log(chunking);
16+
17+
Q = Q.reset();
18+
19+
20+
const transformedChunk = Q.from('users')
21+
.where('location', '=', 'Barisal')
22+
.chunk(4, (chunk) => {
23+
return chunk.map((data) => {{ id: data.id}});
24+
});

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ class JSJsonQ {
8484
* chunk - group the resulted collection to multiple chunk
8585
*
8686
* @param {integer} The length of each chunk
87-
* @return {Array} New Array
87+
* @param {fn} an anonymous function
88+
* @return {Array} New Array
8889
*/
89-
90-
chunk(size = 0) {
90+
chunk(size = 0, fn = null) {
9191
if(size <= 0) {
9292
throw Error('Invalid chunk size');
9393
}
94-
94+
9595
this._prepare();
9696

9797
let _newContent = [];
@@ -100,6 +100,12 @@ class JSJsonQ {
100100
_newContent.push(this._jsonContent.splice(0, size));
101101
}
102102

103+
if(fn instanceof Function) {
104+
for(let i = 0; i < this._jsonContent.length; i++) {
105+
_newContent[i] = fn(_newContent[i]);
106+
}
107+
}
108+
103109
this._jsonContent = _newContent;
104110

105111
return this._jsonContent;

0 commit comments

Comments
 (0)