diff --git a/src/util.js b/src/util.js index d8fc064..684a4c2 100644 --- a/src/util.js +++ b/src/util.js @@ -97,6 +97,59 @@ class Util { return tempNum } + /** + * @api util.chunk20() chunk20() + * @apiName chunk20 + * @apiGroup Util + * @apiDescription chunk up an array into multiple arrays of 20 elements each. + * Input: arrayToSlice - a one-dimensional array of elements. + * Returns a two-dimensional array. An array of 20-element arrays. + * + * @apiExample Example usage: + * (async () => { + * try { + * const bigArray = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26] + * + * const chunked = bchjs.Util.chunk20(bigArray) + * console.log(chunked) + * } catch(error) { + * console.error(error) + * } + * })() + * + * // returns + * [ + * [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19], + * [20,21,22,23,24,25,26] + * ] + */ + // chunk20 - chunk up an array into multiple arrays of 20 elements each. + // Input: arrayToSlice - a one-dimensional array of elements. + // Returns a two-dimensional array. An array of 20-element arrays. + chunk20 (arrayToSlice) { + try { + // Validate inputs + if (!Array.isArray(arrayToSlice)) { + throw new Error('input must be an array') + } + + let offset = 0 + const result = [] + + // Loop over the array and slice off chunks of 20 elements. + while (offset < arrayToSlice.length) { + const chunk = arrayToSlice.slice(offset, offset + 20) + result.push(chunk) + offset = offset + 20 + } + + return result + } catch (err) { + console.error('Error in chunk20()') + throw err + } + } + /** * @api util.validateAddress() validateAddress() * @apiName Validate Address. diff --git a/test/unit/util.js b/test/unit/util.js index 09ffcaa..f40b4c6 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -99,4 +99,77 @@ describe('#Util', () => { assert2.equal(result, 1.2) }) }) + + describe('#chunk20', () => { + const thirtyFiveElements = [ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34 + ] + + it('should split 35 elements into 2 arrays', () => { + const result = bchjs.Util.chunk20(thirtyFiveElements) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert2.isArray(result) + assert2.isArray(result[0]) + assert2.equal(result.length, 2) + assert2.equal(result[0].length, 20) + }) + + it('should return accurately with an array of less than 20 elements', () => { + const elems = [0, 1, 2, 3, 4, 5] + + const result = bchjs.Util.chunk20(elems) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert2.isArray(result) + assert2.isArray(result[0]) + assert2.equal(result.length, 1) + assert2.equal(result[0].length, 6) + }) + + it('should throw an error for non-array input', () => { + try { + bchjs.Util.chunk20('string') + + assert2.equal(true, false, 'Unexpected result') + } catch (err) { + // console.log(err) + assert2.include(err.message, 'input must be an array') + } + }) + }) })