UNPKG

@formatjs/intl-pluralrules

Version:
51 lines (50 loc) 1.3 kB
import { invariant, ToNumber, ZERO } from "@formatjs/ecma402-abstract"; /** * ECMA-402 Spec: GetOperands abstract operation * https://tc39.es/ecma402/#sec-getoperands * * Implementation: Extended to support compact exponent (c/e operands) * * @param s Formatted number string * @param exponent Compact decimal exponent (c/e operand), defaults to 0 */ export function GetOperands(s, exponent = 0) { invariant(typeof s === "string", `GetOperands should have been called with a string`); const n = ToNumber(s); invariant(n.isFinite(), "n should be finite"); let dp = s.indexOf("."); let iv; let f; let v; let fv = ""; if (dp === -1) { iv = n; f = ZERO; v = 0; } else { iv = s.slice(0, dp); fv = s.slice(dp, s.length); f = ToNumber(fv); v = fv.length; } const i = ToNumber(iv).abs(); let w; let t; if (!f.isZero()) { const ft = fv.replace(/0+$/, ""); w = ft.length; t = ToNumber(ft); } else { w = 0; t = ZERO; } return { Number: n, IntegerDigits: i.lessThanOrEqualTo(Number.MAX_SAFE_INTEGER) && i.greaterThanOrEqualTo(-Number.MAX_SAFE_INTEGER) ? i.toNumber() : i.toString(), NumberOfFractionDigits: v, NumberOfFractionDigitsWithoutTrailing: w, FractionDigits: f.toNumber(), FractionDigitsWithoutTrailing: t.toNumber(), CompactExponent: exponent }; }