Skip to content

Commit 56340c6

Browse files
authored
Merge pull request #3 from hashemirafsan/h11n
Adding 'chunk()' method as a new API
2 parents 3f35275 + 776c2df commit 56340c6

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +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)
171172

172173
### `fetch()`
173174

@@ -523,6 +524,12 @@ It will return a complete clone of the Object instance.
523524

524525
See a detail example [here](examples/copy.js).
525526

527+
### `chunk(size)`
528+
529+
It will return a complete new array after chunking your array with specific size.
530+
531+
See a detail example [here](examples/chunk.js).
532+
526533
## Bugs and Issues
527534

528535
If you encounter any bugs or issues, feel free to [open an issue at

examples/chunk.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Example of Chunk() API
3+
---------------------
4+
You need to pass an integer number to chunk method to get your desired result.
5+
*/
6+
7+
const jsonQ = require('../index.js');
8+
let Q = new jsonQ(__dirname + '/data.json');
9+
10+
const chunking = Q.from('users')
11+
.where('location', '=', 'Barisal')
12+
.chunk(4);
13+
14+
console.log('-------- Printing Result of chunk ---------');
15+
console.log(chunking);

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ class JSJsonQ {
8080
return this._jsonContent;
8181
}
8282

83+
/**
84+
* chunk - group the resulted collection to multiple chunk
85+
*
86+
* @param {integer} The length of each chunk
87+
* @return {Array} New Array
88+
*/
89+
90+
chunk(size = 0) {
91+
if(size <= 0) {
92+
throw Error('Invalid chunk size');
93+
}
94+
95+
this._prepare();
96+
97+
let _newContent = [];
98+
99+
while(this._jsonContent.count() > 0) {
100+
_newContent.push(this._jsonContent.splice(0, size));
101+
}
102+
103+
this._jsonContent = _newContent;
104+
105+
return this._jsonContent;
106+
}
107+
108+
83109
/**
84110
* at - get the data traversing through the given path hierarchy
85111
* Will return the JsonQuery Object instance, so that further

0 commit comments

Comments
 (0)