RLE array encoder/decoder
npm install orle

orle (pronounced "Oh, really?") is simple run length encoder for Javascript typed arrays. orle is simple. There are two exposed methods: encode and decode.Uint16Array or Float32Array) to encode, that data type is used. encode returns a promise with an encoded buffer. If you pass a non-typed array, it will try to find the most compact data type possible to encode the data. Note: if decimals are found, it will use Float64Array to preserve precision.```
const orle = require('orle');
const buffer = await orle.encode([1,1,1,1,1,1,5,6,1,1,1,1,1,1,1,1,1]);`
or`
const orle = require('orle');
const buffer = await orle.encode(new Int8Array([1,1,1,1,1,1,5,6,1,1,1,1,1,1,1,1,1]));
If it's advantageous to gzip part of the payload, it will do so automatically. You can opt out of this by passing options to encode orle.encode(arr, { gzip: false }).
or undefined as elements. null or undefined get coerced to 0 when encoding.Decoding
Pass a buffer to decode and you will get back a promise of a typed array. `
const orle = require('orle');
const arr = await orle.decode(buffer);
`Compression
Obviously your results may vary. If you have a large array with entirely non-repeating numbers, this will add about 9 bytes to the total payload. If you have a large array of entirely repeating numbers, the resulting payload will be about 10 bytes.Format
The binary format is pretty simple:$3
Bytes: 1
Sample Value: 7
$3
Bytes: 1
Sample Value: The first 7 bits are an unsigned int representing different data formats. Possible formats are:
* 0: Int32
* 1: Int16
* 2: Int8
* 3: Uint32
* 4: Uint16
* 5: Uint8
* 6: Float32
* 7: Float64
* 8: String If the last bit is set, that indicates that a lookup table is present
If the second last bit is set, that indicates that the
Payload is gzipped$3
Bytes: 1
Sample Value: The size of each "run" value. Unsigned 8 bit int:
* 0: Int32
* 1: Int16
* 2: Int8 $3
Bytes: size-of-each-run-value * (number-of-distinct-runs+1)
Sample Value: Store each set of run values. Positive values indicates that the value is repeated that number of times. Negative values indicates that there is a run of distinct values. 0 indicates there are no more runs defined$3
Bytes: 0 or 1
Sample Value: If the lookup table bit was set, this indicates how many items are in the LUT (max of 256)$3
Bytes: size-of-each-LUT-value * number-of-items-in-LUT
Sample Value: The data is serialized flat and is obviously variable length. There are a maximum of 256 values in the lookup table. If a Lookup table is used, the payload items are serialized as Uint8s indicating the index into this array that they map to$3
Bytes: A 4 byte UInt32 followed by a gzip payload
Note: The Payload is gzipped if the second bit of the data type lookup is set. If the payload is gzipped, the rest of the storage format remains the same, except it is gzipped/gunzipped first.$3
Bytes: size-of-each-value * number-of-values-stored`