Skip to content

Latest commit

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

chunk-handler

NPMjs-semistandard-style

npm versionBuild StatusCoverage StatusKnown VulnerabilitiesNPM download/monthNPM download total
The Chunk Handler for string and array object in NodeJS or Browser.

Install using NPM

$ npm install chunk-handler

Or 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>

Usage

constChunkHandler=require('chunk-handler');// in browser doesn't need this linevarch=newChunkHandler();

Chunk with String

Make Chunk

varstr='this fruit is mango!';varresult=ch.make(str,2);// Result// [ 'th', 'is', ' f', 'ru', 'it', ' i', 's ', 'ma', 'ng', 'o!' ]

Save chunk to memory

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);};

Get saved chunk by name

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!' }// ]

Merge Chunk

varresult=ch.merge(ch.get('xxx'));// Result// this fruit is mango!

Chunk with Array

Make Chunk

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 ] ]

Make Aleatory Chunks

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 ] ]

Save chunk to memory

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);};

Get saved chunk by name

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 ] }// ]

Merge Chunk

varresult=ch.merge(ch.get('yyy'));// Result// [1,2,3,4,5,6,7,8,9,10]

Remove chunk by name

ch.remove('xxx');

Clean all chunk

ch.clean();

Get all saved data

ch.getBody()

Get Best Size before make a chunk

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.

Asynchronous

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!' }// ]});

Helper function

Here is available helper function

  • isString(value)
  • isArray(value)
  • isObject(value)
  • isEmpty(value)
  • isEmptyArray(value)
  • isEmptyObject(value)
  • getBestSize(length,split=5)
  • blockingTest(ms=1000)

Unit test

If you want to playing arround with test
npm test

About

The Chunk Handler for string and array object in NodeJS

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages