hashnum
A small library to shorten array of positive numbers or zero into a string of characters.
Demo: https://pynnl.github.io/hashnum
Install
npm i -S hashnum
Usage
Hashnum // '1meSQKbq1'Hashnum // [12, 3, 45, 6, 78, 9]
encode
must take an array of unsigned numbers or strings which can be parsed into number.
If invalid input is passed, an empty string will be returned.
Hashnum // '1meSQKbq1'Hashnum // ''Hashnum // ''Hashnum // ''
decode
must take a string as first argument. If the string is an invalid encoded string,
an empty array will be returned.
Hashnum // []Hashnum // []
decode
can take a second boolean
argument to output number as string.
This is very useful for big numbers, where native type cannot handle.
Hashnum // ['12', '3', '45', '6', '78', '9'] const str = Hashnum // 'ddi3jfQBbX1TNFq'Hashnum // [98765432100123460000]Hashnum // ['98765432100123456789']
Custom table
Using custom table of characters by creating a new instance of the library.
const hashnum = table: '!@#$%^&*()' strOutput: true// ORconst hashnum = hashnumtable = '!@#$%^&*()'hashnumstrOutput = true//hashnum // '&^%@&)%%)((()(^&(*'hashnum // ['12', '3', '45', '6', '78', '9']
table
- the table of characters. Must contains at least 5 unique characters, or else
the default table will be used.
const hashnum = table: '!@#$%' hashnumtable == Hashnumtable // true - [0-9][a-z][A-Z]
strOutput
- the flag to output decoded number as string. It is overrided
if explicitly being set on calling decoding.
const hashnum = strOutput: true hashnum // [12, 3, 45, 6, 78, 9]hashnum // ['12', '3', '45', '6', '78', '9']
Dependencies
This library uses following third party libraries:
- JSBI - Apache-2.0.
License
This library is under MIT license.