@numio/bigmath 2.2.3 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### 2.2.4
2
+ Fix handling negative numbers for toBase function.
3
+
1
4
  ### 2.2.3
2
5
  Add validation for negative number input in square root function
3
6
  Add validation for negative number input in cube root function
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@numio/bigmath",
3
3
  "description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
4
- "version": "2.2.3",
4
+ "version": "2.2.4",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -18,8 +18,16 @@ export const bi2s = (bigInt, fpe) => {
18
18
  };
19
19
  export const s2bi = (str) => {
20
20
  const fpi = str.indexOf(".");
21
- if (fpi === -1)
21
+ const isHex = str.startsWith("0x") || str.startsWith("-0x");
22
+ const isOctal = str.startsWith("0o") || str.startsWith("-0o");
23
+ const isBinary = str.startsWith("0b") || str.startsWith("-0b");
24
+ if (fpi === -1 && !isHex && !isOctal && !isBinary)
22
25
  return [BigInt(str), 0];
26
+ if (isHex || isBinary || isOctal) {
27
+ const isNegative = str[0] === "-";
28
+ const bi = BigInt(str.slice(isNegative ? 1 : 0));
29
+ return [isNegative ? -1n * bi : bi, 0];
30
+ }
23
31
  if (str.length < 15 && str[0] !== "0") {
24
32
  return [
25
33
  BigInt(+(str.slice(0, fpi) + str.slice(fpi + 1))),
@@ -1,4 +1,14 @@
1
+ import { s2bi } from "../shared/utils.js";
1
2
  /** Convert number to another base */
2
3
  export const toBase = ({ value, toBase }) => {
3
- return BigInt(value).toString(toBase);
4
+ const [bi] = s2bi(value);
5
+ const isNegative = value[0] === "-";
6
+ const map = {
7
+ 2: "0b",
8
+ 8: "0o",
9
+ 10: "",
10
+ 16: "0x",
11
+ };
12
+ return (isNegative ? "-" : "") + map[toBase] +
13
+ (isNegative ? -1n * bi : bi).toString(toBase).toUpperCase();
4
14
  };
@@ -1,4 +1,4 @@
1
1
  export type ToBase = (arg: {
2
2
  value: string;
3
- toBase: number;
3
+ toBase: 16 | 10 | 8 | 2;
4
4
  }) => string;