UNPKG

@daysnap/utils

Version:
23 lines (19 loc) 1.18 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/formatAmount.ts function formatAmount(s, radix = 1) { const money = parseFloat(_optionalChain([s, 'optionalAccess', _2 => _2.toString, 'call', _3 => _3()])); if (money) { s = `${(money / radix).toFixed(2)}`; const l = s.split(".")[0].split("").reverse(); const r = s.split(".")[1]; let t = ""; l.forEach((_, key) => { t += l[key] + ((key + 1) % 3 === 0 && key + 1 !== l.length ? "," : ""); }); return `${t.split("").reverse().join("")}.${r}`; } if (+s === 0) { return "0.00"; } return "---"; } exports.formatAmount = formatAmount;