@formatjs/utils 1.7.0 → 1.8.0

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.
@@ -1,12 +1,8 @@
1
+ type DTFParameters = ConstructorParameters<typeof Intl.DateTimeFormat>;
1
2
  /**
2
- * Return the default timezone for the system.
3
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
3
4
  * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
4
5
  * @returns default timezone for the system
5
6
  */
6
- export declare function defaultTimezone(dateDateTimeFormatCreator?: () => Intl.DateTimeFormat): string;
7
- /**
8
- * Reset the default timezone.
9
- * Only for testing purposes.
10
- * @private
11
- */
12
- export declare function _resetDefaultTimezone(): void;
7
+ export declare function defaultTimezone(locales?: DTFParameters[0], options?: Pick<Intl.DateTimeFormatOptions, 'timeZoneName'>): string;
8
+ export {};
@@ -1,29 +1,54 @@
1
- var DEFAULT_TIMEZONE;
2
- var DEFAULT_DATETIMEFORMAT_CREATOR = function () { return new Intl.DateTimeFormat(); };
1
+ import { __spreadArray } from "tslib";
2
+ import { memoize, strategies } from '@formatjs/fast-memoize';
3
+ var cache = {};
4
+ function createFastMemoizeCache(store) {
5
+ return {
6
+ create: function () {
7
+ return {
8
+ get: function (key) {
9
+ return store[key];
10
+ },
11
+ set: function (key, value) {
12
+ store[key] = value;
13
+ },
14
+ };
15
+ },
16
+ };
17
+ }
18
+ var getDateTimeFormat = memoize(function () {
19
+ var _a;
20
+ var args = [];
21
+ for (var _i = 0; _i < arguments.length; _i++) {
22
+ args[_i] = arguments[_i];
23
+ }
24
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
25
+ }, {
26
+ cache: createFastMemoizeCache(cache),
27
+ strategy: strategies.variadic,
28
+ });
29
+ var now = Date.now();
3
30
  /**
4
- * Return the default timezone for the system.
31
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
5
32
  * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
6
33
  * @returns default timezone for the system
7
34
  */
8
- export function defaultTimezone(dateDateTimeFormatCreator) {
9
- if (dateDateTimeFormatCreator === void 0) { dateDateTimeFormatCreator = DEFAULT_DATETIMEFORMAT_CREATOR; }
10
- if (DEFAULT_TIMEZONE) {
11
- return DEFAULT_TIMEZONE;
12
- }
35
+ export function defaultTimezone(locales, options) {
36
+ var _a;
37
+ var timeZoneName = options === null || options === void 0 ? void 0 : options.timeZoneName;
13
38
  try {
14
- return (DEFAULT_TIMEZONE =
15
- dateDateTimeFormatCreator().resolvedOptions().timeZone);
39
+ var dtf = getDateTimeFormat(locales, {
40
+ timeZoneName: options === null || options === void 0 ? void 0 : options.timeZoneName,
41
+ });
42
+ // If there's no `timeZoneName` specified, return the IANA timezone name
43
+ if (!timeZoneName) {
44
+ return dtf.resolvedOptions().timeZone;
45
+ }
46
+ // If `timeZoneName` is specified, return the localized timezone name
47
+ return (((_a = dtf.formatToParts(now).find(function (p) { return p.type === 'timeZoneName'; })) === null || _a === void 0 ? void 0 : _a.value) ||
48
+ 'UTC');
16
49
  }
17
50
  catch (e) {
18
51
  return 'UTC';
19
52
  }
20
53
  }
21
- /**
22
- * Reset the default timezone.
23
- * Only for testing purposes.
24
- * @private
25
- */
26
- export function _resetDefaultTimezone() {
27
- DEFAULT_TIMEZONE = undefined;
28
- }
29
54
  //# sourceMappingURL=defaultTimezone.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"defaultTimezone.js","sourceRoot":"","sources":["../../src/defaultTimezone.ts"],"names":[],"mappings":"AAAA,IAAI,gBAAoC,CAAA;AAExC,IAAM,8BAA8B,GAAG,cAAM,OAAA,IAAI,IAAI,CAAC,cAAc,EAAE,EAAzB,CAAyB,CAAA;AAEtE;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,yBAA0D;IAA1D,0CAAA,EAAA,0DAA0D;IAE1D,IAAI,gBAAgB,EAAE,CAAC;QACrB,O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OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,CAAA;IAC1C,IAAI,CAAC;QACH,IAAM,GAAG,GAAG,iBAAiB,CAAC,OAAO,EAAE;YACrC,YAAY,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY;SACpC,CAAC,CAAA;QACF,wEAAwE;QACxE,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,GAAG,CAAC,eAAe,EAAE,CAAC,QAAQ,CAAA;QACvC,CAAC;QACD,qEAAqE;QACrE,OAAO,CACL,CAAA,MAAA,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,IAAI,KAAK,cAAc,EAAzB,CAAyB,CAAC,0CAAE,KAAK;YAClE,KAAK,CACN,CAAA;IACH,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/utils",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Collection of useful intl utilities",
5
5
  "main": "index.js",
6
6
  "module": "lib/index.js",
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "homepage": "https://github.com/formatjs/formatjs#readme",
23
23
  "dependencies": {
24
- "tslib": "2"
24
+ "tslib": "2",
25
+ "@formatjs/fast-memoize": "2.2.3"
25
26
  }
26
27
  }
@@ -1,12 +1,8 @@
1
+ type DTFParameters = ConstructorParameters<typeof Intl.DateTimeFormat>;
1
2
  /**
2
- * Return the default timezone for the system.
3
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
3
4
  * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
4
5
  * @returns default timezone for the system
5
6
  */
6
- export declare function defaultTimezone(dateDateTimeFormatCreator?: () => Intl.DateTimeFormat): string;
7
- /**
8
- * Reset the default timezone.
9
- * Only for testing purposes.
10
- * @private
11
- */
12
- export declare function _resetDefaultTimezone(): void;
7
+ export declare function defaultTimezone(locales?: DTFParameters[0], options?: Pick<Intl.DateTimeFormatOptions, 'timeZoneName'>): string;
8
+ export {};
@@ -1,33 +1,57 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultTimezone = defaultTimezone;
4
- exports._resetDefaultTimezone = _resetDefaultTimezone;
5
- var DEFAULT_TIMEZONE;
6
- var DEFAULT_DATETIMEFORMAT_CREATOR = function () { return new Intl.DateTimeFormat(); };
4
+ var tslib_1 = require("tslib");
5
+ var fast_memoize_1 = require("@formatjs/fast-memoize");
6
+ var cache = {};
7
+ function createFastMemoizeCache(store) {
8
+ return {
9
+ create: function () {
10
+ return {
11
+ get: function (key) {
12
+ return store[key];
13
+ },
14
+ set: function (key, value) {
15
+ store[key] = value;
16
+ },
17
+ };
18
+ },
19
+ };
20
+ }
21
+ var getDateTimeFormat = (0, fast_memoize_1.memoize)(function () {
22
+ var _a;
23
+ var args = [];
24
+ for (var _i = 0; _i < arguments.length; _i++) {
25
+ args[_i] = arguments[_i];
26
+ }
27
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
28
+ }, {
29
+ cache: createFastMemoizeCache(cache),
30
+ strategy: fast_memoize_1.strategies.variadic,
31
+ });
32
+ var now = Date.now();
7
33
  /**
8
- * Return the default timezone for the system.
34
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
9
35
  * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
10
36
  * @returns default timezone for the system
11
37
  */
12
- function defaultTimezone(dateDateTimeFormatCreator) {
13
- if (dateDateTimeFormatCreator === void 0) { dateDateTimeFormatCreator = DEFAULT_DATETIMEFORMAT_CREATOR; }
14
- if (DEFAULT_TIMEZONE) {
15
- return DEFAULT_TIMEZONE;
16
- }
38
+ function defaultTimezone(locales, options) {
39
+ var _a;
40
+ var timeZoneName = options === null || options === void 0 ? void 0 : options.timeZoneName;
17
41
  try {
18
- return (DEFAULT_TIMEZONE =
19
- dateDateTimeFormatCreator().resolvedOptions().timeZone);
42
+ var dtf = getDateTimeFormat(locales, {
43
+ timeZoneName: options === null || options === void 0 ? void 0 : options.timeZoneName,
44
+ });
45
+ // If there's no `timeZoneName` specified, return the IANA timezone name
46
+ if (!timeZoneName) {
47
+ return dtf.resolvedOptions().timeZone;
48
+ }
49
+ // If `timeZoneName` is specified, return the localized timezone name
50
+ return (((_a = dtf.formatToParts(now).find(function (p) { return p.type === 'timeZoneName'; })) === null || _a === void 0 ? void 0 : _a.value) ||
51
+ 'UTC');
20
52
  }
21
53
  catch (e) {
22
54
  return 'UTC';
23
55
  }
24
56
  }
25
- /**
26
- * Reset the default timezone.
27
- * Only for testing purposes.
28
- * @private
29
- */
30
- function _resetDefaultTimezone() {
31
- DEFAULT_TIMEZONE = undefined;
32
- }
33
57
  //# sourceMappingURL=defaultTimezone.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"defaultTimezone.js","sourceRoot":"","sources":["defaultTimezone.ts"],"names":[],"mappings":";;AASA,0CAYC;AAOD,sDAEC;AA9BD,IAAI,gBAAoC,CAAA;AAExC,IAAM,8BAA8B,GAAG,cAAM,OAAA,IAAI,IAAI,CAAC,cAAc,EAAE,EAAzB,CAAyB,CAAA;AAEtE;;;;GAIG;AACH,SAAgB,eAAe,CAC7B,yBAA0D;IAA1D,0CAAA,EAAA,0DAA0D;IAE1D,IAAI,gBAAgB,EAAE,CAAC;QACrB,OAAO,gBAAgB,CAAA;IACzB,CAAC;IACD,IAAI,CAAC;QACH,OAAO,CAAC,gBAAgB;YACtB,yBAAyB,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ,CAAC,CAAA;IAC3D,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,SAAgB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