Skip to content

Commit

Permalink
feat: convert helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AxiosLeo committed Feb 23, 2024
1 parent 235c1d0 commit 73b6339
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ const fs = require('./src/helper/fs');
const obj = require('./src/helper/obj');
const cmd = require('./src/helper/cmd');
const is = require('./src/helper/is');
const convert = require('./src/helper/convert');
const helper = {
str,
fs,
obj,
cmd,
is
is,
convert
};

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"license": "MIT",
"dependencies": {
"big.js": "^6.2.1",
"clone-deep": "^4.0.1",
"colors": "1.4.0",
"enquirer": "^2.3.6",
Expand Down
73 changes: 73 additions & 0 deletions src/helper/convert.js
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
};
22 changes: 22 additions & 0 deletions tests/convert.tests.js
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');
});
});
2 changes: 1 addition & 1 deletion tests/fs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('fs test case', function () {
expect(files3.length).to.be.equal(0);

let files4 = await _list(__dirname, false, '.js');
expect(files4.length).to.be.equal(10);
expect(files4.length).to.be.equal(11);

let files5 = await _list(__dirname + '/not-exist/dir/');
expect(files5.length).to.be.equal(0);
Expand Down

0 comments on commit 73b6339

Please sign in to comment.