@formatjs/utils 1.6.3 → 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.
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { canonicalizeCountryCode } from './src/countryCodes';
2
2
  export { countriesUsingDefaultCurrency, defaultCurrency, } from './src/defaultCurrency';
3
3
  export { defaultLocale } from './src/defaultLocale';
4
+ export { defaultTimezone } from './src/defaultTimezone';
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultLocale = exports.defaultCurrency = exports.countriesUsingDefaultCurrency = exports.canonicalizeCountryCode = void 0;
3
+ exports.defaultTimezone = exports.defaultLocale = exports.defaultCurrency = exports.countriesUsingDefaultCurrency = exports.canonicalizeCountryCode = void 0;
4
4
  var countryCodes_1 = require("./src/countryCodes");
5
5
  Object.defineProperty(exports, "canonicalizeCountryCode", { enumerable: true, get: function () { return countryCodes_1.canonicalizeCountryCode; } });
6
6
  var defaultCurrency_1 = require("./src/defaultCurrency");
@@ -8,4 +8,6 @@ Object.defineProperty(exports, "countriesUsingDefaultCurrency", { enumerable: tr
8
8
  Object.defineProperty(exports, "defaultCurrency", { enumerable: true, get: function () { return defaultCurrency_1.defaultCurrency; } });
9
9
  var defaultLocale_1 = require("./src/defaultLocale");
10
10
  Object.defineProperty(exports, "defaultLocale", { enumerable: true, get: function () { return defaultLocale_1.defaultLocale; } });
11
+ var defaultTimezone_1 = require("./src/defaultTimezone");
12
+ Object.defineProperty(exports, "defaultTimezone", { enumerable: true, get: function () { return defaultTimezone_1.defaultTimezone; } });
11
13
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,mDAA0D;AAAlD,uHAAA,uBAAuB,OAAA;AAC/B,yDAG8B;AAF5B,gIAAA,6BAA6B,OAAA;AAC7B,kHAAA,eAAe,OAAA;AAEjB,qDAAiD;AAAzC,8GAAA,aAAa,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,mDAA0D;AAAlD,uHAAA,uBAAuB,OAAA;AAC/B,yDAG8B;AAF5B,gIAAA,6BAA6B,OAAA;AAC7B,kHAAA,eAAe,OAAA;AAEjB,qDAAiD;AAAzC,8GAAA,aAAa,OAAA;AACrB,yDAAqD;AAA7C,kHAAA,eAAe,OAAA"}
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { canonicalizeCountryCode } from './src/countryCodes';
2
2
  export { countriesUsingDefaultCurrency, defaultCurrency, } from './src/defaultCurrency';
3
3
  export { defaultLocale } from './src/defaultLocale';
4
+ export { defaultTimezone } from './src/defaultTimezone';
package/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { canonicalizeCountryCode } from './src/countryCodes';
2
2
  export { countriesUsingDefaultCurrency, defaultCurrency, } from './src/defaultCurrency';
3
3
  export { defaultLocale } from './src/defaultLocale';
4
+ export { defaultTimezone } from './src/defaultTimezone';
4
5
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,uBAAuB,EAAC,MAAM,oBAAoB,CAAA;AAC1D,OAAO,EACL,6BAA6B,EAC7B,eAAe,GAChB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,uBAAuB,EAAC,MAAM,oBAAoB,CAAA;AAC1D,OAAO,EACL,6BAA6B,EAC7B,eAAe,GAChB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,8 @@
1
+ type DTFParameters = ConstructorParameters<typeof Intl.DateTimeFormat>;
2
+ /**
3
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
4
+ * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
5
+ * @returns default timezone for the system
6
+ */
7
+ export declare function defaultTimezone(locales?: DTFParameters[0], options?: Pick<Intl.DateTimeFormatOptions, 'timeZoneName'>): string;
8
+ export {};
@@ -0,0 +1,54 @@
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();
30
+ /**
31
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
32
+ * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
33
+ * @returns default timezone for the system
34
+ */
35
+ export function defaultTimezone(locales, options) {
36
+ var _a;
37
+ var timeZoneName = options === null || options === void 0 ? void 0 : options.timeZoneName;
38
+ try {
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');
49
+ }
50
+ catch (e) {
51
+ return 'UTC';
52
+ }
53
+ }
54
+ //# sourceMappingURL=defaultTimezone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaultTimezone.js","sourceRoot":"","sources":["../../src/defaultTimezone.ts"],"names":[],"mappings":";AAAA,OAAO,EAAQ,OAAO,EAAE,UAAU,EAAC,MAAM,wBAAwB,CAAA;AAEjE,IAAM,KAAK,GAAwC,EAAE,CAAA;AAErD,SAAS,sBAAsB,CAC7B,KAAoC;IAEpC,OAAO;QACL,MAAM;YACJ,OAAO;gBACL,GAAG,YAAC,GAAG;oBACL,OAAO,KAAK,CAAC,GAAG,CAAC,CAAA;gBACnB,CAAC;gBACD,GAAG,YAAC,GAAG,EAAE,KAAK;oBACZ,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;gBACpB,CAAC;aACF,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAID,IAAM,iBAAiB,GACrB,OAAO,CAAC;;IAAC,cAAsB;SAAtB,UAAsB,EAAtB,qBAAsB,EAAtB,IAAsB;QAAtB,yBAAsB;;IAAK,YAAI,CAAA,KAAA,IAAI,CAAC,cAAc,CAAA,wCAAI,IAAI;AAA/B,CAAgC,EAAE;IACpE,KAAK,EAAE,sBAAsB,CAAC,KAAK,CAAC;IACpC,QAAQ,EAAE,UAAU,CAAC,QAAQ;CAC9B,CAAC,CAAA;AAEJ,IAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;AAEtB;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,OAA0B,EAC1B,OAA0D;;IAE1D,IAAM,YAAY,GAAG,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.6.3",
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
  }
@@ -0,0 +1,8 @@
1
+ type DTFParameters = ConstructorParameters<typeof Intl.DateTimeFormat>;
2
+ /**
3
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
4
+ * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
5
+ * @returns default timezone for the system
6
+ */
7
+ export declare function defaultTimezone(locales?: DTFParameters[0], options?: Pick<Intl.DateTimeFormatOptions, 'timeZoneName'>): string;
8
+ export {};
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultTimezone = defaultTimezone;
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();
33
+ /**
34
+ * Return the localized default timezone for the system. If `timeZoneName` is not supported, return the IANA timezone name.
35
+ * @param dateDateTimeFormatCreator creator fn for Intl.DateTimeFormat
36
+ * @returns default timezone for the system
37
+ */
38
+ function defaultTimezone(locales, options) {
39
+ var _a;
40
+ var timeZoneName = options === null || options === void 0 ? void 0 : options.timeZoneName;
41
+ try {
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');
52
+ }
53
+ catch (e) {
54
+ return 'UTC';
55
+ }
56
+ }
57
+ //# sourceMappingURL=defaultTimezone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaultTimezone.js","sourceRoot":"","sources":["defaultTimezone.ts"],"names":[],"mappings":";;A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