set-compressor
v4.0.1
Published
Compress and decompress Sets of non-negative Integers.
Downloads
268
Maintainers
Readme
Set-Compressor
Compress and decompress Sets of non-negative Integers.
Getting Started
$ npm install --save set-compressor
Usage
Consider an sequential array with gaps of the form
[0, 1, 2, ..., 498, 499, 500, 700, 701, 702, ..., 998, 999, 1000]
.
We want to store this efficiently. This is where this utility comes in handy.
const compressor = require('set-compressor').Compressor({/* options */});
compressor.compress([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// => /wc=
compressor.decompress('/wc=');
// => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Further examples can be found below.
Compressor
Options
The following options can be passed in when creating a Compressor.
gzip
Type: constants.GZIP_MODE
Default: AUTO
Controls how to use gzip: AUTO
, FORCE
and NEVER
,
where the default only uses compression if it improves the result size.
gzipLevel
Type: zlib.constants
Default: Z_BEST_COMPRESSION
Can be set to control the gzip compression level.
Functions
The following functions are available on the created Compressor.
compress(<iterable>)
Takes Iterable of non-negative Integers as input and returns compressed string.
decompress(<string>)
Takes compressed string as input and returns Array of unique, non-negative, sorted Integers.
Constants
GZIP_MODE
Values AUTO
, NEVER
, FORCE
Defines gzip mode used internally.
Examples
const compressor = require('set-compressor').Compressor({/* options */});
compressor.compress([0, 1, 2, /* ..., */ 9998, 9999, 10000]);
// => "H4sIAAAAAAACA/v/fxSMglEwCoYrYAAAhHk44+MEAIA="
compressor.decompress('H4sIAAAAAAACA/v/fxSMglEwCoYrYAAAhHk44+MEAIA=');
// => [0, 1, 2, ..., 9998, 9999, 10000]
compressor.decompress(compressor.compress([2, 2, 5, 1, 0]));
// => [0, 1, 2, 5]
Gotchas and Lmitations
This library operates with Arrays for performance reasons.
Any iterable containing non-negative integers can be provided as input, but re-inflating always outputs a unique and ordered Array.
This library is not meant to be used with and wont work well with huge Integers.