JavaScript port of tuupola/base62 which is used on base62.io.
npm install base62.io
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
const { createEncoder } = require('base62.io');
const { encode, decode } = createEncoder({
characters: '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
encoding: 'latin1',
});
encode('Hello world!'); // t9DGCJrgUyuUEwHT
decode('t9DGCJrgUyuUEwHT'); // Hello world!
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!