@munchi_oy/core 2.0.0 → 2.0.1

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.
package/src/region.ts ADDED
@@ -0,0 +1,61 @@
1
+ import type { CashRounding } from "./money";
2
+
3
+ /** What can't be derived from a store's country with Intl. Every country is allowed; this only fills in local habits. */
4
+ export interface CountryQuirks {
5
+ cashRounding: CashRounding | null;
6
+ /** Languages a customer-facing screen (kiosk, receipt) offers; the first is the default. */
7
+ customerLanguages: string[];
8
+ }
9
+
10
+ const QUIRKS: Readonly<Record<string, Partial<CountryQuirks>>> = {
11
+ FI: { cashRounding: { increment: 5 }, customerLanguages: ["fi", "sv", "en"] },
12
+ EE: { cashRounding: { increment: 5 }, customerLanguages: ["et", "ru", "en"] },
13
+ SE: { cashRounding: { increment: 100 } },
14
+ ES: { customerLanguages: ["es", "ca", "en"] },
15
+ };
16
+
17
+ // Intl.Locale is missing on some runtimes (older Hermes), and an unknown region throws.
18
+ function mainLanguage(countryCode: string): string | undefined {
19
+ if (typeof Intl.Locale !== "function") {
20
+ return undefined;
21
+ }
22
+ try {
23
+ return new Intl.Locale(`und-${countryCode}`).maximize().language;
24
+ } catch {
25
+ return undefined;
26
+ }
27
+ }
28
+
29
+ export function countryQuirks(
30
+ countryCode: string | null | undefined,
31
+ ): CountryQuirks {
32
+ if (!countryCode) {
33
+ return { cashRounding: null, customerLanguages: ["en"] };
34
+ }
35
+ const code = countryCode.toUpperCase();
36
+ const quirks = QUIRKS[code] ?? {};
37
+ const language = mainLanguage(code);
38
+ return {
39
+ cashRounding: quirks.cashRounding ?? null,
40
+ customerLanguages:
41
+ quirks.customerLanguages ??
42
+ (language && language !== "und"
43
+ ? [...new Set([language, "en"])]
44
+ : ["en"]),
45
+ };
46
+ }
47
+
48
+ /** The locale to format numbers and dates in: the reader's language with the venue's country ("en" + "FI" → "en-FI"). */
49
+ export function regionLocale(
50
+ language: string,
51
+ countryCode: string | null | undefined,
52
+ ): string {
53
+ const base = language.split("-")[0].toLowerCase();
54
+ if (!countryCode) {
55
+ return base;
56
+ }
57
+ const candidate = `${base}-${countryCode.toUpperCase()}`;
58
+ return Intl.NumberFormat.supportedLocalesOf([candidate]).length > 0
59
+ ? candidate
60
+ : base;
61
+ }
package/src/version.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  // This file is auto-generated. Do not edit manually.
2
2
  // Run 'pnpm version:sync' to update this file.
3
- export const VERSION = "2.0.0";
3
+ export const VERSION = "2.0.1";