-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctal.js
33 lines (27 loc) · 788 Bytes
/
octal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const { octalRegex1, octalRegex2 } = require("./const");
const { sum, multiply, pow } = require(`operation-strint`);
const isOctal = (money) => {
if (typeof money !== `string`) return undefined;
if (/__/i.test(money)) return false;
if (/^0o.+/i.test(money)) {
money = money.replace(/(?<=[0-7])_(?=[0-7])/g, "");
} else if (/_/i.test(money)) {
return false;
}
return octalRegex1.test(money) || octalRegex2.test(money);
};
const toDec = (num) => {
if (!isOctal(num)) return num;
num = num.replace(/^0+o?/, '');
return num
.split("")
.toReversed()
.reduce((acc, digit, index) => {
const thispos_val = multiply(pow(`8`, `${index}`), digit);
return sum(acc, thispos_val) || "0";
}, "0");
};
module.exports = {
isOctal,
toDec,
};