npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chunk-handler

v1.6.0

Published

The Chunk Handler for string and array object in NodeJS or Browser

Downloads

61

Readme

chunk-handler

NPM js-semistandard-style

npm version Build Status Coverage Status Known Vulnerabilities NPM download/month NPM 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! -->
<script src="https://cdn.jsdelivr.net/npm/chunk-handler/dist/chunkhandler.min.js"></script>

<!-- Get minor updates and patch fixes within a major version -->
<script src="https://cdn.jsdelivr.net/npm/chunk-handler@1/dist/chunkhandler.min.js"></script>

<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chunkhandler.min.js"></script>

<!-- Get a specific version -->
<!-- Recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chunkhandler.min.js"></script>

Usage

const ChunkHandler = require('chunk-handler'); // in browser doesn't need this line
var ch = new ChunkHandler();

Chunk with String

Make Chunk

var str = 'this fruit is mango!';
var result = ch.make(str,2);

// Result
// [ 'th', 'is', ' f', 'ru', 'it', ' i', 's ', 'ma', 'ng', 'o!' ]

Save chunk to memory

var str = 'this fruit is mango!';
var result = ch.make(str,2);
var len = result.length;
var i = 0;
for (i;i<len;i++) {
    ch.add('xxx',result[i],i);
};

Get saved chunk by name

var result = 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

var result = ch.merge(ch.get('xxx'));

// Result
// this fruit is mango!

Chunk with Array

Make Chunk

var arr = [1,2,3,4,5,6,7,8,9,10];
var result = ch.make(arr,2);

// Result
// [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ]

Make Aleatory Chunks

var arr = [1,2,3,4,5,6,7,8,9,10];
var result = 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

var arr = [1,2,3,4,5,6,7,8,9,10];
var result = ch.make(arr,2);
var i = 0, len = result.length;
for (i; i < len; i++) {
    ch.add('yyy',result[i],i);
};

Get saved chunk by name

var result = 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

var result = 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.

var str = 'assume this is a big string with more than 300K chars length';

var result = 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) => {return builder}).then((chunk) => {
    var result = chunk.make(str,2);

    // just print output
    console.log(result);

    // result
    // [ 'th', 'is', ' f', 'ru', 'it', ' i', 's ', 'ma', 'ng', 'o!' ]

    // or if you want to save into memory
    var len = result.length;
    var i = 0;
    for (i;i<len;i++) {
        chunk.add('xxx',result[i],i);
    };

    // print output from saved memory
    console.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