@formatjs/intl 4.0.7 → 4.0.9

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/src/utils.js CHANGED
@@ -1,168 +1,125 @@
1
- import { __assign, __spreadArray } from "tslib";
2
- import { memoize, strategies } from '@formatjs/fast-memoize';
3
- import { IntlMessageFormat } from 'intl-messageformat';
4
- import { UnsupportedFormatterError } from './error.js';
5
- export function invariant(condition, message, Err) {
6
- if (Err === void 0) { Err = Error; }
7
- if (!condition) {
8
- throw new Err(message);
9
- }
1
+ import "@formatjs/ecma402-abstract";
2
+ import { memoize, strategies } from "@formatjs/fast-memoize";
3
+ import { IntlMessageFormat } from "intl-messageformat";
4
+ import { UnsupportedFormatterError } from "./error.js";
5
+ import "./types.js";
6
+ export function invariant(condition, message, Err = Error) {
7
+ if (!condition) {
8
+ throw new Err(message);
9
+ }
10
10
  }
11
- export function filterProps(props, allowlist, defaults) {
12
- if (defaults === void 0) { defaults = {}; }
13
- return allowlist.reduce(function (filtered, name) {
14
- if (name in props) {
15
- filtered[name] = props[name];
16
- }
17
- else if (name in defaults) {
18
- filtered[name] = defaults[name];
19
- }
20
- return filtered;
21
- }, {});
11
+ export function filterProps(props, allowlist, defaults = {}) {
12
+ return allowlist.reduce((filtered, name) => {
13
+ if (name in props) {
14
+ filtered[name] = props[name];
15
+ } else if (name in defaults) {
16
+ filtered[name] = defaults[name];
17
+ }
18
+ return filtered;
19
+ }, {});
22
20
  }
23
- var defaultErrorHandler = function (error) {
24
- // @ts-ignore just so we don't need to declare dep on @types/node
25
- if (process.env.NODE_ENV !== 'production') {
26
- console.error(error);
27
- }
21
+ const defaultErrorHandler = (error) => {
22
+ // @ts-ignore just so we don't need to declare dep on @types/node
23
+ if (process.env.NODE_ENV !== "production") {
24
+ console.error(error);
25
+ }
28
26
  };
29
- var defaultWarnHandler = function (warning) {
30
- // @ts-ignore just so we don't need to declare dep on @types/node
31
- if (process.env.NODE_ENV !== 'production') {
32
- console.warn(warning);
33
- }
27
+ const defaultWarnHandler = (warning) => {
28
+ // @ts-ignore just so we don't need to declare dep on @types/node
29
+ if (process.env.NODE_ENV !== "production") {
30
+ console.warn(warning);
31
+ }
34
32
  };
35
- export var DEFAULT_INTL_CONFIG = {
36
- formats: {},
37
- messages: {},
38
- timeZone: undefined,
39
- defaultLocale: 'en',
40
- defaultFormats: {},
41
- fallbackOnEmptyString: true,
42
- onError: defaultErrorHandler,
43
- onWarn: defaultWarnHandler,
33
+ export const DEFAULT_INTL_CONFIG = {
34
+ formats: {},
35
+ messages: {},
36
+ timeZone: undefined,
37
+ defaultLocale: "en",
38
+ defaultFormats: {},
39
+ fallbackOnEmptyString: true,
40
+ onError: defaultErrorHandler,
41
+ onWarn: defaultWarnHandler
44
42
  };
45
43
  export function createIntlCache() {
46
- return {
47
- dateTime: {},
48
- number: {},
49
- message: {},
50
- relativeTime: {},
51
- pluralRules: {},
52
- list: {},
53
- displayNames: {},
54
- };
44
+ return {
45
+ dateTime: {},
46
+ number: {},
47
+ message: {},
48
+ relativeTime: {},
49
+ pluralRules: {},
50
+ list: {},
51
+ displayNames: {}
52
+ };
55
53
  }
56
54
  function createFastMemoizeCache(store) {
57
- return {
58
- create: function () {
59
- return {
60
- get: function (key) {
61
- return store[key];
62
- },
63
- set: function (key, value) {
64
- store[key] = value;
65
- },
66
- };
67
- },
68
- };
55
+ return { create() {
56
+ return {
57
+ get(key) {
58
+ return store[key];
59
+ },
60
+ set(key, value) {
61
+ store[key] = value;
62
+ }
63
+ };
64
+ } };
69
65
  }
70
66
  /**
71
- * Create intl formatters and populate cache
72
- * @param cache explicit cache to prevent leaking memory
73
- */
74
- export function createFormatters(cache) {
75
- if (cache === void 0) { cache = createIntlCache(); }
76
- var RelativeTimeFormat = Intl.RelativeTimeFormat;
77
- var ListFormat = Intl.ListFormat;
78
- var DisplayNames = Intl.DisplayNames;
79
- var getDateTimeFormat = memoize(function () {
80
- var _a;
81
- var args = [];
82
- for (var _i = 0; _i < arguments.length; _i++) {
83
- args[_i] = arguments[_i];
84
- }
85
- return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
86
- }, {
87
- cache: createFastMemoizeCache(cache.dateTime),
88
- strategy: strategies.variadic,
89
- });
90
- var getNumberFormat = memoize(function () {
91
- var _a;
92
- var args = [];
93
- for (var _i = 0; _i < arguments.length; _i++) {
94
- args[_i] = arguments[_i];
95
- }
96
- return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
97
- }, {
98
- cache: createFastMemoizeCache(cache.number),
99
- strategy: strategies.variadic,
100
- });
101
- var getPluralRules = memoize(function () {
102
- var _a;
103
- var args = [];
104
- for (var _i = 0; _i < arguments.length; _i++) {
105
- args[_i] = arguments[_i];
106
- }
107
- return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
108
- }, {
109
- cache: createFastMemoizeCache(cache.pluralRules),
110
- strategy: strategies.variadic,
111
- });
112
- return {
113
- getDateTimeFormat: getDateTimeFormat,
114
- getNumberFormat: getNumberFormat,
115
- getMessageFormat: memoize(function (message, locales, overrideFormats, opts) {
116
- return new IntlMessageFormat(message, locales, overrideFormats, __assign({ formatters: {
117
- getNumberFormat: getNumberFormat,
118
- getDateTimeFormat: getDateTimeFormat,
119
- getPluralRules: getPluralRules,
120
- } }, opts));
121
- }, {
122
- cache: createFastMemoizeCache(cache.message),
123
- strategy: strategies.variadic,
124
- }),
125
- getRelativeTimeFormat: memoize(function () {
126
- var args = [];
127
- for (var _i = 0; _i < arguments.length; _i++) {
128
- args[_i] = arguments[_i];
129
- }
130
- return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat, __spreadArray([void 0], args, false)))();
131
- }, {
132
- cache: createFastMemoizeCache(cache.relativeTime),
133
- strategy: strategies.variadic,
134
- }),
135
- getPluralRules: getPluralRules,
136
- getListFormat: memoize(function () {
137
- var args = [];
138
- for (var _i = 0; _i < arguments.length; _i++) {
139
- args[_i] = arguments[_i];
140
- }
141
- return new (ListFormat.bind.apply(ListFormat, __spreadArray([void 0], args, false)))();
142
- }, {
143
- cache: createFastMemoizeCache(cache.list),
144
- strategy: strategies.variadic,
145
- }),
146
- getDisplayNames: memoize(function () {
147
- var args = [];
148
- for (var _i = 0; _i < arguments.length; _i++) {
149
- args[_i] = arguments[_i];
150
- }
151
- return new (DisplayNames.bind.apply(DisplayNames, __spreadArray([void 0], args, false)))();
152
- }, {
153
- cache: createFastMemoizeCache(cache.displayNames),
154
- strategy: strategies.variadic,
155
- }),
156
- };
67
+ * Create intl formatters and populate cache
68
+ * @param cache explicit cache to prevent leaking memory
69
+ */
70
+ export function createFormatters(cache = createIntlCache()) {
71
+ const RelativeTimeFormat = Intl.RelativeTimeFormat;
72
+ const ListFormat = Intl.ListFormat;
73
+ const DisplayNames = Intl.DisplayNames;
74
+ const getDateTimeFormat = memoize((...args) => new Intl.DateTimeFormat(...args), {
75
+ cache: createFastMemoizeCache(cache.dateTime),
76
+ strategy: strategies.variadic
77
+ });
78
+ const getNumberFormat = memoize((...args) => new Intl.NumberFormat(...args), {
79
+ cache: createFastMemoizeCache(cache.number),
80
+ strategy: strategies.variadic
81
+ });
82
+ const getPluralRules = memoize((...args) => new Intl.PluralRules(...args), {
83
+ cache: createFastMemoizeCache(cache.pluralRules),
84
+ strategy: strategies.variadic
85
+ });
86
+ return {
87
+ getDateTimeFormat,
88
+ getNumberFormat,
89
+ getMessageFormat: memoize((message, locales, overrideFormats, opts) => new IntlMessageFormat(message, locales, overrideFormats, {
90
+ formatters: {
91
+ getNumberFormat,
92
+ getDateTimeFormat,
93
+ getPluralRules
94
+ },
95
+ ...opts
96
+ }), {
97
+ cache: createFastMemoizeCache(cache.message),
98
+ strategy: strategies.variadic
99
+ }),
100
+ getRelativeTimeFormat: memoize((...args) => new RelativeTimeFormat(...args), {
101
+ cache: createFastMemoizeCache(cache.relativeTime),
102
+ strategy: strategies.variadic
103
+ }),
104
+ getPluralRules,
105
+ getListFormat: memoize((...args) => new ListFormat(...args), {
106
+ cache: createFastMemoizeCache(cache.list),
107
+ strategy: strategies.variadic
108
+ }),
109
+ getDisplayNames: memoize((...args) => new DisplayNames(...args), {
110
+ cache: createFastMemoizeCache(cache.displayNames),
111
+ strategy: strategies.variadic
112
+ })
113
+ };
157
114
  }
158
115
  export function getNamedFormat(formats, type, name, onError) {
159
- var formatType = formats && formats[type];
160
- var format;
161
- if (formatType) {
162
- format = formatType[name];
163
- }
164
- if (format) {
165
- return format;
166
- }
167
- onError(new UnsupportedFormatterError("No ".concat(type, " format named: ").concat(name)));
116
+ const formatType = formats && formats[type];
117
+ let format;
118
+ if (formatType) {
119
+ format = formatType[name];
120
+ }
121
+ if (format) {
122
+ return format;
123
+ }
124
+ onError(new UnsupportedFormatterError(`No ${type} format named: ${name}`));
168
125
  }