kodieren
Encoding related utilities
Last updated 3 years ago by thlorenz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install kodieren 
SYNC missed versions from official npm registry.

kodieren build status

Encoding related utilities

Installation

npm install kodieren

Table of Contents generated with DocToc

API

dectoen

Derives an encoding map from a decoding table

Parameters

  • arr Array<T> encoding table

Returns Map<T, Number> decoding map

maskForBits

Gets the bit mask for the specified number of bits

Parameters

Returns Number the bitmask, i.e. 111 for 3 bits

bitsToLayout

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.

Example:

bitsToLayout([ [ 'foo', 2 ], [ 'bar', 4 ] ])
// => { foo: [ 0, 0b11 ], bar: [ 2, 0b1111 ] }

Parameters

Returns Object<Array<Number, Number>>

shiftMask

Righ shifts the given number as specified and then applies the given mask. This is useful to isolate information from a bit field.

Example

shiftMask(0b100010110011, 4, 0b1111)
=> 0b1011

Parameters

  • n Number the number containing the information we want
  • digits Number the amount
  • mask Number the mask to apply after shifting

Returns Number the result after shift and mask was applied

isolate

Isolates some digits from a bit field

Parameters

  • bits Number the bit field
  • idx Number the index at which the digits to isolate start (from the right)
  • len Number the amount of bits to isolate

Returns Number the isolated bits

number

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

bin

Renders a binary representation of the given number padded as specified

Parameters

  • n Number number to print
  • nbits Number? amount of total digits to print (optional, default 32)

Returns String the rendered number

Encoder

Instantiates an encoder that uses the decodeArray to encode/decode to/from.

Example

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 map
  • Array.null <Number, Number> [ bitIdx, mask ] used to isolate values from the given bits
  • preEncode 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

Encoder.encode

Encodes the item to bits according to the encoding table derived from the decodeArray

Parameters

  • item T item to encode

Returns Number bits representing the item

Encoder.decode

Decodes the bits from the decodeArray

Parameters

Returns T the item represented by the bits

License

MIT

Current Tags

  • 0.2.0                                ...           latest (3 years ago)

2 Versions

  • 0.2.0                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (3)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |