Compress and decompress Sets of non-negative Integers.
$ npm install --save set-compressor
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.
The following options can be passed in when creating a Compressor.
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.
Type: zlib.constants
Default: Z_BEST_COMPRESSION
Can be set to control the gzip compression level.
The following functions are available on the created Compressor.
Takes Iterable of non-negative Integers as input and returns compressed string.
Takes compressed string as input and returns Array of unique, non-negative, sorted Integers.
Values AUTO
, NEVER
, FORCE
Defines gzip mode used internally.
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]
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.