1.0.0 • Published 5 years ago
base62.io v1.0.0
base62.io
JavaScript port of tuupola/base62 which is used on base62.io.
Download
npm install base62.io
Basic Usage
String
const { encode, decode } = require('base62.io');
encode('Hello world!'); // BfClwpqbAZpeaVbeGIwSh3Djrgbvwbx
decode('BfClwpqbAZpeaVbeGIwSh3Djrgbvwbx'); // Hello world!
Int
const { encodeInt, decodeInt } = require('base62.io');
encodeInt(987654321); // 14q60P
decodeInt('14q60P'); // 987654321
Advanced Usage
const { createEncoder } = require('base62.io');
const { encode, decode } = createEncoder({
characters: '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
encoding: 'latin1',
});
encode('Hello world!'); // t9DGCJrgUyuUEwHT
decode('t9DGCJrgUyuUEwHT'); // Hello world!
Character encoding and compatibility with PHP
JavaScript's default character encoding is UTF-16 while PHP's is latin1. Because of this, data encoded in PHP is not compatible with data encoded in JavaScript. To preserve compatibility with the PHP library, set the character encoding to latin1
. This will only work if the string you are attempting to encode is only made up of latin characters.
const { createEncoder } = require('base62.io');
const { encode, decode } = createEncoder({
encoding: 'latin1',
});
encode('Hello world!'); // t9DGCJrgUyuUEwHT
decode('t9DGCJrgUyuUEwHT'); // Hello world!
1.0.0
5 years ago