base37-string
v1.0.2
Published
Encode a string into a base-37 64-bit number
Downloads
12
Maintainers
Readme
base37-string
Encode a string into a base-37 64-bit number
Installation
npm install base37-string --save
Usage
import { encode, decode } from "base37-string";
const encoded = encode("james"); // 0x00000000011F0598
const decoded = decode(encoded); // james
Algorithm
encode
- Take a string
x
- Convert
x
into an array of character codesc[]
- Initialise an accumulator
acc
as0
- Iterate through
c[]
for individual character codesc
:- If
65 <= c <= 90
, leti
equal(1 + c) - 65
- If
97 <= c <= 122
, leti
equal(1 + c) - 97
- If
48 <= c <= 57
, leti
equal(27 + c) - 48
- If
- Multiply
acc
by37
and addi
- While
acc
is not0
and is evenly divisible by37
, divide by37
- Return
acc
decode
- Take a long
x
which must not:- be
<= 0
- be
>= 0x5b5b57f8a98a5dd1
- be evenly divisible by 37
- be
- Initialice an accumulator
acc
asx
- Initialise an array
c[]
- While
acc
is not0
:- Store the value of
acc
asn
- Divide
acc
by37
- let
i
equaln - acc * 37
- get value at index
i
from array of valid characters and push to front ofc[]
- Store the value of
- Join
c[]
into string and return