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 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
- const sign = value[0] === "-" ? "-" : "";
27974
- // eslint-disable-next-line prefer-const
27975
- let [integerPart, decimalPart] = value
27976
- .replace("-", "")
27977
- .replace(",", ".")
27978
- .split(".");
27979
- if (!decimalPart)
27980
- decimalPart = "";
27981
- decimalPart = decimalPart.padEnd(decimals, "0");
27982
- if (decimalPart.length > decimals) {
27983
- // approximate
27984
- const miniDecimals = decimalPart.substring(decimals);
27985
- decimalPart = decimalPart.substring(0, decimals);
27986
- if (miniDecimals.startsWith("5")) {
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
- return `${sign}${`${integerPart}${decimalPart}`.replace(/^0+(?=\d)/, "")}`;
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
  /**