1.0.1 • Published 1 year ago

@hookun/vlq v1.0.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
1 year ago

@hookun/vlq

codecov .github/workflows/push.yml .github/workflows/publish.yml

A tool to encode/decode arbitrary unsigned integers using predefined-length chunk of bits.

Usage

import {encodeToArrayBuffer, decodeToArray} from '@hookhookun/vlq';
const data = [1, 2, 3, 4];
const encoded1 = encodeToArrayBuffer(data, 1);
console.info(Buffer.from(encoded1).toString('hex'));
// 073788
console.info(Buffer.from(encoded1).toString('base64url'));
// BzeI
console.info(decodeToArray(encoded1));
// [ 1, 2, 3, 4 ]
const encoded2 = encodeToArrayBuffer(data, 2);
console.info(Buffer.from(encoded1).toString('hex'));
// 129d10
console.info(Buffer.from(encoded1).toString('base64url'));
// Ep0Q
console.info(decodeToArray(encoded1));
// [ 1, 2, 3, 4 ]

Install

npm install @hookun/vlq

API document

docs/modules.md

Format of encoded binary

Encoded     = ChunkSize -> Data -> EndOfChunks
ChunkSize   = Value(3,C-1)
Data        = *Value(C,x)
EndOfChunks = SB(C,0)
Value(n,x)  = *SB(n,x1) -> LSB(n,x0)
SB(n,x)     = 1xxxxxxxxxxx
               | <- n -> |
LSB(n,y)    = 0yyyyyyyyyyy

The binary representation of ChunkSize is 1 less than the actual value.

Example: Value(1+n) and EndOfChunks (EOC)

Assume that ChunkSize is 1.

xValue(1,x)
000
101
211 00
311 01
411 10 00
511 10 01
811 10 10 00
1611 10 10 10 00
EOC10

Assume that ChunkSize is 2.

xValue(2,x)
0000
1001
2010
3011
4101 000
5101 001
8110 000
16101 100 000
32110 100 000
64101 100 100 000
EOC100

Assume that ChunkSize is 3.

xValue(3,x)
00000
10001
20010
30011
40100
81001 0000
161010 0000
321100 0000
641001 1000 0000
EOC1000

Example: Encoded

Assume that Data is [1, 2, 3, 4] ChunkSize is 1.

EndOfChunks = 10
Data        = 01 11 00 11 01 11 10 00
ChunkSize   = 0000
Encoded     = 0000 01 11 00 11 01 11 10 00 10
            = ChunkSize -> Data -> EndOfChunks
            = 0000011100110111100010
            = 0000 0111 0011 0111 1000 10(00)
            =    0    7    3    7    8     8
            = 0x073788

Assume that Data is [1, 2, 3, 4] ChunkSize is 2.

EndOfChunks = 100
Data        = 001 010 011 101 000
ChunkSize   = 0001
Encoded     = 0001 001 010 011 101 000 100
            = ChunkSize -> Data -> EndOfChunks
            = 0001001010011101000100
            = 0001 0010 1001 1101 0001 00(00)
            =    1    2    9    d    1     0
            = 0x129d10

Example: decoding 0x51450f2880

Encoded =    5   1   4   5   0   f   2   8   8   0
        = 0101000101000101000011110010100010000000
          ^^^^|          <-   Data   ->          |

Since ChunkSize is 5, divide Data into (5+offset+flag bit)=(5+1+1)=7 bits each.

Data = 0001010 0010100 0011110 0101000 1000000 0
     =      10      20      30      40     EOC
     = [10, 20, 30, 40]

License

Apache License, Version 2.0

1.0.1

1 year ago

1.0.0

1 year ago

0.0.8

2 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.3

4 years ago