1.0.2 • Published 7 years ago
base37-string v1.0.2
base37-string
Encode a string into a base-37 64-bit number
Installation
npm install base37-string --saveUsage
import { encode, decode } from "base37-string";
const encoded = encode("james"); // 0x00000000011F0598
const decoded = decode(encoded); // jamesAlgorithm
encode
- Take a string
x - Convert
xinto an array of character codesc[] - Initialise an accumulator
accas0 - Iterate through
c[]for individual character codesc:- If
65 <= c <= 90, letiequal(1 + c) - 65 - If
97 <= c <= 122, letiequal(1 + c) - 97 - If
48 <= c <= 57, letiequal(27 + c) - 48
- If
- Multiply
accby37and addi - While
accis not0and is evenly divisible by37, divide by37 - Return
acc
decode
- Take a long
xwhich must not:- be
<= 0 - be
>= 0x5b5b57f8a98a5dd1 - be evenly divisible by 37
- be
- Initialice an accumulator
accasx - Initialise an array
c[] - While
accis not0:- Store the value of
accasn - Divide
accby37 - let
iequaln - acc * 37 - get value at index
ifrom array of valid characters and push to front ofc[]
- Store the value of
- Join
c[]into string and return