@reykjavik/webtools 0.1.17 → 0.1.18

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/CHANGELOG.md CHANGED
@@ -4,14 +4,14 @@
4
4
 
5
5
  - ... <!-- Add new lines here. -->
6
6
 
7
- ## 0.1.17
7
+ ## 0.1.18
8
8
 
9
- _2024-03-09_
9
+ _2024-03-11_
10
10
 
11
11
  - `@reykjavik/webtools/fixIcelandicLocale`:
12
- - feat: Add support for `DateTimeFormat`'s `dayPeriod` option
12
+ - feat: Patch `Intl.PluralRules` and `Intl.ListFormat`
13
13
 
14
- ## 0.1.16
14
+ ## 0.1.16 – 0.1.17
15
15
 
16
16
  _2024-03-09_
17
17
 
package/README.md CHANGED
@@ -439,6 +439,8 @@ to their return values.
439
439
  - `Intl.Collator` and `String.prototype.localeCompare`
440
440
  - `Intl.NumberFormat` and `Number.prototype.toLocaleString`
441
441
  - `Intl.DateTimeFormat` and `Date.prototype.toLocaleDateString`
442
+ - `Intl.PluralRules`
443
+ - `Intl.ListFormat`
442
444
 
443
445
  This provides usable (but not perfect) results, with some caveats listed
444
446
  below.
@@ -449,7 +451,7 @@ your app's entry point:
449
451
  ```ts
450
452
  import '@reykjavik/webtools/fixIcelandicLocale';
451
453
 
452
- // Then continue with your day and use `localeCompare` and Intl.Collator,
454
+ // Then continue with your day and use `localeCompare` and other Intl.* methods
453
455
  // as you normally would. (See "limitations" below.)
454
456
  ```
455
457
 
@@ -1,4 +1,4 @@
1
- import { _PatchedCollator, _PatchedDateTimeFormat, _patchedLocaleCompare, _PatchedNumberFormat, _patchedToLocaleDateString, _patchedToLocaleString, } from './fixIcelandicLocale.privates.js';
1
+ import { _PatchedCollator, _PatchedDateTimeFormat, _PatchedListFormat, _patchedLocaleCompare, _PatchedNumberFormat, _PatchedPluralRules, _patchedToLocaleDateString, _patchedToLocaleString, } from './fixIcelandicLocale.privates.js';
2
2
  /*
3
3
  Mantra: Partial Icelandic suppoort is better than none. Partial Icelandic
4
4
  suppoort is better than none. Partial Icelandic suppoort is better than
@@ -12,3 +12,11 @@ if (Intl.Collator.supportedLocalesOf(['is']).length < 1) {
12
12
  Intl.DateTimeFormat = _PatchedDateTimeFormat;
13
13
  Date.prototype.toLocaleDateString = _patchedToLocaleDateString;
14
14
  }
15
+ /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-unnecessary-condition */
16
+ if (Intl.ListFormat && Intl.ListFormat.supportedLocalesOf(['is']).length < 1) {
17
+ Intl.ListFormat = _PatchedListFormat;
18
+ }
19
+ if (Intl.PluralRules && Intl.PluralRules.supportedLocalesOf(['is']).length < 1) {
20
+ Intl.PluralRules = _PatchedPluralRules;
21
+ }
22
+ /* eslint-enable @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-unnecessary-condition */
@@ -43,3 +43,19 @@ export declare const _patchedToLocaleDateString: {
43
43
  (locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions | undefined): string;
44
44
  };
45
45
  };
46
+ export declare const _PatchedPluralRules: {
47
+ (locales?: string | string[] | undefined, options?: Intl.PluralRulesOptions | undefined): Intl.PluralRules;
48
+ new (locales?: string | string[] | undefined, options?: Intl.PluralRulesOptions | undefined): Intl.PluralRules;
49
+ supportedLocalesOf(locales: string | string[], options?: {
50
+ localeMatcher?: "best fit" | "lookup" | undefined;
51
+ } | undefined): string[];
52
+ } & {
53
+ $original: typeof Intl.PluralRules;
54
+ };
55
+ export declare const _PatchedListFormat: {
56
+ new (locales?: string | string[] | undefined, options?: Intl.ListFormatOptions | undefined): Intl.ListFormat;
57
+ prototype: Intl.ListFormat;
58
+ supportedLocalesOf(locales: string | string[], options?: Pick<Intl.ListFormatOptions, "localeMatcher"> | undefined): string[];
59
+ } & {
60
+ $original: typeof Intl.ListFormat;
61
+ };
@@ -1,6 +1,5 @@
1
+ var _a;
1
2
  const _Collator = Intl.Collator;
2
- const _NumberFormat = Intl.NumberFormat;
3
- const _DateTimeFormat = Intl.DateTimeFormat;
4
3
  const mapLocales = (locales) => {
5
4
  locales = typeof locales === 'string' ? [locales] : locales || [];
6
5
  for (let i = 0, loc; (loc = locales[i]); i++) {
@@ -44,6 +43,7 @@ _patchedLocaleCompare.$original = _localeCompare;
44
43
  // ===========================================================================
45
44
  // NumberFormat
46
45
  // ===========================================================================
46
+ const _NumberFormat = Intl.NumberFormat;
47
47
  const reformatNumberParts = function (parts) {
48
48
  if (!this.mapped) {
49
49
  return parts;
@@ -192,6 +192,7 @@ const reformatDateTimeParts = function (parts) {
192
192
  });
193
193
  return parts;
194
194
  };
195
+ const _DateTimeFormat = Intl.DateTimeFormat;
195
196
  const PatchedDateTimeFormat = function DateTimeFormat(locales, options) {
196
197
  if (!(this instanceof PatchedDateTimeFormat)) {
197
198
  // @ts-expect-error (YOLO! Can't be arsed)
@@ -240,3 +241,72 @@ export const _patchedToLocaleDateString = function toLocaleDateString(locales, o
240
241
  return _PatchedDateTimeFormat(locales, options).format(this);
241
242
  };
242
243
  _patchedToLocaleDateString.$original = _toLocaleDateString;
244
+ // ===========================================================================
245
+ // PluralRules
246
+ // ===========================================================================
247
+ const _PluralRules = Intl.PluralRules;
248
+ let PatchedPluralRules;
249
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
250
+ if (_PluralRules) {
251
+ PatchedPluralRules = (_a = class PluralRules extends _PluralRules {
252
+ pluralIsl(n) {
253
+ return this.ord ? 'other' : n % 10 !== 1 || n % 100 === 11 ? 'other' : 'one';
254
+ }
255
+ constructor(locales, options) {
256
+ const mappedLocales = mapLocales(locales);
257
+ super(mappedLocales || locales, options);
258
+ this.mapped = !!mappedLocales;
259
+ this.ord = (options === null || options === void 0 ? void 0 : options.type) === 'ordinal';
260
+ }
261
+ select(n) {
262
+ if (this.mapped) {
263
+ // Pluralization function for Icelandic
264
+ // Copied over from https://www.npmjs.com/package/translate.js
265
+ return this.pluralIsl(n);
266
+ }
267
+ return super.select(n);
268
+ }
269
+ selectRange(n, n2) {
270
+ if (this.mapped) {
271
+ return this.pluralIsl(n2);
272
+ }
273
+ // @ts-expect-error (TS doesn't know about the .selectRange() method ...yet?)
274
+ return super.selectRange(n, n2);
275
+ }
276
+ },
277
+ _a.$original = _PluralRules,
278
+ _a);
279
+ }
280
+ export const _PatchedPluralRules = PatchedPluralRules;
281
+ // ===========================================================================
282
+ // ListFormat
283
+ // ===========================================================================
284
+ const _ListFormat = Intl.ListFormat;
285
+ let PatchedListFormat;
286
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
287
+ if (_ListFormat) {
288
+ PatchedListFormat = class ListFormat extends _ListFormat {
289
+ constructor(locales, options) {
290
+ const mappedLocales = mapLocales(locales);
291
+ super(mappedLocales || locales, options);
292
+ this.$original = _ListFormat;
293
+ this.mapped = !!mappedLocales;
294
+ }
295
+ format(list) {
296
+ return this.mapped ? combineParts(this.formatToParts(list)) : super.format(list);
297
+ }
298
+ formatToParts(list) {
299
+ const parts = super.formatToParts(list);
300
+ if (this.mapped) {
301
+ for (const item of parts) {
302
+ const { value } = item;
303
+ if (item.type === 'literal' && (value === ', el.' || value === ', eller')) {
304
+ item.value = ', eða';
305
+ }
306
+ }
307
+ }
308
+ return parts;
309
+ }
310
+ };
311
+ }
312
+ export const _PatchedListFormat = PatchedListFormat;
@@ -14,3 +14,11 @@ if (Intl.Collator.supportedLocalesOf(['is']).length < 1) {
14
14
  Intl.DateTimeFormat = fixIcelandicLocale_privates_js_1._PatchedDateTimeFormat;
15
15
  Date.prototype.toLocaleDateString = fixIcelandicLocale_privates_js_1._patchedToLocaleDateString;
16
16
  }
17
+ /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-unnecessary-condition */
18
+ if (Intl.ListFormat && Intl.ListFormat.supportedLocalesOf(['is']).length < 1) {
19
+ Intl.ListFormat = fixIcelandicLocale_privates_js_1._PatchedListFormat;
20
+ }
21
+ if (Intl.PluralRules && Intl.PluralRules.supportedLocalesOf(['is']).length < 1) {
22
+ Intl.PluralRules = fixIcelandicLocale_privates_js_1._PatchedPluralRules;
23
+ }
24
+ /* eslint-enable @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-unnecessary-condition */
@@ -43,3 +43,19 @@ export declare const _patchedToLocaleDateString: {
43
43
  (locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions | undefined): string;
44
44
  };
45
45
  };
46
+ export declare const _PatchedPluralRules: {
47
+ (locales?: string | string[] | undefined, options?: Intl.PluralRulesOptions | undefined): Intl.PluralRules;
48
+ new (locales?: string | string[] | undefined, options?: Intl.PluralRulesOptions | undefined): Intl.PluralRules;
49
+ supportedLocalesOf(locales: string | string[], options?: {
50
+ localeMatcher?: "best fit" | "lookup" | undefined;
51
+ } | undefined): string[];
52
+ } & {
53
+ $original: typeof Intl.PluralRules;
54
+ };
55
+ export declare const _PatchedListFormat: {
56
+ new (locales?: string | string[] | undefined, options?: Intl.ListFormatOptions | undefined): Intl.ListFormat;
57
+ prototype: Intl.ListFormat;
58
+ supportedLocalesOf(locales: string | string[], options?: Pick<Intl.ListFormatOptions, "localeMatcher"> | undefined): string[];
59
+ } & {
60
+ $original: typeof Intl.ListFormat;
61
+ };
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._patchedToLocaleDateString = exports._PatchedDateTimeFormat = exports._patchedToLocaleString = exports._PatchedNumberFormat = exports._patchedLocaleCompare = exports._PatchedCollator = void 0;
4
+ exports._PatchedListFormat = exports._PatchedPluralRules = exports._patchedToLocaleDateString = exports._PatchedDateTimeFormat = exports._patchedToLocaleString = exports._PatchedNumberFormat = exports._patchedLocaleCompare = exports._PatchedCollator = void 0;
4
5
  const _Collator = Intl.Collator;
5
- const _NumberFormat = Intl.NumberFormat;
6
- const _DateTimeFormat = Intl.DateTimeFormat;
7
6
  const mapLocales = (locales) => {
8
7
  locales = typeof locales === 'string' ? [locales] : locales || [];
9
8
  for (let i = 0, loc; (loc = locales[i]); i++) {
@@ -48,6 +47,7 @@ exports._patchedLocaleCompare.$original = _localeCompare;
48
47
  // ===========================================================================
49
48
  // NumberFormat
50
49
  // ===========================================================================
50
+ const _NumberFormat = Intl.NumberFormat;
51
51
  const reformatNumberParts = function (parts) {
52
52
  if (!this.mapped) {
53
53
  return parts;
@@ -197,6 +197,7 @@ const reformatDateTimeParts = function (parts) {
197
197
  });
198
198
  return parts;
199
199
  };
200
+ const _DateTimeFormat = Intl.DateTimeFormat;
200
201
  const PatchedDateTimeFormat = function DateTimeFormat(locales, options) {
201
202
  if (!(this instanceof PatchedDateTimeFormat)) {
202
203
  // @ts-expect-error (YOLO! Can't be arsed)
@@ -246,3 +247,72 @@ const _patchedToLocaleDateString = function toLocaleDateString(locales, options)
246
247
  };
247
248
  exports._patchedToLocaleDateString = _patchedToLocaleDateString;
248
249
  exports._patchedToLocaleDateString.$original = _toLocaleDateString;
250
+ // ===========================================================================
251
+ // PluralRules
252
+ // ===========================================================================
253
+ const _PluralRules = Intl.PluralRules;
254
+ let PatchedPluralRules;
255
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
256
+ if (_PluralRules) {
257
+ PatchedPluralRules = (_a = class PluralRules extends _PluralRules {
258
+ pluralIsl(n) {
259
+ return this.ord ? 'other' : n % 10 !== 1 || n % 100 === 11 ? 'other' : 'one';
260
+ }
261
+ constructor(locales, options) {
262
+ const mappedLocales = mapLocales(locales);
263
+ super(mappedLocales || locales, options);
264
+ this.mapped = !!mappedLocales;
265
+ this.ord = (options === null || options === void 0 ? void 0 : options.type) === 'ordinal';
266
+ }
267
+ select(n) {
268
+ if (this.mapped) {
269
+ // Pluralization function for Icelandic
270
+ // Copied over from https://www.npmjs.com/package/translate.js
271
+ return this.pluralIsl(n);
272
+ }
273
+ return super.select(n);
274
+ }
275
+ selectRange(n, n2) {
276
+ if (this.mapped) {
277
+ return this.pluralIsl(n2);
278
+ }
279
+ // @ts-expect-error (TS doesn't know about the .selectRange() method ...yet?)
280
+ return super.selectRange(n, n2);
281
+ }
282
+ },
283
+ _a.$original = _PluralRules,
284
+ _a);
285
+ }
286
+ exports._PatchedPluralRules = PatchedPluralRules;
287
+ // ===========================================================================
288
+ // ListFormat
289
+ // ===========================================================================
290
+ const _ListFormat = Intl.ListFormat;
291
+ let PatchedListFormat;
292
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
293
+ if (_ListFormat) {
294
+ PatchedListFormat = class ListFormat extends _ListFormat {
295
+ constructor(locales, options) {
296
+ const mappedLocales = mapLocales(locales);
297
+ super(mappedLocales || locales, options);
298
+ this.$original = _ListFormat;
299
+ this.mapped = !!mappedLocales;
300
+ }
301
+ format(list) {
302
+ return this.mapped ? combineParts(this.formatToParts(list)) : super.format(list);
303
+ }
304
+ formatToParts(list) {
305
+ const parts = super.formatToParts(list);
306
+ if (this.mapped) {
307
+ for (const item of parts) {
308
+ const { value } = item;
309
+ if (item.type === 'literal' && (value === ', el.' || value === ', eller')) {
310
+ item.value = ', eða';
311
+ }
312
+ }
313
+ }
314
+ return parts;
315
+ }
316
+ };
317
+ }
318
+ exports._PatchedListFormat = PatchedListFormat;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reykjavik/webtools",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "Misc. JS/TS helpers used by Reykjavík City's web dev teams.",
5
5
  "main": "index.js",
6
6
  "repository": "ssh://git@github.com:reykjavikcity/webtools.git",