multi-base-number
What
Small class to build a number system based on a pattern
and some symbols
. There is no requirement for a pattern to have numbers of the same base, which makes it neat for interpreting identifiers as an ordered system. As long as you can describe your string with a consistent pattern, you could use this library to treat that string as a multi base number.
The core library is short and only provides the very basics of a number system. Things like pattern enforcement are not implemented (but could be via extending the class to suit your use case).
Usage
Can be used to poorly reinvent the wheel:
const MBN = let pattern = 'bbbb'let symbols = '01' let nibble = pattern symbols console // '1110'console // 14console // '1111'
Numbers are cyclical:
let one = '0001' let current = '0000'let results = for let i = 0; i <= 16; i++ results current = nibble console/*[ '0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111', '0000']*/ console // '1111'console // '0000'console // '1111'console // '0000'
Paterns do not need to use the same base:
const MBN = let pattern = 'LL-NN-LL'// each unique pattern token is expected to have a corresponsing symbol array let symbols = 'ABCDEF' // the symbol array should specify its values in ascending order. A = 0 '-' // the symbol array is expected to be in the same order as the pattern '1234' // the symbol array can have mixes characters let myNumber = pattern symbols console // 6 * 6 * 1 * 4 * 4 * 1 * 6 * 6 = 20,736console // AA-11-AAconsole // FF-44-FF console // AA-11-AAconsole // 0 console // FF-44-FFconsole // 20,735 console // 7,165console // 12,551 console // 19,716console // FE-14-EAconsole // 19,716