What is base62?
The base62 npm package provides utilities for encoding and decoding data using the Base62 encoding scheme. Base62 encoding is commonly used for shortening URLs, creating compact representations of data, and other applications where a compact, alphanumeric representation is beneficial.
What are base62's main functionalities?
Encoding
This feature allows you to encode a number into a Base62 string. The example encodes the number 12345 into the Base62 string 'dnh'.
const base62 = require('base62/lib/ascii');
const encoded = base62.encode(12345);
console.log(encoded); // Output: 'dnh'
Decoding
This feature allows you to decode a Base62 string back into a number. The example decodes the Base62 string 'dnh' back into the number 12345.
const base62 = require('base62/lib/ascii');
const decoded = base62.decode('dnh');
console.log(decoded); // Output: 12345
Other packages similar to base62
base-x
The base-x package is a versatile encoding library that supports custom base encoding schemes, including Base62. It is more flexible than base62 as it allows you to define your own character set for encoding and decoding.
base64url
The base64url package provides Base64 URL-safe encoding and decoding. While it focuses on Base64 rather than Base62, it is useful for similar applications where URL-safe encoding is required.
btoa
The btoa package provides Base64 encoding and decoding for Node.js. It is a simple and lightweight alternative for Base64 encoding, but does not support Base62.
A JavaScript Base62 encode/decoder
What is Base62 encoding?
Base62 encoding converts numbers to ASCII strings (0-9, a-z and A-Z) and vice
versa, which typically results in comparatively short strings. Such identifiers
also tend to more readily identifiable by humans.
999
≙ "g7"
9999
≙ "2Bh"
238327
≙ "ZZZ"
Installation
npm install base62
alternatively using Yarn:
yarn add base62
Usage
For backwards compatibility, Base62.js exposes v1.x's API by default – see
Legacy API below. For efficiency, v2.x's modernized API allows
selectively importing individual modules instead:
var base62 = require("base62/lib/ascii");
Base62.encode(999);
Base62.decode("g7");
This uses the default ASCII character set for encoding/decoding.
It's also possible to define a custom character set instead:
var base62 = require("base62/lib/custom");
var charset = "~9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-=";
charset = base62.indexCharset(charset);
Base62.encode(999, charset);
Base62.decode("F3", charset);
Note that indexCharset
typically expects the respective string to contain
exactly 62 unique character, but does not validate this for efficieny. In fact,
it's also possible to use characters sets with more than 62 characters in order
to achieve shorter identifiers for large numbers.
Legacy API
Base62.js v1.x's API is maintained for backwards compatibility.
var Base62 = require("base62");
Base62.encode(999);
Base62.decode("g7");
This uses the default ASCII character set for encoding/decoding.
It's also possible to define a custom character set instead:
var Base62 = require("base62");
var charset = "~9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-=";
Base62.setCharacterSet(charset);
Base62.encode(999);
Base62.decode("F3");
setCharacterSet
ensures that the respective string contains exactly 62 unique
characters.
Development
Source code is hosted on GitHub.
Please report issues or feature requests in
GitHub Issues.
Note on Patches/Pull Requests
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version
unintentionally.
- Send me a pull request. Bonus points for topic branches.
Release Process for Maintainers
- Update the version number in
package.json
. - Commit this change with the respective version number as commit message
(e.g. "1.2.3").
- Create a tag, using the prefixed version number (e.g.
git tag v1.2.3
). - Publish the new version:
git push --tags origin master
and npm publish
.
Copyright
Copyright (c) 2016 Andrew Nesbitt. See LICENSE for details.