2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-07 18:24:04 -05:00
|
|
|
exports.parseEther = exports.formatEther = exports.parseUnits = exports.formatUnits = exports.commify = void 0;
|
2019-08-25 02:39:20 -04:00
|
|
|
var bignumber_1 = require("@ethersproject/bignumber");
|
2019-08-02 02:10:58 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2019-05-14 18:48:48 -04:00
|
|
|
var names = [
|
|
|
|
"wei",
|
|
|
|
"kwei",
|
|
|
|
"mwei",
|
|
|
|
"gwei",
|
|
|
|
"szabo",
|
|
|
|
"finney",
|
|
|
|
"ether",
|
|
|
|
];
|
|
|
|
// Some environments have issues with RegEx that contain back-tracking, so we cannot
|
|
|
|
// use them.
|
|
|
|
function commify(value) {
|
|
|
|
var comps = String(value).split(".");
|
|
|
|
if (comps.length > 2 || !comps[0].match(/^-?[0-9]*$/) || (comps[1] && !comps[1].match(/^[0-9]*$/)) || value === "." || value === "-.") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid value", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// Make sure we have at least one whole digit (0 if none)
|
|
|
|
var whole = comps[0];
|
|
|
|
var negative = "";
|
|
|
|
if (whole.substring(0, 1) === "-") {
|
|
|
|
negative = "-";
|
|
|
|
whole = whole.substring(1);
|
|
|
|
}
|
|
|
|
// Make sure we have at least 1 whole digit with no leading zeros
|
|
|
|
while (whole.substring(0, 1) === "0") {
|
|
|
|
whole = whole.substring(1);
|
|
|
|
}
|
|
|
|
if (whole === "") {
|
|
|
|
whole = "0";
|
|
|
|
}
|
|
|
|
var suffix = "";
|
|
|
|
if (comps.length === 2) {
|
|
|
|
suffix = "." + (comps[1] || "0");
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
while (suffix.length > 2 && suffix[suffix.length - 1] === "0") {
|
|
|
|
suffix = suffix.substring(0, suffix.length - 1);
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
var formatted = [];
|
|
|
|
while (whole.length) {
|
|
|
|
if (whole.length <= 3) {
|
|
|
|
formatted.unshift(whole);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var index = whole.length - 3;
|
|
|
|
formatted.unshift(whole.substring(index));
|
|
|
|
whole = whole.substring(0, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return negative + formatted.join(",") + suffix;
|
|
|
|
}
|
|
|
|
exports.commify = commify;
|
|
|
|
function formatUnits(value, unitName) {
|
|
|
|
if (typeof (unitName) === "string") {
|
|
|
|
var index = names.indexOf(unitName);
|
|
|
|
if (index !== -1) {
|
|
|
|
unitName = 3 * index;
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bignumber_1.formatFixed)(value, (unitName != null) ? unitName : 18);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
exports.formatUnits = formatUnits;
|
|
|
|
function parseUnits(value, unitName) {
|
2020-10-07 20:10:50 -04:00
|
|
|
if (typeof (value) !== "string") {
|
|
|
|
logger.throwArgumentError("value must be a string", "value", value);
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (unitName) === "string") {
|
|
|
|
var index = names.indexOf(unitName);
|
|
|
|
if (index !== -1) {
|
|
|
|
unitName = 3 * index;
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bignumber_1.parseFixed)(value, (unitName != null) ? unitName : 18);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
exports.parseUnits = parseUnits;
|
|
|
|
function formatEther(wei) {
|
|
|
|
return formatUnits(wei, 18);
|
|
|
|
}
|
|
|
|
exports.formatEther = formatEther;
|
|
|
|
function parseEther(ether) {
|
|
|
|
return parseUnits(ether, 18);
|
|
|
|
}
|
|
|
|
exports.parseEther = parseEther;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=index.js.map
|