$ cnpm install kodieren
Encoding related utilities
npm install kodieren
Table of Contents generated with DocToc
Derives an encoding map from a decoding table
Parameters
arr
Array<T> encoding tableReturns Map<T, Number> decoding map
Gets the bit mask for the specified number of bits
Parameters
n
Number number of bitsReturns Number the bitmask, i.e. 111
for 3 bits
Takes a table of properties with bit length and derives bit layout from it. The bit layout is a hash map indexed by property name and each entry contains the index of the property in the bit field and it's mask needed to isolate it.
bitsToLayout([ [ 'foo', 2 ], [ 'bar', 4 ] ])
// => { foo: [ 0, 0b11 ], bar: [ 2, 0b1111 ] }
Parameters
Returns Object<Array<Number, Number>>
Righ shifts the given number as specified and then applies the given mask. This is useful to isolate information from a bit field.
shiftMask(0b100010110011, 4, 0b1111)
=> 0b1011
Parameters
n
Number the number containing the information we wantdigits
Number the amountmask
Number the mask to apply after shiftingReturns Number the result after shift and mask was applied
Isolates some digits from a bit field
Parameters
bits
Number the bit fieldidx
Number the index at which the digits to isolate start (from the right)len
Number the amount of bits to isolateReturns Number the isolated bits
An array of numbers whose index is equal it's value. Useful if we need to pass a table when encoding a number.
Type: Number
Renders a binary representation of the given number padded as specified
Parameters
Returns String the rendered number
Instantiates an encoder that uses the decodeArray to encode/decode to/from.
const decodeArray = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]
const encoder = new Encoder(decodeArray, [ 0, 0b111 ])
const encoded = encoder.encode('c')
const decoded = encoder.decode(encoded)
console.log({ encoded, decoded })
// => { encoded: 2, decoded: 'c' }
Parameters
decodeArray
Array<T> the decode/encode mapArray.null
<Number, Number> [ bitIdx, mask ]
used to isolate values from the given bitspreEncode
function? conversion function run before an item is encoded (optional, default identity
)postDecode
function? conversion function run after an item is decoded (optional, default identity
)Returns Encoder instance
Encodes the item to bits according to the encoding table derived from the decodeArray
Parameters
item
T item to encodeReturns Number bits representing the item
Decodes the bits from the decodeArray
Parameters
bits
Number bits to decodeReturns T the item represented by the bits
MIT
Copyright 2014 - 2017 © taobao.org |