2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Numerical base operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const Base = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
DEFAULT_RADIX: 36,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* To Base operation.
|
|
|
|
*
|
|
|
|
* @param {number} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runTo: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
if (!input) {
|
|
|
|
throw ("Error: Input must be a number");
|
|
|
|
}
|
|
|
|
var radix = args[0] || Base.DEFAULT_RADIX;
|
|
|
|
if (radix < 2 || radix > 36) {
|
|
|
|
throw "Error: Radix argument must be between 2 and 36";
|
|
|
|
}
|
|
|
|
return input.toString(radix);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* From Base operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runFrom: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var radix = args[0] || Base.DEFAULT_RADIX;
|
|
|
|
if (radix < 2 || radix > 36) {
|
|
|
|
throw "Error: Radix argument must be between 2 and 36";
|
|
|
|
}
|
|
|
|
return parseInt(input.replace(/\s/g, ""), radix);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default Base;
|