3.0.1 • Published 5 years ago

string64 v3.0.1

Weekly downloads
22
License
MIT
Repository
github
Last release
5 years ago

string64

This is a fork from kutuluk/number-to-base64 with the following changes: This lib uses BigInt, and hence prerequisite is Node.js v10+

  • It provides a String64 class can be initialized with custom radix
  • Default 64 character set changed to use $ and _ instead of + and / to make strings web safe
  • Default character set has character sequence in increasing ascii value. i.e, as 65534 < 65535, their encoded strings will also respect the comparison "Ezy" < "Ezz" (for positive values). This makes default string64 encoding ideal for timeseries keys which must always increase
  • timestamp() function does exactly that, ie., generates timestamp as a compact string which is particularly useful when your keys cannot be number/bigint data type.

  • currently working for positive numbers only

Extremely fast number to radix64 converting.

NPM versionBuild Status

Features

  • Converts all values of javascript safe integers range (from -9007199254740991 to 9007199254740991)
  • Extremely fast due to bitwise operations
  • Does not add extra padding characters for more efficient compression
  • ES3 compatible
NumberResult
0\$
63z
640\$
4095zz
262143zzz
16777215zzzz
68719476735zzzzzz
281474976710655zzzzzzzz
9007199254740991Uzzzzzzzz
-9007199254740991-Uzzzzzzzz

Installation

npm install string64

API

String64(radix)

Initialize String64 object with optional custom radix. Default radix is '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'

Eg.,

const { String64 }= require('string64');

var str64 = new String64();

toString64(number)

Takes a number, discards a fractional part and returns a string.

Eg.,

str64.toString64(0) // "$"

toNumber(base64Encoded)

Takes a string and returns a number.

timestamp(randChars: int)

This is useful for generating time series keys. It encodes current time with microsecond accuracy. Optional randChars is the number of random characters that you wish to suffix to reduce collision possibility.

Usage

Browser directly

<script src="https://unpkg.com/string64/dist/string64.min.js"></script>

<script>
  var number = -9007199254740991;
  var base64 = String64().toString64(number);
  var back = String64().toNumber(base64);
  console.log('%s -> "%s" -> %s (%s)', number, base64, back, back === number);
</script>

Output

-9007199254740991 -> "-Uzzzzzzzz" -> -9007199254740991 (true)

ES6

import { String64 } from "string64";

const test = number => {
  var base64 = String64().toString64(number);
  var back = String64().toNumber(base64);
  console.log('%s -> "%s" -> %s (%s)', number, base64, back, back === number);
};

[
  0,
  1,
  -1,
  255,
  65535,
  4294967295,
  4294967296,
  Date.now(),
  9007199254740991
].forEach(number => test(number));

Output

0 -> "$" -> 0 (true)
1 -> "0" -> 1 (true)
-1 -> "-0" -> -1 (true)
255 -> "2z" -> 255 (true)
65535 -> "Ezz" -> 65535 (true)
4294967295 -> "2zzzzz" -> 4294967295 (true)
4294967296 -> "3$$$$$" -> 4294967296 (true)
9007199254740991 -> "Uzzzzzzzz" -> 9007199254740991 (true)
3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago