@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
46 lines • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.clampDecimals = clampDecimals;
exports.isOverDecimalLimit = isOverDecimalLimit;
exports.trimTrailingZeros = trimTrailingZeros;
function clampDecimals(display) {
const dotIndex = display.lastIndexOf(".");
const commaIndex = display.lastIndexOf(",");
const sepIndex = Math.max(dotIndex, commaIndex);
if (sepIndex === -1)
return display;
const integerPart = display.slice(0, sepIndex);
const sep = display[sepIndex];
const decimals = display.slice(sepIndex + 1);
if (decimals.length <= 2)
return display;
return `${integerPart}${sep}${decimals.slice(0, 2)}`;
}
function isOverDecimalLimit(display) {
const dotIndex = display.lastIndexOf(".");
const commaIndex = display.lastIndexOf(",");
const sepIndex = Math.max(dotIndex, commaIndex);
if (sepIndex === -1)
return false;
return display.length - sepIndex - 1 > 2;
}
function trimTrailingZeros(display) {
const dotIndex = display.lastIndexOf(".");
const commaIndex = display.lastIndexOf(",");
const sepIndex = Math.max(dotIndex, commaIndex);
if (sepIndex === -1)
return display;
const integerPart = display.slice(0, sepIndex);
const sep = display[sepIndex];
let decimals = display.slice(sepIndex + 1);
// Remove trailing zeros
let endIndex = decimals.length;
while (endIndex > 0 && decimals[endIndex - 1] === "0") {
endIndex--;
}
decimals = decimals.slice(0, endIndex);
if (!decimals)
return integerPart;
return `${integerPart}${sep}${decimals}`;
}
//# sourceMappingURL=decimals.js.map