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 for node.js
What is Base62 encoding?
From wikipedia:
Base62 is a priority system to the base 62, which for encoding large numbers using ASCII is character. The digits 0-9 (value 0-9), uppercase letters A-Z (value 10-35) and lowercase letters a-z (value 36-61) are used.
Due to the high number of base formed shorter strings than with the decimal or hexadecimal system , which mainly offers two advantages:
- They can be entered by a human being faster and with a smaller risk of error. In this case, a font should be selected in which characters that can be confused, such as small L and large i, or zero, and large O, are distinguishable.
- Length restrictions, eg when a number is to be used as part of an identifier or file name, can be bypassed. However, it should be noted that the processing system is case-sensitive.
Install
npm install base62
or for yarn:
yarn add base62
Usage
Default Character Set Example
Base62 = require('base62')
Base62.encode(999)
Base62.decode('g7')
Custom Character Set Example
The default character set is 0-9a-zA-Z
. This can be updated to a custom character set. Naturally, it must be 62 characters long.
Instead of the character set 0-9a-zA-Z
you want to use 0-9A-Za-z
, call the setCharacterSet()
method on the Base62 object passing in the string "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
. Note that all characters must be unique.
Base62 = require('base62')
Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
Base62.encode(999)
Base62.decode('G7')
Development
Source hosted at GitHub.
Report Issues/Feature requests on 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.
Copyright
Copyright (c) 2016 Andrew Nesbitt. See LICENSE for details.