@naturalcycles/js-lib 15.93.2 → 15.93.3

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.
@@ -868,8 +868,7 @@ class LocalTimeFactory {
868
868
  isTimezoneValid(tz) {
869
869
  if (tz === 'UTC')
870
870
  return true; // we deliberately consider UTC a valid timezone, while it's mostly used in testing
871
- // oxlint-disable-next-line no-restricted-globals
872
- return Intl.supportedValuesOf('timeZone').includes(tz);
871
+ return Intl2.getSupportedTimezones().includes(tz);
873
872
  }
874
873
  sort(items, dir = 'asc', opt = {}) {
875
874
  const cmp = comparators.by((lt) => lt.$date.valueOf(), { dir });
@@ -7,13 +7,28 @@ import type { IANATimezone } from '../types.js';
7
7
  *
8
8
  * Methods accept non-optional arguments consciously,
9
9
  * to be able to cache them better. Just pass {} for options.
10
+ *
11
+ * The cache is never evicted, it grows with each unique combination of arguments.
12
+ * Don't pass unbounded dynamic values (e.g arbitrary user input) as locales or options.
10
13
  */
11
14
  declare class MemoizedIntl {
12
15
  /**
13
16
  * Returns the IANA timezone e.g `Europe/Stockholm`.
14
17
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
18
+ *
19
+ * Memoized, so it returns the timezone at the time of the first call.
20
+ * Changing `process.env.TZ` at runtime after that is not reflected.
15
21
  */
16
22
  getTimezone(): IANATimezone;
23
+ /**
24
+ * Returns the list of IANA timezones supported by the runtime (~420 entries).
25
+ * Memoized, because V8 rebuilds the list on every call (~70µs).
26
+ *
27
+ * The list contains canonical CLDR ids only, so valid aliases are missing from it,
28
+ * e.g `UTC` and `Europe/Kyiv` (listed as `Europe/Kiev`).
29
+ * Don't use it to validate user input as is.
30
+ */
31
+ getSupportedTimezones(): readonly IANATimezone[];
17
32
  DateTimeFormat(locales: Intl.LocalesArgument, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat;
18
33
  RelativeTimeFormat(locales: Intl.LocalesArgument, options: Intl.RelativeTimeFormatOptions): Intl.RelativeTimeFormat;
19
34
  DurationFormat(locales: Intl.LocalesArgument, options: Intl.DurationFormatOptions): Intl.DurationFormat;
package/dist/intl/intl.js CHANGED
@@ -9,15 +9,32 @@ import { _Memo } from '../decorators/memo.decorator.js';
9
9
  *
10
10
  * Methods accept non-optional arguments consciously,
11
11
  * to be able to cache them better. Just pass {} for options.
12
+ *
13
+ * The cache is never evicted, it grows with each unique combination of arguments.
14
+ * Don't pass unbounded dynamic values (e.g arbitrary user input) as locales or options.
12
15
  */
13
16
  class MemoizedIntl {
14
17
  /**
15
18
  * Returns the IANA timezone e.g `Europe/Stockholm`.
16
19
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
20
+ *
21
+ * Memoized, so it returns the timezone at the time of the first call.
22
+ * Changing `process.env.TZ` at runtime after that is not reflected.
17
23
  */
18
24
  getTimezone() {
19
25
  return Intl.DateTimeFormat().resolvedOptions().timeZone;
20
26
  }
27
+ /**
28
+ * Returns the list of IANA timezones supported by the runtime (~420 entries).
29
+ * Memoized, because V8 rebuilds the list on every call (~70µs).
30
+ *
31
+ * The list contains canonical CLDR ids only, so valid aliases are missing from it,
32
+ * e.g `UTC` and `Europe/Kyiv` (listed as `Europe/Kiev`).
33
+ * Don't use it to validate user input as is.
34
+ */
35
+ getSupportedTimezones() {
36
+ return Intl.supportedValuesOf('timeZone');
37
+ }
21
38
  DateTimeFormat(locales, options) {
22
39
  return new Intl.DateTimeFormat(locales, options);
23
40
  }
@@ -51,29 +68,49 @@ __decorate([
51
68
  ], MemoizedIntl.prototype, "getTimezone", null);
52
69
  __decorate([
53
70
  _Memo()
71
+ ], MemoizedIntl.prototype, "getSupportedTimezones", null);
72
+ __decorate([
73
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
54
74
  ], MemoizedIntl.prototype, "DateTimeFormat", null);
55
75
  __decorate([
56
- _Memo()
76
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
57
77
  ], MemoizedIntl.prototype, "RelativeTimeFormat", null);
58
78
  __decorate([
59
- _Memo()
79
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
60
80
  ], MemoizedIntl.prototype, "DurationFormat", null);
61
81
  __decorate([
62
- _Memo()
82
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
63
83
  ], MemoizedIntl.prototype, "NumberFormat", null);
64
84
  __decorate([
65
- _Memo()
85
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
66
86
  ], MemoizedIntl.prototype, "Collator", null);
67
87
  __decorate([
68
- _Memo()
88
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
69
89
  ], MemoizedIntl.prototype, "PluralRules", null);
70
90
  __decorate([
71
- _Memo()
91
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
72
92
  ], MemoizedIntl.prototype, "ListFormat", null);
73
93
  __decorate([
74
- _Memo()
94
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
75
95
  ], MemoizedIntl.prototype, "DisplayNames", null);
76
96
  __decorate([
77
- _Memo()
97
+ _Memo({ cacheKeyFn: intlCacheKeyFn })
78
98
  ], MemoizedIntl.prototype, "Segmenter", null);
79
99
  export const Intl2 = new MemoizedIntl();
100
+ /**
101
+ * Default `jsonMemoSerializer` would serialize `Intl.Locale` instances as `{}`,
102
+ * making all Locale arguments share one cache key. Convert them to their string tags instead.
103
+ *
104
+ * Options keys are serialized in sorted order, so that `{ a, b }` and `{ b, a }` share one cache key.
105
+ * Passing a keys array as replacer works, because Intl options are flat objects.
106
+ */
107
+ function intlCacheKeyFn([locales, options]) {
108
+ return JSON.stringify(localesKey(locales)) + JSON.stringify(options, Object.keys(options).sort());
109
+ }
110
+ function localesKey(locales) {
111
+ if (locales === undefined)
112
+ return;
113
+ if (Array.isArray(locales))
114
+ return locales.map(String);
115
+ return String(locales);
116
+ }
package/dist/semver.d.ts CHANGED
@@ -16,6 +16,8 @@ export type SemverTokens = [major: number, minor: number, patch: number];
16
16
  *
17
17
  * _semver('1').toString() === '1.0.0'
18
18
  *
19
+ * 4-part strings are treated as equal to 3-part strings, e.g.: '1.2.3.4' === '1.2.3'
20
+ *
19
21
  * @experimental
20
22
  */
21
23
  export declare class Semver {
package/dist/semver.js CHANGED
@@ -14,6 +14,8 @@ import { _assert } from './error/assert.js';
14
14
  *
15
15
  * _semver('1').toString() === '1.0.0'
16
16
  *
17
+ * 4-part strings are treated as equal to 3-part strings, e.g.: '1.2.3.4' === '1.2.3'
18
+ *
17
19
  * @experimental
18
20
  */
19
21
  export class Semver {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.93.2",
4
+ "version": "15.93.3",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -1016,8 +1016,7 @@ class LocalTimeFactory {
1016
1016
  */
1017
1017
  isTimezoneValid(tz: string): boolean {
1018
1018
  if (tz === 'UTC') return true // we deliberately consider UTC a valid timezone, while it's mostly used in testing
1019
- // oxlint-disable-next-line no-restricted-globals
1020
- return Intl.supportedValuesOf('timeZone').includes(tz)
1019
+ return Intl2.getSupportedTimezones().includes(tz as IANATimezone)
1021
1020
  }
1022
1021
 
1023
1022
  sort(items: LocalTime[], dir: SortDirection = 'asc', opt: MutateOptions = {}): LocalTime[] {
package/src/intl/intl.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { _Memo } from '../decorators/memo.decorator.js'
2
- import type { IANATimezone } from '../types.js'
2
+ import type { AnyObject, IANATimezone } from '../types.js'
3
3
 
4
4
  // oxlint-disable no-restricted-globals
5
5
 
@@ -11,18 +11,37 @@ import type { IANATimezone } from '../types.js'
11
11
  *
12
12
  * Methods accept non-optional arguments consciously,
13
13
  * to be able to cache them better. Just pass {} for options.
14
+ *
15
+ * The cache is never evicted, it grows with each unique combination of arguments.
16
+ * Don't pass unbounded dynamic values (e.g arbitrary user input) as locales or options.
14
17
  */
15
18
  class MemoizedIntl {
16
19
  /**
17
20
  * Returns the IANA timezone e.g `Europe/Stockholm`.
18
21
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
22
+ *
23
+ * Memoized, so it returns the timezone at the time of the first call.
24
+ * Changing `process.env.TZ` at runtime after that is not reflected.
19
25
  */
20
26
  @_Memo()
21
27
  getTimezone(): IANATimezone {
22
28
  return Intl.DateTimeFormat().resolvedOptions().timeZone as IANATimezone
23
29
  }
24
30
 
31
+ /**
32
+ * Returns the list of IANA timezones supported by the runtime (~420 entries).
33
+ * Memoized, because V8 rebuilds the list on every call (~70µs).
34
+ *
35
+ * The list contains canonical CLDR ids only, so valid aliases are missing from it,
36
+ * e.g `UTC` and `Europe/Kyiv` (listed as `Europe/Kiev`).
37
+ * Don't use it to validate user input as is.
38
+ */
25
39
  @_Memo()
40
+ getSupportedTimezones(): readonly IANATimezone[] {
41
+ return Intl.supportedValuesOf('timeZone') as IANATimezone[]
42
+ }
43
+
44
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
26
45
  DateTimeFormat(
27
46
  locales: Intl.LocalesArgument,
28
47
  options: Intl.DateTimeFormatOptions,
@@ -30,7 +49,7 @@ class MemoizedIntl {
30
49
  return new Intl.DateTimeFormat(locales, options)
31
50
  }
32
51
 
33
- @_Memo()
52
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
34
53
  RelativeTimeFormat(
35
54
  locales: Intl.LocalesArgument,
36
55
  options: Intl.RelativeTimeFormatOptions,
@@ -38,7 +57,7 @@ class MemoizedIntl {
38
57
  return new Intl.RelativeTimeFormat(locales, options)
39
58
  }
40
59
 
41
- @_Memo()
60
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
42
61
  DurationFormat(
43
62
  locales: Intl.LocalesArgument,
44
63
  options: Intl.DurationFormatOptions,
@@ -46,7 +65,7 @@ class MemoizedIntl {
46
65
  return new Intl.DurationFormat(locales, options)
47
66
  }
48
67
 
49
- @_Memo()
68
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
50
69
  NumberFormat(
51
70
  locales: Intl.LocalesArgument,
52
71
  options: Intl.NumberFormatOptions,
@@ -54,22 +73,22 @@ class MemoizedIntl {
54
73
  return new Intl.NumberFormat(locales, options)
55
74
  }
56
75
 
57
- @_Memo()
76
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
58
77
  Collator(locales: Intl.LocalesArgument, options: Intl.CollatorOptions): Intl.Collator {
59
78
  return new Intl.Collator(locales, options)
60
79
  }
61
80
 
62
- @_Memo()
81
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
63
82
  PluralRules(locales: Intl.LocalesArgument, options: Intl.PluralRulesOptions): Intl.PluralRules {
64
83
  return new Intl.PluralRules(locales, options)
65
84
  }
66
85
 
67
- @_Memo()
86
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
68
87
  ListFormat(locales: Intl.LocalesArgument, options: Intl.ListFormatOptions): Intl.ListFormat {
69
88
  return new Intl.ListFormat(locales, options)
70
89
  }
71
90
 
72
- @_Memo()
91
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
73
92
  DisplayNames(
74
93
  locales: Intl.LocalesArgument,
75
94
  options: Intl.DisplayNamesOptions,
@@ -77,10 +96,27 @@ class MemoizedIntl {
77
96
  return new Intl.DisplayNames(locales, options)
78
97
  }
79
98
 
80
- @_Memo()
99
+ @_Memo({ cacheKeyFn: intlCacheKeyFn })
81
100
  Segmenter(locales: Intl.LocalesArgument, options: Intl.SegmenterOptions): Intl.Segmenter {
82
101
  return new Intl.Segmenter(locales, options)
83
102
  }
84
103
  }
85
104
 
86
105
  export const Intl2 = new MemoizedIntl()
106
+
107
+ /**
108
+ * Default `jsonMemoSerializer` would serialize `Intl.Locale` instances as `{}`,
109
+ * making all Locale arguments share one cache key. Convert them to their string tags instead.
110
+ *
111
+ * Options keys are serialized in sorted order, so that `{ a, b }` and `{ b, a }` share one cache key.
112
+ * Passing a keys array as replacer works, because Intl options are flat objects.
113
+ */
114
+ function intlCacheKeyFn([locales, options]: [Intl.LocalesArgument, AnyObject]): string {
115
+ return JSON.stringify(localesKey(locales)) + JSON.stringify(options, Object.keys(options).sort())
116
+ }
117
+
118
+ function localesKey(locales: Intl.LocalesArgument): string | string[] | undefined {
119
+ if (locales === undefined) return
120
+ if (Array.isArray(locales)) return locales.map(String)
121
+ return String(locales)
122
+ }
package/src/semver.ts CHANGED
@@ -20,6 +20,8 @@ export type SemverTokens = [major: number, minor: number, patch: number]
20
20
  *
21
21
  * _semver('1').toString() === '1.0.0'
22
22
  *
23
+ * 4-part strings are treated as equal to 3-part strings, e.g.: '1.2.3.4' === '1.2.3'
24
+ *
23
25
  * @experimental
24
26
  */
25
27
  export class Semver {