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

basek

v1.0.5

Published

Use an array of multi-character alphanumeric symbols to convert bases exceeding 62

Downloads

12

Readme

basek.js

Use an array of multi-character alphanumeric symbols to convert between bases exceeding 62.

Features

  • Convert between a potentially infinite number of bases using an array representation of an alphabet.
  • Predefined methods for converting between life's most popular bases (2,6,8,..62)
  • Functionality that allows you to set your alphabet as a string or an array of strings
  • Create your own convenience methods for converting between base(n) and base(k)
  • Create a logically coherent chain of functions with the Basek object
    • set an alphabet -> convert to base -> pad with digits

Usage

Include in your Node.js project
var basek = require('basek');
Include as a standalone
<script src='basek.min.js'></script> // var basek = Basek()

=== The default alphabet is 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ which supports all base conversions up to 62.

Predefined convenience methods
basek.base10to2(15) // "1111"
basek.base10to16(15) // "f"
basek.base16to10("f") // "15"
basek.base2to10(1111) // "15" 
Defining a convenience method
basek.makeTag(2,16) 
// Basek.prototype.base2to16 and Basek.prototype.base16to2 are now defined
basek.base2to16(1111) // "f"
basek.base16to2("f") // "1111"
Generics
  • Basek.prototype.toBase(n[, base])
  • Basek.prototype.fromBase(n[, base])

Convert to/from decimal using these generics. If you don't provide a base parameter, Basek uses the length of your alphabet to determine the base.

basek.toBase(61).get() // "Z" 
basek.toBase(61,2).get() // "111110"
basek.fromBase("Z").get() // "61"
basek.fromBase("ff",16).get() // "255"
Alphabetic Representation
  • Basek.prototype.alpha()
  • Basek.prototype.alphaDefault()
  • Basek.prototype.alphaSet('abc' || ['a','b','c'])
  • Basek.prototype.alphaExtend('abc' || ['a','b','c'])

You can literally use any combination of multi-character symbols to extend Basek's alphabetic representation and thus extending your base limit. First here's some basic usage.

// Using only single character symbols
basek.alpha() 
// '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - default base62
basek.alphaExtend('%^') 
// '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%^' - base64
basek.alphaSet() 
// '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - back to default
basek.alphaSet('abcdefghijklmnopqrstuvwzyz')
// 'abcdefghijklmnopqrstuvwzyz' - alphabet set
basek.toBase(15,2).get() // 'bbbb'

Now using multi-character alphanumeric symbols to extend Basek's base range to 100

basek.alphaSet() // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - back to default
for (var i = 0; i < 38; i++) {
  basek.alphaExtend(['a'+i]);
} // alphabet set to Array[100] (['0','1',...,'a37'])
basek.toBase(99).get() // 'a37' - your multi-character symbol representing decimal 99 in base100
Utility (more to come)
  • Basek.prototype.pad(bits)
  • Basek.prototype.unpad()

Use pad to prepend n bits (bit = character at the 0th index of your alphabet) to your string and unpad to slice the representation back to its original glory.

basek.alphaSet()
basek.toBase(15,2) // '1111'
basek.pad(10) // '0000001111'
basek.unpad() // '1111'

Chain several functions together

basek.alphaSet('ab').toBase(10).pad(10).get() // 'aaaaaababa'
License

MIT