-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const is = require('./is'); | ||
const Big = require('big.js'); | ||
|
||
const defaultDigits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
|
||
/** | ||
* co | ||
* @param {*} num | ||
* @param {number} from | ||
* @returns {Big} | ||
*/ | ||
function _convertToDecimalist(num, from) { | ||
let len = num.length; | ||
let dec = new Big(0); | ||
let base = new Big(from); | ||
for (let i = 0; i < len; i++) { | ||
let pos = defaultDigits.indexOf(num[i]); | ||
if (pos === -1) { | ||
throw new Error('Invalid number character: ' + num[i]); | ||
} | ||
dec = base.pow(len - i - 1).times(pos).plus(dec); | ||
} | ||
return dec; | ||
} | ||
|
||
/** | ||
* @param {Big} num | ||
* @param {number} to | ||
* @param {string} digits | ||
* @returns {string} | ||
*/ | ||
function _convertDecimalist(num, to, digits) { | ||
let res = ''; | ||
let base = new Big(to); | ||
while (num.gt(0)) { | ||
let mod = num.mod(base); | ||
res = digits.charAt(mod) + res; | ||
num = num.div(base).round(0, 0); | ||
} | ||
return res; | ||
} | ||
|
||
/** | ||
* @param {number|string} num | ||
* @param {number} from | ||
* @param {number} to | ||
* @param {{digits:string,patch:string,length?:number}} options | ||
* @returns {string} | ||
*/ | ||
function _convertNumber(num, from, to, options = {}) { | ||
if (to > 62) { | ||
throw new Error('target base must be less than 62'); | ||
} | ||
if (!is.string(num)) { | ||
num = `${num}`; | ||
} | ||
const digits = options.digits || defaultDigits; | ||
const patch = options.patch || defaultDigits; | ||
const { length } = options; | ||
let n = from !== 10 ? _convertToDecimalist(num, from) : new Big(num); | ||
let res = _convertDecimalist(n, to, digits); | ||
if (typeof length !== 'undefined' && res.length < length) { | ||
res = patch.charAt(0).repeat(length - res.length) + res; | ||
} | ||
// console.log({ n, to, digits }); | ||
return res; | ||
} | ||
|
||
module.exports = { | ||
_convertNumber | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const _convertNumber = require('../src/helper/convert')._convertNumber; | ||
|
||
const digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; | ||
|
||
describe('convert test case', function () { | ||
it('convert to decimalist', function () { | ||
expect(_convertNumber(61, 10, 62)).to.be.equal('Z'); | ||
expect(_convertNumber(61, 10, 62, { digits })).to.be.equal('z'); | ||
expect(_convertNumber('z', 62, 10)).to.be.equal('35'); | ||
expect(_convertNumber('z', 62, 10, { digits })).to.be.equal('DF'); | ||
expect(_convertNumber('z', 62, 10, { digits, length: 4, patch: '0' })).to.be.equal('00DF'); | ||
expect(_convertNumber('ZZZZZZ', 62, 10)).to.be.equal('56800235583'); | ||
}); | ||
|
||
it('throw error', function () { | ||
expect(() => _convertNumber('$', 62, 10)).to.throw('Invalid number character: $'); | ||
expect(() => _convertNumber('z', 62, 63)).to.throw('target base must be less than 62'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters