Skip to content

SeanCannon/aybabtu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aybabtu

All Your Base Are Belong To Us

A simple and convenient API for converting data between mathematical bases.

Build Status npm version Dependency Status Coverage Status

Disclaimer

Use of this library is quite silly since all it is really doing is this:

parseInt(myValue, baseFrom).toString(baseTo)

Why use aybabtu then?

  • Your app could always use another dependency
  • 31,200% unit test coverage (pay no attention to the coveralls badge)
  • Magic constants like 16 (base hex) and 10 (base dec) can confuse junior devs
  • Because parseInt and toString should never be seen together so we hide them for you
  • Somebody set up us the bomb.

Limitations

32-bit limitation

JavaScript caps bitwise operator support at 32 bits, so giving aybabtu a value larger than that won't work.

A simple workaround is to break up your value into 32 bit chunks, for example:

const tooLargeBinary = '11111011000001010011100101111001010001110011100011011010';
const wrongValue     = base.bin2hex(tooLargeBinary); // "fb0539794738d8"
const rightValue     = tooLargeBinary.match(/.{1,32}/g).map(base.bin2hex).join(''); // "fb0539794738da"

Install

$ npm install aybabtu --save

Run the specs

$ npm test

Usage

Using quick methods
const base = require('aybabtu');

base.dec2hex('42');  // '2a'
Using the master converter
const { allYour } = require('aybabtu');

allYour('decimal').areBelongTo('binary')(50);  // '110010'

Supported Bases

Binary: Base 2

Convert to Ternary

String → String

expect(bin2ter('110010')).toBe('1212');

Convert to Quaternary

String → String

expect(bin2qua('110010')).toBe('302');

Convert to Quinary

String → String

expect(bin2qui('110010')).toBe('200');

Convert to Senary

String → String

expect(bin2sen('110010')).toBe('122');

Convert to Septenary

String → String

expect(bin2sep('110010')).toBe('101');

Convert to Octal

String → String

expect(bin2oct('110010')).toBe('62');

Convert to Nonary

String → String

expect(bin2non('110010')).toBe('55');

Convert to Decimal

String → String

expect(bin2dec('110010')).toBe('50');

Convert to Undenary

String → String

expect(bin2und('110010')).toBe('46');

Convert to Duodecimal

String → String

expect(bin2duo('110010')).toBe('42');

Convert to Hexadecimal

String → String

expect(bin2hex('110010')).toBe('32');

Convert to Vigesimal

String → String

expect(bin2vig('110010')).toBe('2a');

Ternary: Base 3

Convert to Binary

String → String

expect(ter2bin('1212')).toBe('110010');

Convert to Quaternary

String → String

expect(ter2qua('1212')).toBe('302');

Convert to Quinary

String → String

expect(ter2qui('1212')).toBe('200');

Convert to Senary

String → String

expect(ter2sen('1212')).toBe('122');

Convert to Septenary

String → String

expect(ter2sep('1212')).toBe('101');

Convert to Octal

String → String

expect(ter2oct('1212')).toBe('62');

Convert to Nonary

String → String

expect(ter2non('1212')).toBe('55');

Convert to Decimal

String → String

expect(ter2dec('1212')).toBe('50');

Convert to Undenary

String → String

expect(ter2und('1212')).toBe('46');

Convert to Duodecimal

String → String

expect(ter2duo('1212')).toBe('42');

Convert to Hexadecimal

String → String

expect(ter2hex('1212')).toBe('32');

Convert to Vigesimal

String → String

expect(ter2vig('1212')).toBe('2a');

Quaternary: Base 4

Convert to Binary

String → String

expect(qua2bin('302')).toBe('110010');

Convert to Ternary

String → String

expect(qua2ter('302')).toBe('1212');

Convert to Quinary

String → String

expect(qua2qui('302')).toBe('200');

Convert to Senary

String → String

expect(qua2sen('302')).toBe('122');

Convert to Septenary

String → String

expect(qua2sep('302')).toBe('101');

Convert to Octal

String → String

expect(qua2oct('302')).toBe('62');

Convert to Nonary

String → String

expect(qua2non('302')).toBe('55');

Convert to Decimal

String → String

expect(qua2dec('302')).toBe('50');

Convert to Undenary

String → String

expect(qua2und('302')).toBe('46');

Convert to Duodecimal

String → String

expect(qua2duo('302')).toBe('42');

Convert to Hexadecimal

String → String

expect(qua2hex('302')).toBe('32');

Convert to Vigesimal

String → String

expect(qua2vig('302')).toBe('2a');

Quinary: Base 5

Convert to Binary

String → String

expect(qui2bin('200')).toBe('110010');

Convert to Ternary

String → String

expect(qui2ter('200')).toBe('1212');

Convert to Quaternary

String → String

expect(qui2qua('200')).toBe('302');

Convert to Senary

String → String

expect(qui2sen('200')).toBe('122');

Convert to Septenary

String → String

expect(qui2sep('200')).toBe('101');

Convert to Octal

String → String

expect(qui2oct('200')).toBe('62');

Convert to Nonary

String → String

expect(qui2non('200')).toBe('55');

Convert to Decimal

String → String

expect(qui2dec('200')).toBe('50');

Convert to Undenary

String → String

expect(qui2und('200')).toBe('46');

Convert to Duodecimal

String → String

expect(qui2duo('200')).toBe('42');

Convert to Hexadecimal

String → String

expect(qui2hex('200')).toBe('32');

Convert to Vigesimal

String → String

expect(qui2vig('200')).toBe('2a');

Senary: Base 6

Convert to Binary

String → String

expect(sen2bin('122')).toBe('110010');

Convert to Ternary

String → String

expect(sen2ter('122')).toBe('1212');

Convert to Quaternary

String → String

expect(sen2qua('122')).toBe('302');

Convert to Quinary

String → String

expect(sen2qui('122')).toBe('200');

Convert to Septenary

String → String

expect(sen2sep('122')).toBe('101');

Convert to Octal

String → String

expect(sen2oct('122')).toBe('62');

Convert to Nonary

String → String

expect(sen2non('122')).toBe('55');

Convert to Decimal

String → String

expect(sen2dec('122')).toBe('50');

Convert to Undenary

String → String

expect(sen2und('122')).toBe('46');

Convert to Duodecimal

String → String

expect(sen2duo('122')).toBe('42');

Convert to Hexadecimal

String → String

expect(sen2hex('122')).toBe('32');

Convert to Vigesimal

String → String

expect(sen2vig('122')).toBe('2a');

Septenary: Base 7

Convert to Binary

String → String

expect(sep2bin('101')).toBe('110010');

Convert to Ternary

String → String

expect(sep2ter('101')).toBe('1212');

Convert to Quaternary

String → String

expect(sep2qua('101')).toBe('302');

Convert to Quinary

String → String

expect(sep2qui('101')).toBe('200');

Convert to Senary

String → String

expect(sep2sen('101')).toBe('122');

Convert to Octal

String → String

expect(sep2oct('101')).toBe('62');

Convert to Nonary

String → String

expect(sep2non('101')).toBe('55');

Convert to Decimal

String → String

expect(sep2dec('101')).toBe('50');

Convert to Undenary

String → String

expect(sep2und('101')).toBe('46');

Convert to Duodecimal

String → String

expect(sep2duo('101')).toBe('42');

Convert to Hexadecimal

String → String

expect(sep2hex('101')).toBe('32');

Convert to Vigesimal

String → String

expect(sep2vig('101')).toBe('2a');

Octal: Base 8

Convert to Binary

String → String

expect(oct2bin('62')).toBe('110010');

Convert to Ternary

String → String

expect(oct2ter('62')).toBe('1212');

Convert to Quaternary

String → String

expect(oct2qua('62')).toBe('302');

Convert to Quinary

String → String

expect(oct2qui('62')).toBe('200');

Convert to Senary

String → String

expect(oct2sen('62')).toBe('122');

Convert to Septenary

String → String

expect(oct2sep('62')).toBe('101');

Convert to Nonary

String → String

expect(oct2non('62')).toBe('55');

Convert to Decimal

String → String

expect(oct2dec('62')).toBe('50');

Convert to Undenary

String → String

expect(oct2und('62')).toBe('46');

Convert to Duodecimal

String → String

expect(oct2duo('62')).toBe('42');

Convert to Hexadecimal

String → String

expect(oct2hex('62')).toBe('32');

Convert to Vigesimal

String → String

expect(oct2vig('62')).toBe('2a');

Nonary: Base 9

Convert to Binary

String → String

expect(non2bin('55')).toBe('110010');

Convert to Ternary

String → String

expect(non2ter('55')).toBe('1212');

Convert to Quaternary

String → String

expect(non2qua('55')).toBe('302');

Convert to Quinary

String → String

expect(non2qui('55')).toBe('200');

Convert to Senary

String → String

expect(non2sen('55')).toBe('122');

Convert to Septenary

String → String

expect(non2sep('55')).toBe('101');

Convert to Octal

String → String

expect(non2oct('55')).toBe('62');

Convert to Decimal

String → String

expect(non2dec('55')).toBe('50');

Convert to Undenary

String → String

expect(non2und('55')).toBe('46');

Convert to Duodecimal

String → String

expect(non2duo('55')).toBe('42');

Convert to Hexadecimal

String → String

expect(non2hex('55')).toBe('32');

Convert to Vigesimal

String → String

expect(non2vig('55')).toBe('2a');

Decimal: Base 10

Convert to Binary

String → String

expect(dec2bin('50')).toBe('110010');

Convert to Ternary

String → String

expect(dec2ter('50')).toBe('1212');

Convert to Quaternary

String → String

expect(dec2qua('50')).toBe('302');

Convert to Quinary

String → String

expect(dec2qui('50')).toBe('200');

Convert to Senary

String → String

expect(dec2sen('50')).toBe('122');

Convert to Septenary

String → String

expect(dec2sep('50')).toBe('101');

Convert to Octal

String → String

expect(dec2oct('50')).toBe('62');

Convert to Nonary

String → String

expect(dec2non('50')).toBe('55');

Convert to Undenary

String → String

expect(dec2und('50')).toBe('46');

Convert to Duodecimal

String → String

expect(dec2duo('50')).toBe('42');

Convert to Hexadecimal

String → String

expect(dec2hex('50')).toBe('32');

Convert to Vigesimal

String → String

expect(dec2vig('50')).toBe('2a');

Undenary: Base 11

Convert to Binary

String → String

expect(und2bin('46')).toBe('110010');

Convert to Ternary

String → String

expect(und2ter('46')).toBe('1212');

Convert to Quaternary

String → String

expect(und2qua('46')).toBe('302');

Convert to Quinary

String → String

expect(und2qui('46')).toBe('200');

Convert to Senary

String → String

expect(und2sen('46')).toBe('122');

Convert to Septenary

String → String

expect(und2sep('46')).toBe('101');

Convert to Octal

String → String

expect(und2oct('46')).toBe('62');

Convert to Nonary

String → String

expect(und2non('46')).toBe('55');

Convert to Decimal

String → String

expect(und2dec('46')).toBe('50');

Convert to Duodecimal

String → String

expect(und2duo('46')).toBe('42');

Convert to Hexadecimal

String → String

expect(und2hex('46')).toBe('32');

Convert to Vigesimal

String → String

expect(und2vig('46')).toBe('2a');

Duodecimal: Base 12

Convert to Binary

String → String

expect(duo2bin('42')).toBe('110010');

Convert to Ternary

String → String

expect(duo2ter('42')).toBe('1212');

Convert to Quaternary

String → String

expect(duo2qua('42')).toBe('302');

Convert to Quinary

String → String

expect(duo2qui('42')).toBe('200');

Convert to Senary

String → String

expect(duo2sen('42')).toBe('122');

Convert to Septenary

String → String

expect(duo2sep('42')).toBe('101');

Convert to Octal

String → String

expect(duo2oct('42')).toBe('62');

Convert to Nonary

String → String

expect(duo2non('42')).toBe('55');

Convert to Decimal

String → String

expect(duo2dec('42')).toBe('50');

Convert to Undenary

String → String

expect(duo2und('42')).toBe('46');

Convert to Hexadecimal

String → String

expect(duo2hex('42')).toBe('32');

Convert to Vigesimal

String → String

expect(duo2vig('42')).toBe('2a');

Hexadecimal: Base 16

Convert to Binary

String → String

expect(hex2bin('32')).toBe('110010');

Convert to Ternary

String → String

expect(hex2ter('32')).toBe('1212');

Convert to Quaternary

String → String

expect(hex2qua('32')).toBe('302');

Convert to Quinary

String → String

expect(hex2qui('32')).toBe('200');

Convert to Senary

String → String

expect(hex2sen('32')).toBe('122');

Convert to Septenary

String → String

expect(hex2sep('32')).toBe('101');

Convert to Octal

String → String

expect(hex2oct('32')).toBe('62');

Convert to Nonary

String → String

expect(hex2non('32')).toBe('55');

Convert to Decimal

String → String

expect(hex2dec('32')).toBe('50');

Convert to Undenary

String → String

expect(hex2und('32')).toBe('46');

Convert to Duodecimal

String → String

expect(hex2duo('32')).toBe('42');

Convert to Vigesimal

String → String

expect(hex2vig('32')).toBe('2a');

Vigesimal: Base 20

Convert to Binary

String → String

expect(vig2bin('2a')).toBe('110010');

Convert to Ternary

String → String

expect(vig2ter('2a')).toBe('1212');

Convert to Quaternary

String → String

expect(vig2qua('2a')).toBe('302');

Convert to Quinary

String → String

expect(vig2qui('2a')).toBe('200');

Convert to Senary

String → String

expect(vig2sen('2a')).toBe('122');

Convert to Septenary

String → String

expect(vig2sep('2a')).toBe('101');

Convert to Octal

String → String

expect(vig2oct('2a')).toBe('62');

Convert to Nonary

String → String

expect(vig2non('2a')).toBe('55');

Convert to Decimal

String → String

expect(vig2dec('2a')).toBe('50');

Convert to Undenary

String → String

expect(vig2und('2a')).toBe('46');

Convert to Duodecimal

String → String

expect(vig2duo('2a')).toBe('42');

Convert to Hexadecimal

String → String

expect(vig2hex('2a')).toBe('32');