npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vle-integers

v0.1.1

Published

Variable Length Encoded Integers

Downloads

31

Readme

JavaScript Variable Length Encoded Integers

Dynamically encodes a number into N bytes:

0->63 -- 1 bytes
->8,191 -- 2 bytes
->1,048,575 -- 3 bytes
->2^27-1 -- 4 bytes
->2^31-1 -- 5 bytes

Important: 2^31 - 1 is currently the largest number this library can encode. This is not a hard limitation, it's just that it requires a bit more logic after that point and I don't currently need numbers that large myself so I didn't write it. If you need this open a bug report (or better yet a pull request)!

I could make an unsigned version, but I don't need it myself -- open an issue if you need this.

Usage

var encode  = require('vle-integers').encode;
var decode  = require('vle-integers').decode;

var encode  = encode(123);
var decoded = decode(123);

Decode Method

The decode method will only decode a specific number, meaning that if you attempt to decode something like:

var decoded = encode('123') + 'asdf'; // yields 123

It will work correctly. It will also properly decode Buffers. There is an optional offset that you can use to tell it where to start decodeing from:

var encoded = 'asdf' + encode(123);
var decoded = decode(encoded, 4); // yields {number: 123, offset:6}

Encode Method

Encoding always returns a string. If you want a Buffer, create one by passing the result to a Buffer and being sure to set the encoding as ascii:

var encode  = require('vle-integers').encode;
var encoded = encode(123);

var buf = new Buffer(encoded, 'ascii');

// or

buf.write(encoded, 0, 'ascii');

There is also an optional encode.array method that returns an array of numbers representing the ascii value of each byte.