@eldrajs/sdk 0.4.0 → 0.4.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.
@@ -0,0 +1,21 @@
1
+ type Restore = () => void;
2
+ export interface InstallIcelandicIntlOptions {
3
+ /**
4
+ * Patch even where the runtime has Icelandic data (Node, Safari, Firefox), so every runtime prints
5
+ * the same Icelandic. Off by default: there the runtime's own Icelandic is left alone.
6
+ */
7
+ force?: boolean;
8
+ }
9
+ /**
10
+ * Formats Icelandic from this package's data wherever the runtime has none, and leaves every other
11
+ * locale to the runtime. Call it once, before anything formats (a Nuxt client plugin, the top of
12
+ * `main.ts`). Returns a function that puts the runtime's own `Intl` back.
13
+ *
14
+ * Covers numbers and dates. Dates in a zone other than UTC (Iceland's) stay with the runtime, since
15
+ * the bundled data carries no time zones. Lists, relative times, names and sorting are not patched:
16
+ * use `listFormat`, `relativeTimeFormat`, `displayNames` and `comparator` for those.
17
+ *
18
+ * Only the call that installs the patch can remove it, once; a call made while it is installed
19
+ * changes nothing and returns a function that does nothing.
20
+ */
21
+ export declare function installIcelandicIntl(options?: InstallIcelandicIntlOptions): Restore;
@@ -0,0 +1,159 @@
1
+ import { n as isUtcZone, t as dateTimeFormat } from "../date-DzIUeT6Y.js";
2
+ import { n as numberFormat } from "../number-nkpzAc71.js";
3
+ // src/format/intl.ts
4
+ /**
5
+ * `installIcelandicIntl()` — Icelandic for code you do not control.
6
+ *
7
+ * `@eldrajs/sdk/format`'s functions are the way to format text you write. A date picker, vue-i18n's
8
+ * `n()`/`d()` or a chart library calls `Intl` itself, and where the runtime has no Icelandic data
9
+ * (Chrome, Edge, Cloudflare Workers) it prints English. This patches `Intl.NumberFormat`,
10
+ * `Intl.DateTimeFormat`, `Number.prototype.toLocaleString` and the three `Date.prototype.toLocale*`
11
+ * methods so that a request for Icelandic is formatted from this package's own data, and every other
12
+ * locale is left to the runtime exactly as before. Unlike a polyfill that replaces `Intl`, nothing
13
+ * changes for a browser on `en-GB` or `de-DE`, and there is no default locale to get wrong.
14
+ */
15
+ const INSTALLED = Symbol.for("@eldrajs/sdk/format/intl");
16
+ /** Whether the runtime would format `locales` as Icelandic if it could: an Icelandic tag before any tag it supports. */
17
+ function wantsIcelandic(Runtime, locales) {
18
+ if (locales === void 0) return false;
19
+ for (const tag of Intl.getCanonicalLocales(locales)) {
20
+ if (/^is(-|$)/i.test(tag)) return true;
21
+ if (Runtime.supportedLocalesOf([tag]).length > 0) return false;
22
+ }
23
+ return false;
24
+ }
25
+ function supportedLocalesOf(Runtime) {
26
+ return (locales, options) => {
27
+ const supported = new Set(Runtime.supportedLocalesOf(locales, options));
28
+ return Intl.getCanonicalLocales(locales).filter((tag) => supported.has(tag) || /^is(-|$)/i.test(tag));
29
+ };
30
+ }
31
+ const DATE_FIELDS = [
32
+ "weekday",
33
+ "year",
34
+ "month",
35
+ "day"
36
+ ];
37
+ const TIME_FIELDS = [
38
+ "dayPeriod",
39
+ "hour",
40
+ "minute",
41
+ "second",
42
+ "fractionalSecondDigits"
43
+ ];
44
+ /** ECMA-402's ToDateTimeOptions: the fields `toLocale{Date,Time,}String` add when none are asked for. */
45
+ function withDefaults(options, required, defaults) {
46
+ const resolved = { ...options };
47
+ if (required === "date" && resolved.timeStyle !== void 0) throw new TypeError("toLocaleDateString does not accept timeStyle");
48
+ if (required === "time" && resolved.dateStyle !== void 0) throw new TypeError("toLocaleTimeString does not accept dateStyle");
49
+ const has = (fields) => fields.some((field) => resolved[field] !== void 0);
50
+ let needDefaults = true;
51
+ if ((required === "date" || required === "any") && has(DATE_FIELDS)) needDefaults = false;
52
+ if ((required === "time" || required === "any") && has(TIME_FIELDS)) needDefaults = false;
53
+ if (resolved.dateStyle !== void 0 || resolved.timeStyle !== void 0) needDefaults = false;
54
+ if (!needDefaults) return resolved;
55
+ if (defaults === "date" || defaults === "all") Object.assign(resolved, {
56
+ year: "numeric",
57
+ month: "numeric",
58
+ day: "numeric"
59
+ });
60
+ if (defaults === "time" || defaults === "all") Object.assign(resolved, {
61
+ hour: "numeric",
62
+ minute: "numeric",
63
+ second: "numeric"
64
+ });
65
+ return resolved;
66
+ }
67
+ /**
68
+ * Formats Icelandic from this package's data wherever the runtime has none, and leaves every other
69
+ * locale to the runtime. Call it once, before anything formats (a Nuxt client plugin, the top of
70
+ * `main.ts`). Returns a function that puts the runtime's own `Intl` back.
71
+ *
72
+ * Covers numbers and dates. Dates in a zone other than UTC (Iceland's) stay with the runtime, since
73
+ * the bundled data carries no time zones. Lists, relative times, names and sorting are not patched:
74
+ * use `listFormat`, `relativeTimeFormat`, `displayNames` and `comparator` for those.
75
+ *
76
+ * Only the call that installs the patch can remove it, once; a call made while it is installed
77
+ * changes nothing and returns a function that does nothing.
78
+ */
79
+ function installIcelandicIntl(options = {}) {
80
+ const intl = Intl;
81
+ const RuntimeNumberFormat = Intl.NumberFormat;
82
+ const RuntimeDateTimeFormat = Intl.DateTimeFormat;
83
+ if (RuntimeNumberFormat[INSTALLED]) return () => {};
84
+ if (RuntimeNumberFormat.supportedLocalesOf(["is"]).length > 0 && RuntimeDateTimeFormat.supportedLocalesOf(["is"]).length > 0 && !options.force) return () => {};
85
+ /**
86
+ * The zone a date formatter would use: the one asked for, else the runtime's own, read on every
87
+ * call because it can change while the page is open.
88
+ */
89
+ const zoneOf = (dateOptions) => dateOptions?.timeZone ?? new RuntimeDateTimeFormat().resolvedOptions().timeZone;
90
+ const icelandicNumbers = (locales) => wantsIcelandic(RuntimeNumberFormat, locales);
91
+ /** The UTC zone to format an Icelandic date in, or `undefined` when the runtime keeps it. */
92
+ const icelandicZone = (locales, dateOptions) => {
93
+ if (!wantsIcelandic(RuntimeDateTimeFormat, locales)) return void 0;
94
+ const zone = zoneOf(dateOptions);
95
+ return isUtcZone(zone) ? zone : void 0;
96
+ };
97
+ /** The formatters this patch hands out, so `instanceof` recognises them as well as native ones. */
98
+ const bundled = /* @__PURE__ */ new WeakSet();
99
+ const handOut = (formatter) => {
100
+ bundled.add(formatter);
101
+ return formatter;
102
+ };
103
+ const icelandicDateFormat = (dateOptions, zone) => handOut(dateTimeFormat("is", {
104
+ ...dateOptions,
105
+ timeZone: zone
106
+ }));
107
+ const NumberFormat = function NumberFormat(locales, numberOptions) {
108
+ if (icelandicNumbers(locales)) return handOut(numberFormat("is", numberOptions));
109
+ return Reflect.construct(RuntimeNumberFormat, [locales, numberOptions], new.target ?? RuntimeNumberFormat);
110
+ };
111
+ Object.defineProperties(NumberFormat, {
112
+ prototype: { value: RuntimeNumberFormat.prototype },
113
+ supportedLocalesOf: { value: supportedLocalesOf(RuntimeNumberFormat) },
114
+ [Symbol.hasInstance]: { value: (value) => value instanceof RuntimeNumberFormat || typeof value === "object" && value !== null && bundled.has(value) }
115
+ });
116
+ const DateTimeFormat = function DateTimeFormat(locales, dateOptions) {
117
+ const zone = icelandicZone(locales, dateOptions);
118
+ if (zone !== void 0) return icelandicDateFormat(dateOptions, zone);
119
+ return Reflect.construct(RuntimeDateTimeFormat, [locales, dateOptions], new.target ?? RuntimeDateTimeFormat);
120
+ };
121
+ Object.defineProperties(DateTimeFormat, {
122
+ prototype: { value: RuntimeDateTimeFormat.prototype },
123
+ supportedLocalesOf: { value: supportedLocalesOf(RuntimeDateTimeFormat) },
124
+ [Symbol.hasInstance]: { value: (value) => value instanceof RuntimeDateTimeFormat || typeof value === "object" && value !== null && bundled.has(value) }
125
+ });
126
+ NumberFormat[INSTALLED] = true;
127
+ const numberToLocaleString = Number.prototype.toLocaleString;
128
+ const numberValueOf = Number.prototype.valueOf;
129
+ const dateGetTime = Date.prototype.getTime;
130
+ const dateToLocaleString = Date.prototype.toLocaleString;
131
+ const dateToLocaleDateString = Date.prototype.toLocaleDateString;
132
+ const dateToLocaleTimeString = Date.prototype.toLocaleTimeString;
133
+ Number.prototype.toLocaleString = function toLocaleString(locales, numberOptions) {
134
+ const value = numberValueOf.call(this);
135
+ return icelandicNumbers(locales) ? numberFormat("is", numberOptions).format(value) : numberToLocaleString.call(this, locales, numberOptions);
136
+ };
137
+ const patchDate = (original, required, defaults) => function(locales, dateOptions) {
138
+ const zone = Number.isNaN(dateGetTime.call(this)) ? void 0 : icelandicZone(locales, dateOptions);
139
+ if (zone === void 0) return original.call(this, locales, dateOptions);
140
+ return icelandicDateFormat(withDefaults(dateOptions, required, defaults), zone).format(this);
141
+ };
142
+ Date.prototype.toLocaleString = patchDate(dateToLocaleString, "any", "all");
143
+ Date.prototype.toLocaleDateString = patchDate(dateToLocaleDateString, "date", "date");
144
+ Date.prototype.toLocaleTimeString = patchDate(dateToLocaleTimeString, "time", "time");
145
+ intl.NumberFormat = NumberFormat;
146
+ intl.DateTimeFormat = DateTimeFormat;
147
+ let restored = false;
148
+ return () => {
149
+ if (restored) return;
150
+ restored = true;
151
+ intl.NumberFormat = RuntimeNumberFormat;
152
+ intl.DateTimeFormat = RuntimeDateTimeFormat;
153
+ Number.prototype.toLocaleString = numberToLocaleString;
154
+ Date.prototype.toLocaleString = dateToLocaleString;
155
+ Date.prototype.toLocaleDateString = dateToLocaleDateString;
156
+ Date.prototype.toLocaleTimeString = dateToLocaleTimeString;
157
+ };
158
+ }
159
+ export { installIcelandicIntl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eldrajs/sdk",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Framework-free client for the Eldra Web Studio public storefront API: CMS, catalog, cart, checkout handoff, orders, inventory. Types generated from your gateway's OpenAPI document and CMS schemas at build time.",
5
5
  "keywords": [
6
6
  "cms",
@@ -55,6 +55,10 @@
55
55
  "types": "./dist/format/display-names.d.ts",
56
56
  "import": "./dist/format/display-names.js"
57
57
  },
58
+ "./format/intl": {
59
+ "types": "./dist/format/intl.d.ts",
60
+ "import": "./dist/format/intl.js"
61
+ },
58
62
  "./package.json": "./package.json"
59
63
  },
60
64
  "publishConfig": {