UNPKG

@provenanceio/wallet-utils

Version:

Typescript Utilities for Provenance Blockchain Wallet

58 lines (47 loc) 2.88 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } export var numberFormat = function numberFormat(rawValue) { var digits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; var extraOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; // If we don't have a value to start with just return it if (rawValue === null || rawValue === undefined || rawValue === '' || rawValue === '--') return rawValue; // If we get a string, convert it to a number var value = typeof rawValue === 'string' ? Number(rawValue) : rawValue; // If we just want to shorthand a number, don't bother with other calculations // Eg: numberFormat(1245, 3, { shorthand: true }) => 1.24K // Eg: numberFormat(1245000, 3, { shorthand: true }) => 1.24M // Eg: numberFormat(1245000000, 3, { shorthand: true }) => 1.24B if (extraOptions.shorthand) { var letter = ''; // Under Thousand var roundedValue = value; var trillion = 1e12; var billion = 1e9; var million = 1e6; var thousand = 1e3; if (value >= trillion) { letter = 'T'; roundedValue = value / trillion; } else if (value >= billion) { letter = 'B'; roundedValue = value / billion; } else if (value >= million) { letter = 'M'; roundedValue = value / million; } else if (value >= thousand) { letter = 'K'; roundedValue = value / thousand; } var finalValue = roundedValue.toLocaleString('en-US', { maximumFractionDigits: digits, minimumSignificantDigits: extraOptions.minimumSignificantDigits, maximumSignificantDigits: extraOptions.maximumSignificantDigits }); return "".concat(finalValue).concat(letter); } var options = {}; // Amount of significant digits to return in string if (typeof digits === 'number' && digits >= 0) { options = { maximumFractionDigits: digits }; } return value.toLocaleString('en-US', _objectSpread(_objectSpread({}, options), extraOptions)); };