File tree Expand file tree Collapse file tree 3 files changed +24
-7
lines changed Expand file tree Collapse file tree 3 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -168,7 +168,7 @@ Following API examples are shown based on the sample JSON data given [here](exam
168
168
* [ sortBy] ( #sortbyproperty-order )
169
169
* [ reset] ( #resetdata )
170
170
* [ copy] ( #copy )
171
- * [ chunk] ( #chunk )
171
+ * [ chunk] ( #chunksize )
172
172
173
173
### ` fetch() `
174
174
@@ -524,9 +524,11 @@ It will return a complete clone of the Object instance.
524
524
525
525
See a detail example [ here] ( examples/copy.js ) .
526
526
527
- ### ` chunk(size) `
527
+ ### ` chunk(size, fn ) `
528
528
529
529
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.
530
532
531
533
See a detail example [ here] ( examples/chunk.js ) .
532
534
@@ -544,5 +546,5 @@ Speical thanks to [Nahid Bin Azhar](https://github.com/nahid) for the inspiratio
544
546
545
547
## Contributions
546
548
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.
548
550
See all the [ contributors] ( CONTRIBUTORS.md ) .
Original file line number Diff line number Diff line change @@ -13,3 +13,12 @@ const chunking = Q.from('users')
13
13
14
14
console . log ( '-------- Printing Result of chunk ---------' ) ;
15
15
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -84,14 +84,14 @@ class JSJsonQ {
84
84
* chunk - group the resulted collection to multiple chunk
85
85
*
86
86
* @param {integer } The length of each chunk
87
- * @return {Array } New Array
87
+ * @param {fn } an anonymous function
88
+ * @return {Array } New Array
88
89
*/
89
-
90
- chunk ( size = 0 ) {
90
+ chunk ( size = 0 , fn = null ) {
91
91
if ( size <= 0 ) {
92
92
throw Error ( 'Invalid chunk size' ) ;
93
93
}
94
-
94
+
95
95
this . _prepare ( ) ;
96
96
97
97
let _newContent = [ ] ;
@@ -100,6 +100,12 @@ class JSJsonQ {
100
100
_newContent . push ( this . _jsonContent . splice ( 0 , size ) ) ;
101
101
}
102
102
103
+ if ( fn instanceof Function ) {
104
+ for ( let i = 0 ; i < this . _jsonContent . length ; i ++ ) {
105
+ _newContent [ i ] = fn ( _newContent [ i ] ) ;
106
+ }
107
+ }
108
+
103
109
this . _jsonContent = _newContent ;
104
110
105
111
return this . _jsonContent ;
You can’t perform that action at this time.
0 commit comments