hashnum
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.7-0 • Public • Published

    hashnum

    npm version Build Status

    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
    
    import Hashnum from 'hashnum'

    Usage

    Hashnum.encode([12, 3, 45, 6, 78, 9]) // '1meSQKbq1'
    Hashnum.decode('1meSQKbq1')           // [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.encode([12, 3, 45, '6', '78', '9']) // '1meSQKbq1'
    Hashnum.encode(['a']) // ''
    Hashnum.encode([-1])  // ''
    Hashnum.encode()      // ''

    decode must take a string as first argument. If the string is an invalid encoded string, an empty array will be returned.

    Hashnum.decode('*1meSQKbq1') // []
    Hashnum.decode()             // []

    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.decode('1meSQKbq1', true) // ['12', '3', '45', '6', '78', '9']
     
    const str = Hashnum.encode(['98765432100123456789']) // 'ddi3jfQBbX1TNFq'
    Hashnum.decode(str)        // [98765432100123460000]
    Hashnum.decode(str, true)  // ['98765432100123456789']

    Custom table

    Using custom table of characters by creating a new instance of the library.

    const hashnum = new Hashnum({
      table: '!@#$%^&*()',
      strOutput: true,
    })
    // OR
    const hashnum = new Hashnum()
    hashnum.table = '!@#$%^&*()'
    hashnum.strOutput = true
    //
    hashnum.encode([12, 3, 45, 6, 78, 9]) // '&^%@&)%%)((()(^&(*'
    hashnum.decode('&^%@&)%%)((()(^&(*')  // ['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 = new Hashnum({ table: '!@#$%' })
    hashnum.table == Hashnum.table // 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 = new Hashnum({ strOutput: true })
    hashnum.decode('1meSQKbq1', false) // [12, 3, 45, 6, 78, 9]
    hashnum.decode('1meSQKbq1')        // ['12', '3', '45', '6', '78', '9']

    Dependencies

    This library uses following third party libraries:

    License

    This library is under MIT license.

    Install

    npm i hashnum

    DownloadsWeekly Downloads

    2

    Version

    1.0.7-0

    License

    MIT

    Unpacked Size

    13.1 kB

    Total Files

    9

    Last publish

    Collaborators

    • pynnl