2018-12-17 05:37:00 +01:00
/ * *
* @ author tcode2k16 [ tcode2k16 @ gmail . com ]
* @ copyright Crown Copyright 2018
* @ license Apache - 2.0
* /
import Operation from "../Operation" ;
import BigNumber from "bignumber.js" ;
import Utils from "../Utils" ;
/ * *
* From Base62 operation
* /
class FromBase62 extends Operation {
/ * *
* FromBase62 constructor
* /
constructor ( ) {
super ( ) ;
this . name = "From Base62" ;
this . module = "Default" ;
2018-12-18 13:19:42 +01:00
this . description = "Base62 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers. The high number base results in shorter strings than with the decimal or hexadecimal system." ;
this . infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems" ;
2018-12-17 05:37:00 +01:00
this . inputType = "string" ;
2018-12-18 13:19:42 +01:00
this . outputType = "byteArray" ;
this . args = [
{
name : "Alphabet" ,
type : "string" ,
value : "0-9A-Za-z"
}
] ;
2018-12-17 05:37:00 +01:00
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
2018-12-18 13:19:42 +01:00
* @ returns { byteArray }
2018-12-17 05:37:00 +01:00
* /
run ( input , args ) {
2018-12-18 13:19:42 +01:00
if ( input . length < 1 ) return [ ] ;
const ALPHABET = Utils . expandAlphRange ( args [ 0 ] ) . join ( "" ) ;
2018-12-17 05:37:00 +01:00
const BN = BigNumber . clone ( { ALPHABET } ) ;
const re = new RegExp ( "[^" + ALPHABET . replace ( /[[\]\\\-^$]/g , "\\$&" ) + "]" , "g" ) ;
input = input . replace ( re , "" ) ;
const number = new BN ( input , 62 ) ;
2018-12-18 13:19:42 +01:00
return Utils . convertToByteArray ( number . toString ( 16 ) , "Hex" ) ;
2018-12-17 05:37:00 +01:00
}
}
export default FromBase62 ;