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

ya-base62

v1.0.0

Published

Yet Another Base62, first and foremost designed to encode to and decode from structured strings.

Downloads

154

Readme

Yet Another Base62

Build Status NPM version

Installing and testing

With npm do:

npm install ya-base62

To run the test suite, run the following command from the ya-base62 directory:

npm test

Features

  • First and foremost designed to encode/decode multiple values to/from a structured string.
  • Comparatively fast decoding.
  • The library doesn't support big numbers (integers encoded on more than 51 bits, > Number.MAX_SAFE_INTEGER).
  • The library doesn't support custom charsets (it is hardcoded to 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ which is the default charset of most existing implementations).

Usage

Encoding a structured string

var yabe62 = require('ya-base62');

var width = 600,
    height = 480,
    value = 12093;

var structuredString = (
    yabe62.mbValueToBase62(width, 2) +
    yabe62.mbValueToBase62(height, 2) +
    yabe62.mbValueToBase62(value, 8)
); // '9G7K00000393'

Decoding a structured string

var yabe62 = require('ya-base62');

var structuredString = '9G7K00000393';

console.log(
    yabe62.mbValueFromBase62(structuredString, 0, 2),
    yabe62.mbValueFromBase62(structuredString, 2, 2),
    yabe62.mbValueFromBase62(structuredString, 4, 8)
); // 600 480 12093

Decoding and encoding unstructured string

var yabe62 = require('ya-base62');

console.log(yabe62.encode(1200050000)); // '1jdhPa'
console.log(yabe62.decode('1jdhPa')); // 1200050000

Public API

The module is exposing 6 pure functions.

sbValueToBase62(value)

Convert an integer value to a single byte base 62 value.

  • value : An integer in the [0-61] range.

sbValueFromBase62(string, position)

Convert a single byte base 62 value to an integer.

  • string : String containing the single byte base 62 value.
  • position : Position of the character to decode.

mbValueToBase62(value, length)

Convert an integer value to a multiple byte base 62 value.

  • value : An integer in the [0-(62 ** length - 1)] range.
  • length : The number of bytes.

mbValueFromBase62(string, position, length)

Convert a multiple byte base 62 value to an integer.

  • string : String containing the multiple byte base 62 value.
  • position : Start position of the characters to decode.
  • length : Number of bytes to decode.

encode(value)

Convert an integer to a base 62 value.

  • value : Positive integer to encode.

decode(string)

Convert the entirety of a given base 62 string to an integer.

  • string : String containing the base 62 value to decode.

Benchmark

It should be noted that this benchmark is slightly unfair. The slowness of the other module when encoding can be explained by the fact that they allow the use of custom charset.

| Operation | Module | Results | Time elapsed | | ---------- | ------ | ------- | ------------ | | decoding (1000000x) | [email protected] | correct | 97.88 ms | | decoding (1000000x) | [email protected] | correct | 2149.35 ms | | decoding (1000000x) | [email protected] | correct | 909.97 ms | | encoding (1000000x) | [email protected] | correct | 63.78 ms | | encoding (1000000x) | [email protected] | correct | 57.27 ms | | encoding (1000000x) | [email protected] | correct | 86.71 ms | | mbValueToBase62 on 10 characters (1000000x) | [email protected] | correct | 180.27 ms |

Benchmark code

Changelog

1.0.0 (2017.05.14) :

  • First implementation

License

MIT