1.0.1 • Published 2 years ago

short64 v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

short64 : Shorter base64 encoding for integers

short64 facilitates encoding and decoding integers as shorter as possible strings.

Works on browsers and nodeJS.

Installation

Install from NPM

npm i short64

Usage - with ES6 modules

import short64 from "short64";

let inputInt = 123;
let short64string = short64.From(inputInt);
let decodedInt = short64.ToInt(short64string);

console.log({
  inputInt,
  short64string,
  decodedInt,
});

Output

{inputInt: 123, short64string: 'B7', decodedInt: 123}

Usage - with common JS

Access the default property of the module.

let short64 = require("short64").default;

let inputInt = 123;
let short64string = short64.From(inputInt);
let decodedInt = short64.ToInt(short64string);

console.log({
  inputInt,
  short64string,
  decodedInt,
});

Output

{inputInt: 123, short64string: 'B7', decodedInt: 123}

Usage - custom alphabet

import short64 from "short64";

short64.SetAlphabet(
  "A~CDEFGHIJKLMNOPQRSTUVWXYZ$%^&efghijklmnopqrstuvwxyz0987654321*#"
);

let inputInt = 123;
let short64string = short64.From(inputInt);
let decodedInt = short64.ToInt(short64string);

console.log({
  inputInt,
  short64string,
  decodedInt,
});

Output :

{inputInt: 123, short64string: '~3', decodedInt: 123}

Default alphabet is

SetAlphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-");

Base64 friendly alphabet is

SetAlphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");

Sample outputs

Input integerdigitsshort64short64 lengthbase64 (btoa)base64 length
01'A'1'MA=='4
11'B'1'MQ=='4
91'J'1'OQ=='4
442's'1'NDQ='4
1003'Bk'2'MTAw'4
20004'fQ'2'MjAwMA=='8
31354'w-'2'MzEzNQ=='8
40954'--'2'NDA5NQ=='8
1536636'lg-'3'MTUzNjYz'8
2621436'---'3'MjYyMTQz'8
75295357'cuQ-'4'NzUyOTUzNQ=='12
167772158'----'4'MTY3NzcyMTU='12
3689472639'V-bA-'5'MzY4OTQ3MjYz'12
107374182310'-----'5'MTA3Mzc0MTgyMw=='16
1807841593511'Q1jrw-'6'MTgwNzg0MTU5MzU='16
6871947673511'------'6'Njg3MTk0NzY3MzU='16
88584238086312'M5AUgg-'7'ODg1ODQyMzgwODYz'16
439804651110313'-------'7'NDM5ODA0NjUxMTEwMw=='20
900719925474099116'f--------'9'OTAwNzE5OTI1NDc0MDk5MQ=='24