Skip to content

Rich-Harris/vlq

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
October 16, 2021 13:14
October 16, 2021 13:14
src
October 16, 2021 13:03
October 16, 2021 13:00
October 16, 2021 13:00
October 16, 2021 13:14
October 16, 2021 13:00
October 18, 2021 13:12
October 16, 2021 13:03
October 16, 2021 13:14
October 16, 2021 13:00
October 18, 2021 13:12
October 18, 2021 09:27
October 16, 2021 13:00

vlq.js

Convert integers to a Base64-encoded VLQ string, and vice versa. No dependencies, works in node.js or browsers, supports AMD.

Why would you want to do that?

Sourcemaps are the most likely use case. Mappings from original source to generated content are encoded as a sequence of VLQ strings.

What is a VLQ string?

A variable-length quantity is a compact way of encoding large integers in text (i.e. in situations where you can't transmit raw binary data). An integer represented as digits will always take up more space than the equivalent VLQ representation:

Integer VLQ
0 A
1 C
-1 D
123 2H
123456789 qxmvrH

Installation

npm install vlq

Usage

Encoding

vlq.encode accepts an integer, or an array of integers, and returns a string:

vlq.encode(123); // '2H';
vlq.encode([123, 456, 789]); // '2HwcqxB'

Decoding

vlq.decode accepts a string and always returns an array:

vlq.decode('2H'); // [123]
vlq.decode('2HwcqxB'); // [123, 456, 789]

Limitations

Since JavaScript bitwise operators work on 32 bit integers, the maximum value this library can handle is 2^30 - 1, or 1073741823.

Using vlq.js with sourcemaps

See here for an example of using vlq.js with sourcemaps.

Credits

Adapted from murzwin.com/base64vlq.html by Alexander Pavlov.

License

MIT.

About

Generate, and decode, base64 VLQ mappings for sourcemaps and other uses

Resources

License

Stars

Watchers

Forks

Packages

No packages published