number-converter-alphabet
Convert a number to any base/any custom alphabet.
Why
When you need to convert a number to a custom base/alphabet, this comes in handy.
For instance, for Excel style column names, you can count from 0
to n
and
get the representation of the number as a column name: "AZ" "BA" easily.
Install
$ npm install --save number-converter-alphabet
Usage
; console;// Output: DEADBEEFconsole// Output: 11011110101011011011111011101111console// Output: 3132223123323233 console;// Output: pong|tok|pong|ping|pong|tok|pong|ping|pong|ping|
Implicit leading zero
In our number world, ...00001 === 1
, we don't think of the leading zeros.
Same for hexadecimal : ...000FF === FF
.
But sometimes, you want those to be different. For instance, in Excel, the column name are using the normal alphabet, and after "Z", there is "AA", not "BA".
You can pass an option to control this behavior: implicitLeadingZero
(default:
true
).
; console;// Output: baconsole;// Output: aa console;// Output: 26console;// Output: 6console;// Output: 06console;// Output: 16
Generator
For commodity, there is a also a generator function that is available, to automatically increment the value it outputs :
; const gen = ; // 'a'; // 'b' const gen = ;; // 'z'; // 'aa'