Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert helper methods #33

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
71 changes: 71 additions & 0 deletions src/helper/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'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 { 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 = digits.charAt(0).repeat(length - res.length) + res;
}
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 })).to.be.equal('AADF');
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