The Chunk Handler for string and array object in NodeJS or Browser.
$ npm install chunk-handlerOr simply use in Browser with CDN
<!-- Always get the latest version --><!-- Not recommended for production sites! --><scriptsrc="https://cdn.jsdelivr.net/npm/chunk-handler/dist/chunkhandler.min.js"></script><!-- Get minor updates and patch fixes within a major version --><scriptsrc="https://cdn.jsdelivr.net/npm/chunk-handler@1/dist/chunkhandler.min.js"></script><!-- Get patch fixes within a minor version --><scriptsrc="https://cdn.jsdelivr.net/npm/chunk-handler@1.6/dist/chunkhandler.min.js"></script><!-- Get a specific version --><!-- Recommended for production sites! --><scriptsrc="https://cdn.jsdelivr.net/npm/chunk-handler@1.6.0/dist/chunkhandler.min.js"></script>constChunkHandler=require('chunk-handler');// in browser doesn't need this linevarch=newChunkHandler();varstr='this fruit is mango!';varresult=ch.make(str,2);// Result// [ 'th', 'is', ' f', 'ru', 'it', ' i', 's ', 'ma', 'ng', 'o!' ]varstr='this fruit is mango!';varresult=ch.make(str,2);varlen=result.length;vari=0;for(i;i<len;i++){ch.add('xxx',result[i],i);};varresult=ch.get('xxx');// result// [// { part: 0, data: 'th' },// { part: 1, data: 'is' },// { part: 2, data: ' f' },// { part: 3, data: 'ru' },// { part: 4, data: 'it' },// { part: 5, data: ' i' },// { part: 6, data: 's ' },// { part: 7, data: 'ma' },// { part: 8, data: 'ng' },// { part: 9, data: 'o!' }// ]varresult=ch.merge(ch.get('xxx'));// Result// this fruit is mango!vararr=[1,2,3,4,5,6,7,8,9,10];varresult=ch.make(arr,2);// Result// [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ]vararr=[1,2,3,4,5,6,7,8,9,10];varresult=ch.makeAleatory(arr,2);// Result could be (every time you run it, will get different results)// [ [ 3, 5, 10, 8, 7], [2, 1, 4, 9, 6 ] ]vararr=[1,2,3,4,5,6,7,8,9,10];varresult=ch.make(arr,2);vari=0,len=result.length;for(i;i<len;i++){ch.add('yyy',result[i],i);};varresult=ch.get('yyy');// result// [// { part: 0, data: [ 1, 2 ] },// { part: 1, data: [ 3, 4 ] },// { part: 2, data: [ 5, 6 ] },// { part: 3, data: [ 7, 8 ] },// { part: 4, data: [ 9, 10 ] }// ]varresult=ch.merge(ch.get('yyy'));// Result// [1,2,3,4,5,6,7,8,9,10]ch.remove('xxx');
ch.clean();
ch.getBody()
Using getBestSize(length,split=5) if an array/string is very big (higher than 100K chars length) and you don't know what best size to make a deal with chunk. Example case if you want to chunk encoded base64 from image or video.
varstr='assume this is a big string with more than 300K chars length';varresult=ch.make(str,ch.getBestSize(str.length));// Result will return an array with no more than 50 of array length. Note:
getBaseSize(length,split=5)default has split=5 which is will create maximum array (5*10) means will not create array length more than 50.- You are able to change split number between 1-10 only.
- High chars length more than 10 Millions is better to use split 6-10.
chunk-handler is synchronous as default, but if you worry about blocking event loop when handling big data, we have built-in promisify().
All you have to do is just wrap your code like this :
ch.promisify((builder)=>{returnbuilder}).then((chunk)=>{varresult=chunk.make(str,2);// just print outputconsole.log(result);// result// [ 'th', 'is', ' f', 'ru', 'it', ' i', 's ', 'ma', 'ng', 'o!' ]// or if you want to save into memoryvarlen=result.length;vari=0;for(i;i<len;i++){chunk.add('xxx',result[i],i);};// print output from saved memoryconsole.log(chunk.get('xxx'));// output// [// { part: 0, data: 'th' },// { part: 1, data: 'is' },// { part: 2, data: ' f' },// { part: 3, data: 'ru' },// { part: 4, data: 'it' },// { part: 5, data: ' i' },// { part: 6, data: 's ' },// { part: 7, data: 'ma' },// { part: 8, data: 'ng' },// { part: 9, data: 'o!' }// ]});Here is available helper function
isString(value)isArray(value)isObject(value)isEmpty(value)isEmptyArray(value)isEmptyObject(value)getBestSize(length,split=5)blockingTest(ms=1000)
If you want to playing arround with testnpm test
