README
base-n
A utility for encoding/decoding base10 integers into a URL safe base-n string
Getting Started
Install the module with: npm install base-n
Why?
The primary use case for this module is to shorten numerical IDs in terms of number of characters for URL usage, and then to easily decode those again at a later point in time. For example, base10 only supports up to 100 unique IDs in a two character space. By contrast, base64 supports up to (64^2 =) 4096 unique IDs in the same two character space.
It should be noted that the encoding does not use a random number generater or a salt, so if cryptographic security is of importance, this probably won't meet your needs.
base-n supports encoding base10 integers into a non base10 encoded string, where n can be any value between 2 and 64. By default, the utility supports up to base64, using the following URL safe characters:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-
Usage
To use the lib, simply create an encoder instance:
var baseN = require('base-n');
var b64 = baseN.create();
b64.encode(10);
// => 'a'
b64.encode(100);
// => '1a'
b64.encode(842673);
// => '3dKN'
To decode, you can use the same object:
b64.decode('z');
// => 35
b64.decode('zTh');
// => 146897
Choosing a different base simply uses a subset of these available characters. Should you need to use a completely different set of characters (e.g., if you have no need for URL safe characters), you can pass in your own custom set of characters.
var baseN = require('base-n');
var b2 = baseN.create({
characters: ['