@usequeek/theme-kit 0.1.28 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ /**
2
+ * What the server charges for the cart on screen, as the host app read it
3
+ * (quantity-break and spend-gate discounts included). The kit only DISPLAYS it:
4
+ * every amount is a decimal string exactly as the server sent it ("4500.00"),
5
+ * and nothing here ever computes a price, a discount or a total.
6
+ */
7
+
8
+ /** One cart line as the server prices it. Matched to a local line by `line_id` AND `quantity`. */
9
+ export interface CartLinePricing {
10
+ /** The local cart line id (`CartItem.id`). */
11
+ line_id: string;
12
+ /** The quantity the server priced; a line whose quantity differs is not covered. */
13
+ quantity: number;
14
+ /** One unit with its add-ons, as charged. */
15
+ unit_price: string;
16
+ /** The line total as charged. */
17
+ line_total: string;
18
+ /** The list (undiscounted) line total, only when the server charges less than it; otherwise null. */
19
+ list_line_total: string | null;
20
+ }
21
+
22
+ /** The server's pricing of one whole cart. */
23
+ export interface CartServerPricing {
24
+ lines: readonly CartLinePricing[];
25
+ /** The cart total as charged (before coupons and delivery). */
26
+ total: string;
27
+ /** The list (undiscounted) cart total, only when the server charges less than it; otherwise null. */
28
+ list_total: string | null;
29
+ }
30
+
31
+ const DECIMAL = /^\d+(\.\d+)?$/;
32
+
33
+ function isDecimal(value: unknown): value is string {
34
+ return typeof value === 'string' && DECIMAL.test(value);
35
+ }
36
+
37
+ /** Digit-exact `a > b` for two non-negative decimal strings (no float round trip). */
38
+ function isGreater(a: string, b: string): boolean {
39
+ const [aWhole, aFraction = ''] = a.split('.');
40
+ const [bWhole, bFraction = ''] = b.split('.');
41
+ const left = aWhole.replace(/^0+(?=\d)/, '');
42
+ const right = bWhole.replace(/^0+(?=\d)/, '');
43
+
44
+ if (left.length !== right.length) {
45
+ return left.length > right.length;
46
+ }
47
+
48
+ // Equal-width digit strings compare in numeric order; this only orders text, it computes nothing.
49
+ const width = aFraction.length > bFraction.length ? aFraction.length : bFraction.length;
50
+ const leftDigits = `${left}${aFraction.padEnd(width, '0')}`;
51
+ const rightDigits = `${right}${bFraction.padEnd(width, '0')}`;
52
+
53
+ return leftDigits > rightDigits;
54
+ }
55
+
56
+ /** A struck list amount is kept only when it is a well-formed decimal HIGHER than what is charged. */
57
+ function strikeOf(list: unknown, charged: string): string | null {
58
+ return isDecimal(list) && isGreater(list, charged) ? list : null;
59
+ }
60
+
61
+ /**
62
+ * The server pricing that may stand for these cart lines, or null.
63
+ *
64
+ * It stands only when it covers the lines one to one: same count, every line
65
+ * found by id with the same quantity, and every amount a plain
66
+ * non-negative decimal string. Anything else (no pricing, a line added or
67
+ * re-quantified since the server read, a malformed amount) is null, and the
68
+ * caller prints its own list prices exactly as it always has: nothing is
69
+ * guessed and nothing is mixed. A struck list amount that is not higher than
70
+ * the amount charged is dropped to null. Amounts are returned as received.
71
+ */
72
+ export function resolveCartPricing(
73
+ pricing: CartServerPricing | null | undefined,
74
+ lines: readonly { id: string; quantity: number }[],
75
+ ): CartServerPricing | null {
76
+ if (!pricing || !Array.isArray(pricing.lines) || !isDecimal(pricing.total)) {
77
+ return null;
78
+ }
79
+
80
+ if (lines.length === 0 || pricing.lines.length !== lines.length) {
81
+ return null;
82
+ }
83
+
84
+ const byId = new Map<string, CartLinePricing>();
85
+
86
+ for (const entry of pricing.lines) {
87
+ if (!entry || typeof entry.line_id !== 'string' || !isDecimal(entry.unit_price) || !isDecimal(entry.line_total)) {
88
+ return null;
89
+ }
90
+
91
+ byId.set(entry.line_id, entry);
92
+ }
93
+
94
+ const covered: CartLinePricing[] = [];
95
+
96
+ for (const line of lines) {
97
+ const entry = byId.get(line.id);
98
+
99
+ if (!entry || entry.quantity !== line.quantity) {
100
+ return null;
101
+ }
102
+
103
+ covered.push({ ...entry, list_line_total: strikeOf(entry.list_line_total, entry.line_total) });
104
+ }
105
+
106
+ return { lines: covered, total: pricing.total, list_total: strikeOf(pricing.list_total, pricing.total) };
107
+ }
108
+
109
+ /** The server pricing of one cart line from a resolved overlay, or null when there is none for it. */
110
+ export function cartLinePricing(
111
+ pricing: CartServerPricing | null | undefined,
112
+ lineId: string,
113
+ ): CartLinePricing | null {
114
+ return pricing?.lines.find((entry) => entry.line_id === lineId) ?? null;
115
+ }
package/utils/format.ts CHANGED
@@ -171,7 +171,52 @@ export function formatMoney(
171
171
  }
172
172
  }
173
173
 
174
+ /**
175
+ * Format a money amount the server sent as a decimal STRING ("4500.00",
176
+ * KWD "1.250") without a float round trip: the string reaches Intl untouched,
177
+ * at the currency's own precision, with decimals shown only when present (the
178
+ * same style as `formatMoney`, which caps at 2 digits). Returns an empty string
179
+ * for anything that is not a plain non-negative decimal. Never computes, never throws.
180
+ *
181
+ * @example
182
+ * formatWireMoney('4500.00', 'NGN'); // "₦4,500"
183
+ */
184
+ export function formatWireMoney(
185
+ amount: string,
186
+ currency = PLATFORM_DEFAULT_CURRENCY,
187
+ locale?: string | null,
188
+ ): string {
189
+ if (typeof amount !== 'string' || !/^\d+(\.\d+)?$/.test(amount)) {
190
+ return '';
191
+ }
192
+
193
+ const code = normalizeCurrencyCode(currency);
194
+ const tag = displayTagFor(locale, (l) => Intl.NumberFormat.supportedLocalesOf(l));
195
+ try {
196
+ const precision = new Intl.NumberFormat(tag, { style: 'currency', currency: code }).resolvedOptions().maximumFractionDigits ?? 2;
197
+
198
+ // Intl parses a decimal string exactly; the cast only satisfies the lib typing.
199
+ return new Intl.NumberFormat(tag, {
200
+ style: 'currency',
201
+ currency: code,
202
+ minimumFractionDigits: 0,
203
+ maximumFractionDigits: precision,
204
+ }).format(amount as unknown as number);
205
+ } catch {
206
+ return `${code} ${amount}`;
207
+ }
208
+ }
209
+
174
210
  /** A whole-number percentage string, e.g. `toPercent(12.4)` is `"12%"`. */
211
+ /**
212
+ * The one rounding for an amount the kit computes (event values, line totals):
213
+ * three decimals, enough for KWD/BHD/OMR, and it strips float noise such as
214
+ * 59.970000000000006. A non-finite input stays non-finite so callers can omit it.
215
+ */
216
+ export function roundAmount(amount: number): number {
217
+ return Number(amount.toFixed(3));
218
+ }
219
+
175
220
  export function toPercent(value: number): string {
176
221
  return `${Math.max(0, Math.round(value))}%`;
177
222
  }
@@ -1,32 +0,0 @@
1
- 'use client';
2
-
3
- import { gtagPush } from './gtag';
4
-
5
- /**
6
- * Not gated on consent here — Consent Mode (see analytics.tsx) is what
7
- * decides whether gtag.js actually transmits anything for a denied visitor.
8
- * Callers push events unconditionally; Google's library filters them.
9
- */
10
- interface GaItem {
11
- item_id: string;
12
- item_name: string;
13
- price: number;
14
- quantity?: number;
15
- }
16
-
17
- export function pushViewItem(item: GaItem, currency: string): void {
18
- gtagPush('event', 'view_item', { currency, value: item.price, items: [item] });
19
- }
20
-
21
- export function pushAddToCart(item: GaItem, currency: string): void {
22
- gtagPush('event', 'add_to_cart', { currency, value: item.price * (item.quantity ?? 1), items: [item] });
23
- }
24
-
25
- export function pushPurchase(args: { transactionId: string; value: number; currency: string; items: GaItem[] }): void {
26
- gtagPush('event', 'purchase', {
27
- transaction_id: args.transactionId,
28
- value: args.value,
29
- currency: args.currency,
30
- items: args.items,
31
- });
32
- }