0.1.0 • Published 2 months ago

baseint v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

BaseInt

Encode and decode strings to a custom base

Inspired by / derivative of base62.js and Base-x

WARNING: This module is NOT RFC4648 compliant (formerly RFC3548). Meaning base64 and base64url will not encode/decode the same as btoa() and atob() or any of the native encodings (base16, base32)

Installation

npm install baseint

Usage

import BaseInt, {
  BASE64URL,
  BASE64,
  BASE62,
  BASE32,
  BASE32HEX,
  BASE16
} from "baseint";

// Supply which base you want to use (BASE62 is default)
const baseInt = new BaseInt(BASE62);

// Or supply your own base
const charset = "abcd1234";
const baseInt = new BaseInt(charset);

const encoded = baseInt.encode(numstr);
const decoded = baseInt.decode(encoded);

UUID Example

const baseInt = new BaseInt();

const uuid = "b9b03417-a52a-47cf-8638-3c26b2628c98";

//  Convert uuid to bigint/number/numeric string
const bn = BigInt("0x" + uuid.replace(/-/g, ""));

const encoded = baseInt.encode(bn);
// FoYGiVxbLcGdqtB3H0Qzbi

//  Or encode directly
const encoded = baseInt.encodeUUID(uuid);
// FoYGiVxbLcGdqtB3H0Qzbi

const decoded = baseInt.decodeToUUID(encoded);
// b9b03417-a52a-47cf-8638-3c26b2628c98

Predefined Bases / Character Sets

BaseCharacters
BASE64URLABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
BASE64ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
BASE62ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
BASE360123456789abcdefghijklmnopqrstuvwxyz
BASE32Zybndrfg8ejkmcpqxot1uwisza345h769
BASE32HEX0123456789ABCDEFGHIJKLMNOPQRSTUV
BASE32ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
BASE160123456789ABCDEF
BASE801234567
BASE201

License

MIT