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

@fry/base62

v1.1.0

Published

Base62 encoder/decoder

Downloads

735

Readme

base62

Base62 encoder/decoder

Installation

npm install --save @fry/base62

Usage

encode() expects data that can be turned into a Buffer (e.g. a string, an Array, ArrayBuffer, or a Buffer) and it returns the Base62 encoded string.

decode() returns the decoded data in a Buffer which has good readers for binary data as well as a fast toString() that can return the data encoded in utf8 (default), base64, hex, etc.

Binary input

    const base62 = require('@fry/base62');

    const encoded = base62.encode([0x2a, 0x2a]);
    // encoded => AYA

    const decoded = base62.decode(encoded);
    // decoded => <Buffer 2a 2a>

    decoded[0]; // => 0x2a
    decoded.readUInt16BE(); // => 0x2a2a

String input

    const encoded = base62.encode('Memento vivere');
    // encoded => JMLjPMvqRo1sQNPbSc5

    const decoded = base62.decode(encoded);
    // decoded => <Buffer 4d 65 6d 65 6e 74 6f 20 76 69 76 65 72 65>

    decoded.toString(); // => Memento vivere

Custom symbol table

There is no standard for Base62 encoding so various encoders may use different symbol tables. This module will by default use alphanumerical characters in their natural order: 0-9A-Za-z.

To use another set of characters first initiate them with prepareSymbolTable() and then supply the resulting object as a second parameter to the encode/decode functions, e.g.:

    const symbols = base62.prepareSymbolTable('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
    const encoded = base62.encode([0x53, 0xfe, 0x92], symbols);
    // encoded => U98kC

    const decoded = base62.decode(encoded, symbols);
    // decoded => <Buffer 53 fe 92>

Base62 encoding process

The Base62 symbol table is limited to 62 characters, which is just shy of 6 bits of information. This means that special care needs to be taken in order to encode any given stream of bytes. This module takes the approach of reading a sextet (6-bit chunk) from the binary stream and encode it into the symbol table as long as the chunk is not in risk of overflowing. If the sextet is in the ”overflow zone“ then only 5 bits are used while encoding that particular chunk, indicated in the output by the presence of one of the symbol table’s last two characters. The overflow bit is left in the stream until the next sextet will be read. The last chunk may also be left-padded with zeroes during the encoding process which the decoder will detect and reverse to ensure byte-alignment of the underlying binary stream.

Happy encoding!

/ Fry