npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

string64

v3.0.1

Published

Extremely fast number to radix64 converting

Downloads

37

Readme

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

| Number | Result | | ----------------- | ---------- | | 0 | $ | | 63 | z | | 64 | 0$ | | 4095 | zz | | 262143 | zzz | | 16777215 | zzzz | | 68719476735 | zzzzzz | | 281474976710655 | zzzzzzzz | | 9007199254740991 | Uzzzzzzzz | | -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)