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 = ;
Include as a standalone
// var basek = Basek()
===
The default alphabet is 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
which supports all base conversions up to 62.
Predefined convenience methods
basek // "1111"basek // "f"basek // "15"basek // "15"
Defining a convenience method
basek // Basek.prototype.base2to16 and Basek.prototype.base16to2 are now definedbasek // "f"basek // "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 // "Z" basek // "111110"basek // "61"basek // "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 symbolsbasek // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - default base62basek // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%^' - base64basek // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - back to defaultbasek// 'abcdefghijklmnopqrstuvwzyz' - alphabet setbasek // 'bbbb'
Now using multi-character alphanumeric symbols to extend Basek's base range to 100
basek // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - back to defaultfor var i = 0; i < 38; i++ basek; // alphabet set to Array[100] (['0','1',...,'a37'])basek // '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.
basekbasek // '1111'basek // '0000001111'basek // '1111'
Chain several functions together
basek // 'aaaaaababa'
License
MIT