2018-12-17 12:37:00 +08:00
/**
* @author tcode2k16 [tcode2k16@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
2018-12-17 12:37:00 +08:00
import BigNumber from "bignumber.js" ;
2019-07-09 12:23:59 +01:00
import Utils from "../Utils.mjs" ;
import { toHexFast } from "../lib/Hex.mjs" ;
2018-12-17 12:37:00 +08:00
/**
* To Base62 operation
*/
class ToBase62 extends Operation {
/**
* ToBase62 constructor
*/
constructor () {
super ();
this . name = "To Base62" ;
this . module = "Default" ;
2018-12-18 12:19:42 +00: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." ;
2018-12-18 13:50:10 +00:00
this . infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems" ;
2018-12-18 12:19:42 +00:00
this . inputType = "byteArray" ;
2018-12-17 12:37:00 +08:00
this . outputType = "string" ;
2018-12-18 12:19:42 +00:00
this . args = [
{
name : "Alphabet" ,
type : "string" ,
value : "0-9A-Za-z"
}
];
2018-12-17 12:37:00 +08:00
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run ( input , args ) {
if ( input . length < 1 ) return "" ;
2018-12-18 12:19:42 +00:00
const ALPHABET = Utils . expandAlphRange ( args [ 0 ]). join ( "" );
2018-12-17 12:37:00 +08:00
const BN = BigNumber . clone ({ ALPHABET });
2018-12-18 12:19:42 +00:00
input = toHexFast ( input ). toUpperCase ();
2018-12-17 12:37:00 +08:00
const number = new BN ( input , 16 );
return number . toString ( 62 );
}
}
export default ToBase62 ;