UNPKG

@cuppy/core

Version:

A lightweight currency display component with automatic inflation adjustment.

162 lines (161 loc) 5.12 kB
var T = Object.defineProperty; var F = Object.getOwnPropertySymbols; var I = Object.prototype.hasOwnProperty, U = Object.prototype.propertyIsEnumerable; var v = (a, t, e) => t in a ? T(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e, A = (a, t) => { for (var e in t || (t = {})) I.call(t, e) && v(a, e, t[e]); if (F) for (var e of F(t)) U.call(t, e) && v(a, e, t[e]); return a; }; var C = /* @__PURE__ */ ((a) => (a.cpiU = "CUUR0000SA0", a))(C || {}); const o = { hint: "none", hintDisplay: "tooltip", currencyStyle: "symbol", numberStyle: "compactLong", yearDisplay: "ifFallback", hintYearDisplay: "always", precisionMode: "sigfigs", inflationDataset: "cpiU", yearFormatter: (a, t) => `${a} (${t})`, inlineHintFormatter: (a, t) => `${a} [${t}]` }, w = {}, P = (a) => { a($); }, x = (/* @__PURE__ */ new Date()).getFullYear(), j = (a, t) => o.inlineHintFormatter(a, t), V = (a, t) => { const { year: e, requestedYear: n, yearDisplay: r } = t, i = o.yearFormatter(a, e); if (r === "never") return a; if (r === "always") return i; if (r === "exceptCurrent") return e !== x ? i : a; if (r === "ifFallback") return n != null && e !== n ? i : a; throw new RangeError(`Invalid year display style: ${r}.`); }, b = {}, g = (a, t) => { var d, S, f; const e = (S = (d = t.locale) != null ? d : o.locale) != null ? S : document.documentElement.lang, n = "USD", r = t.currencyStyle || o.currencyStyle, i = (f = o.useGrouping) != null ? f : t.useGrouping != null && String(t.useGrouping) !== "false", c = t.signDisplay || o.signDisplay, p = t.precisionMode || o.precisionMode, l = t.minDigits != null ? +t.minDigits : o.minDigits, m = t.maxDigits != null ? +t.maxDigits : o.maxDigits; let y; p === "decimalPlaces" ? y = { minimumFractionDigits: l, maximumFractionDigits: m } : y = { minimumSignificantDigits: l, maximumSignificantDigits: m }; const s = t.numberStyle || o.numberStyle; let D, h; if (s === "compactShort" || s === "compactLong") D = "compact", h = s === "compactShort" ? "short" : "long"; else if (s === "standard" || s === "scientific" || s === "engineering") D = s; else throw new RangeError(`Invalid notation: ${s}.`); const Y = `${e}|${n}|${r}|${i}|${c}|${p}|${l}|${m}|${D}|${h}`; let u; return b[Y] ? u = b[Y] : (u = new Intl.NumberFormat(e, A({ style: "currency", currency: n, currencyDisplay: r, useGrouping: i, signDisplay: c, notation: D, compactDisplay: h }, y)), b[Y] = u), u.format(a); }, E = (a, t, e) => { const { data: n, years: r } = w[a], i = n[t] ? t : r[0], p = n[i][0], l = n[e] ? e : r[r.length - 1], y = n[l][0]; return { from: { value: p, year: +i }, to: { value: y, year: +l } }; }, q = (a, t, e = "cpiU") => { let { from: n, to: r } = t; if (n != null || (n = x), r != null || (r = x), n === r) return { adjustedValue: a, from: n, to: r }; const { from: i, to: c } = E(e, n, r); return { adjustedValue: a * (c.value / i.value), from: i.year, to: c.year }; }, G = /_/g, $ = () => { document.querySelectorAll("[data-cuppy]").forEach((t) => { var p; const e = t.dataset; (p = e.value) != null || (e.value = t.innerText); const n = +e.value.replace(G, ""), r = +(e.from || o.from || x), i = +(e.to || o.to || x), c = e.yearDisplay || o.yearDisplay; if (r !== i) { const l = e.inflationDataset || o.inflationDataset, { adjustedValue: m, from: y, to: s } = q(n, { from: r, to: i }, l), D = e.hint || o.hint, h = e.hintDisplay || o.hintDisplay, Y = e.hintYearDisplay || o.hintYearDisplay; if (D === "to") { const u = g(n, e), d = V(u, { year: y, requestedYear: r, yearDisplay: c }), S = g(m, e), f = V( S, { year: s, requestedYear: i, yearDisplay: Y } ); h === "tooltip" ? (t.innerText = d, t.title = f) : (t.innerText = j( d, f ), t.removeAttribute("title")); } else if (D === "from") { const u = g(n, e), d = V(u, { year: y, requestedYear: r, yearDisplay: Y }), S = g(m, e), f = V( S, { year: s, requestedYear: i, yearDisplay: c } ); h === "tooltip" ? (t.innerText = f, t.title = d) : (t.innerText = j( f, d ), t.removeAttribute("title")); } else { const u = g(m, e); t.innerText = V(u, { year: s, requestedYear: i, yearDisplay: c }), t.removeAttribute("title"); } } else { const l = g(n, e); t.innerText = V(l, { year: r, yearDisplay: c }), t.removeAttribute("title"); } }); }; $.options = o; $.datasets = w; $.dataset = P; $.formatCurrency = g; $.adjustForInflation = q; export { C as InflationDataset, $ as default, E as getInflationData };