@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
27 lines • 1.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatShort = void 0;
const numeral_1 = __importDefault(require("numeral"));
const bignumber_js_1 = require("bignumber.js");
const currencies_1 = require("@ledgerhq/coin-framework/currencies");
/**
* This will format in a very concise way a valid, typically to be used on axis.
* For instance 15k 20k ,...
*/
function formatShort(unit, value) {
const { magnitude } = unit;
const floatValue = value.div(new bignumber_js_1.BigNumber(10).pow(magnitude));
if (floatValue.isZero()) {
return "0";
}
if (new bignumber_js_1.BigNumber(-1).isLessThan(floatValue) && floatValue.isLessThan(1)) {
// numeral have issues with low values, fallback on formatCurrencyUnit
return (0, currencies_1.formatCurrencyUnit)(unit, value);
}
return (0, numeral_1.default)(floatValue.toNumber()).format("0[.]0a");
}
exports.formatShort = formatShort;
//# sourceMappingURL=formatShort.js.map