koilib 9.4.1 → 10.0.0
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/dist/koinos.js +32 -18
- package/dist/koinos.min.js +1 -1
- package/lib/browser/utils.d.ts +15 -1
- package/lib/browser/utils.js +32 -18
- package/lib/browser/utils.js.map +1 -1
- package/lib/utils.d.ts +15 -1
- package/lib/utils.js +32 -18
- package/lib/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/utils.ts +36 -17
package/dist/koinos.js
CHANGED
|
@@ -27961,33 +27961,47 @@ function formatUnits(value, decimals) {
|
|
|
27961
27961
|
}
|
|
27962
27962
|
exports.formatUnits = formatUnits;
|
|
27963
27963
|
/**
|
|
27964
|
-
* Function to format a decimal point number in an integer
|
|
27964
|
+
* Function to format a decimal point number in an integer, without
|
|
27965
|
+
* rounding.
|
|
27966
|
+
*
|
|
27967
|
+
* The value must be a string made of ASCII digits, an optional leading
|
|
27968
|
+
* "-" and at most one "." or "," decimal separator, with at least one
|
|
27969
|
+
* digit. Surrounding whitespace is ignored. Fractional digits beyond
|
|
27970
|
+
* `decimals` are accepted only when they are zeros (the value is still
|
|
27971
|
+
* exact); any other excess digit throws, so that the amount shown to
|
|
27972
|
+
* the user is always the amount that gets encoded.
|
|
27973
|
+
* @throws Error if the value is not a string in that format, cannot be
|
|
27974
|
+
* represented exactly with `decimals` decimals, or `decimals` is not a
|
|
27975
|
+
* non-negative integer.
|
|
27965
27976
|
* @example
|
|
27966
27977
|
* ```js
|
|
27967
27978
|
* const amount = parseUnits("0.00123456", 8);
|
|
27968
27979
|
* console.log(amount);
|
|
27969
27980
|
* // '123456'
|
|
27981
|
+
*
|
|
27982
|
+
* parseUnits("1.000000000", 8); // '100000000' (exact)
|
|
27983
|
+
* parseUnits("1.999999995", 8); // throws: not representable
|
|
27970
27984
|
* ```
|
|
27971
27985
|
*/
|
|
27972
27986
|
function parseUnits(value, decimals) {
|
|
27973
|
-
|
|
27974
|
-
|
|
27975
|
-
|
|
27976
|
-
|
|
27977
|
-
|
|
27978
|
-
|
|
27979
|
-
|
|
27980
|
-
|
|
27981
|
-
|
|
27982
|
-
|
|
27983
|
-
|
|
27984
|
-
|
|
27985
|
-
decimalPart
|
|
27986
|
-
|
|
27987
|
-
decimalPart = (BigInt(decimalPart) + BigInt(1)).toString();
|
|
27988
|
-
}
|
|
27987
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
27988
|
+
throw new Error(`invalid decimals: ${String(decimals)}`);
|
|
27989
|
+
}
|
|
27990
|
+
if (typeof value !== "string") {
|
|
27991
|
+
throw new Error(`invalid number: expected a string, got ${typeof value}`);
|
|
27992
|
+
}
|
|
27993
|
+
const match = /^(-?)(\d*)(?:[.,](\d*))?$/.exec(value.trim());
|
|
27994
|
+
if (!match || (match[2] === "" && !match[3])) {
|
|
27995
|
+
throw new Error(`invalid number: ${value}`);
|
|
27996
|
+
}
|
|
27997
|
+
const [, sign, integerPart, decimalPart = ""] = match;
|
|
27998
|
+
if (decimalPart.length > decimals &&
|
|
27999
|
+
/[1-9]/.test(decimalPart.slice(decimals))) {
|
|
28000
|
+
throw new Error(`invalid number: ${value} cannot be represented with ${decimals} decimals`);
|
|
27989
28001
|
}
|
|
27990
|
-
|
|
28002
|
+
const fraction = decimalPart.slice(0, decimals).padEnd(decimals, "0");
|
|
28003
|
+
const units = `${integerPart || "0"}${fraction}`.replace(/^0+(?=\d)/, "");
|
|
28004
|
+
return `${sign}${units}`;
|
|
27991
28005
|
}
|
|
27992
28006
|
exports.parseUnits = parseUnits;
|
|
27993
28007
|
/**
|