@commercetools-uikit/calendar-time-utils 20.3.0 → 20.3.1

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.
@@ -9,6 +9,7 @@ var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/inst
9
9
  var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/map');
10
10
  var _Array$from = require('@babel/runtime-corejs3/core-js-stable/array/from');
11
11
  var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/starts-with');
12
+ var _someInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/some');
12
13
  var _Map = require('@babel/runtime-corejs3/core-js-stable/map');
13
14
  var _Object$entries = require('@babel/runtime-corejs3/core-js-stable/object/entries');
14
15
  var _reduceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/reduce');
@@ -23,6 +24,7 @@ var _sliceInstanceProperty__default = /*#__PURE__*/_interopDefault(_sliceInstanc
23
24
  var _mapInstanceProperty__default = /*#__PURE__*/_interopDefault(_mapInstanceProperty);
24
25
  var _Array$from__default = /*#__PURE__*/_interopDefault(_Array$from);
25
26
  var _startsWithInstanceProperty__default = /*#__PURE__*/_interopDefault(_startsWithInstanceProperty);
27
+ var _someInstanceProperty__default = /*#__PURE__*/_interopDefault(_someInstanceProperty);
26
28
  var _Map__default = /*#__PURE__*/_interopDefault(_Map);
27
29
  var _Object$entries__default = /*#__PURE__*/_interopDefault(_Object$entries);
28
30
  var _reduceInstanceProperty__default = /*#__PURE__*/_interopDefault(_reduceInstanceProperty);
@@ -194,6 +196,58 @@ const parseInputText = (text, locale, timeZone) => {
194
196
  }
195
197
  return '';
196
198
  };
199
+ const getIntlDatePattern = locale => {
200
+ const formatter = new Intl.DateTimeFormat(locale, {
201
+ year: 'numeric',
202
+ month: '2-digit',
203
+ day: '2-digit'
204
+ });
205
+ const parts = formatter.formatToParts(new Date(2020, 0, 15));
206
+ return _mapInstanceProperty__default["default"](parts).call(parts, part => {
207
+ switch (part.type) {
208
+ case 'day':
209
+ return 'DD';
210
+ case 'month':
211
+ return 'MM';
212
+ case 'year':
213
+ return 'YYYY';
214
+ case 'literal':
215
+ return part.value;
216
+ default:
217
+ return '';
218
+ }
219
+ }).join('');
220
+ };
221
+ const getIntlTimePattern = locale => {
222
+ const formatter = new Intl.DateTimeFormat(locale, {
223
+ timeStyle: 'short'
224
+ });
225
+ // Use 2:30 AM so the hour is single-digit, allowing us to detect padding
226
+ const parts = formatter.formatToParts(new Date(2020, 0, 15, 2, 30));
227
+ const hasDayPeriod = _someInstanceProperty__default["default"](parts).call(parts, p => p.type === 'dayPeriod');
228
+ return _mapInstanceProperty__default["default"](parts).call(parts, part => {
229
+ switch (part.type) {
230
+ case 'hour':
231
+ {
232
+ const padded = part.value.length === 2;
233
+ if (hasDayPeriod) {
234
+ return padded ? 'hh' : 'h';
235
+ }
236
+ return padded ? 'HH' : 'H';
237
+ }
238
+ case 'minute':
239
+ return 'mm';
240
+ case 'dayPeriod':
241
+ return 'A';
242
+ case 'literal':
243
+ // Normalize unicode whitespace (e.g. U+202F narrow no-break space)
244
+ // to regular spaces for consistent placeholder text
245
+ return part.value.replace(/\s/g, ' ');
246
+ default:
247
+ return '';
248
+ }
249
+ }).join('');
250
+ };
197
251
  const localizedDateFormatPatternCache = new _Map__default["default"]();
198
252
  const getLocalizedDateTimeFormatPattern = function (locale) {
199
253
  var _context3, _context4;
@@ -202,21 +256,16 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
202
256
  if (localizedDateFormatPatternCache.has(key)) {
203
257
  return localizedDateFormatPatternCache.get(key);
204
258
  }
205
-
206
- // References:
207
- // https://momentjs.com/docs/#/i18n/locale-data/
208
- // https://momentjs.com/docs/#/displaying/ ("Localized formats" section)
209
- const localeData = moment__default["default"]().locale(locale).localeData();
210
259
  let localizedFormat = '';
211
260
  switch (formatType) {
212
261
  case 'date':
213
- localizedFormat = localeData.longDateFormat('L');
262
+ localizedFormat = getIntlDatePattern(locale);
214
263
  break;
215
264
  case 'time':
216
- localizedFormat = localeData.longDateFormat('LT');
265
+ localizedFormat = getIntlTimePattern(locale);
217
266
  break;
218
267
  case 'full':
219
- localizedFormat = _concatInstanceProperty__default["default"](_context4 = "".concat(localeData.longDateFormat('L'), " - ")).call(_context4, localeData.longDateFormat('LT'));
268
+ localizedFormat = _concatInstanceProperty__default["default"](_context4 = "".concat(getIntlDatePattern(locale), " - ")).call(_context4, getIntlTimePattern(locale));
220
269
  break;
221
270
  default:
222
271
  throw new Error("CalendarTime.getLocalizedDateTimeFormatPattern: Unknown format type '".concat(formatType, "'"));
@@ -230,7 +279,7 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
230
279
  const localeMappings = _Object$entries__default["default"](DATE_FORMAT_LOCALIZED_MAPPINGS[locale] || DATE_FORMAT_LOCALIZED_MAPPINGS[languageCode] || {});
231
280
 
232
281
  // In case we don't have a translation for the locale, we fallback to the
233
- // pattern from the moment locale data.
282
+ // Intl-derived pattern.
234
283
  let pattern = localizedFormat;
235
284
  if (localeMappings && localeMappings.length > 0) {
236
285
  pattern = _reduceInstanceProperty__default["default"](localeMappings).call(localeMappings, (localizedPattern, _ref) => {
@@ -245,7 +294,7 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
245
294
  };
246
295
 
247
296
  // NOTE: This string will be replaced on build time with the package version.
248
- var version = "20.3.0";
297
+ var version = "20.3.1";
249
298
 
250
299
  exports.changeDateInMonth = changeDateInMonth;
251
300
  exports.changeMonth = changeMonth;
@@ -9,6 +9,7 @@ var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/inst
9
9
  var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/map');
10
10
  var _Array$from = require('@babel/runtime-corejs3/core-js-stable/array/from');
11
11
  var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/starts-with');
12
+ var _someInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/some');
12
13
  var _Map = require('@babel/runtime-corejs3/core-js-stable/map');
13
14
  var _Object$entries = require('@babel/runtime-corejs3/core-js-stable/object/entries');
14
15
  var _reduceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/reduce');
@@ -23,6 +24,7 @@ var _sliceInstanceProperty__default = /*#__PURE__*/_interopDefault(_sliceInstanc
23
24
  var _mapInstanceProperty__default = /*#__PURE__*/_interopDefault(_mapInstanceProperty);
24
25
  var _Array$from__default = /*#__PURE__*/_interopDefault(_Array$from);
25
26
  var _startsWithInstanceProperty__default = /*#__PURE__*/_interopDefault(_startsWithInstanceProperty);
27
+ var _someInstanceProperty__default = /*#__PURE__*/_interopDefault(_someInstanceProperty);
26
28
  var _Map__default = /*#__PURE__*/_interopDefault(_Map);
27
29
  var _Object$entries__default = /*#__PURE__*/_interopDefault(_Object$entries);
28
30
  var _reduceInstanceProperty__default = /*#__PURE__*/_interopDefault(_reduceInstanceProperty);
@@ -193,6 +195,58 @@ const parseInputText = (text, locale, timeZone) => {
193
195
  }
194
196
  return '';
195
197
  };
198
+ const getIntlDatePattern = locale => {
199
+ const formatter = new Intl.DateTimeFormat(locale, {
200
+ year: 'numeric',
201
+ month: '2-digit',
202
+ day: '2-digit'
203
+ });
204
+ const parts = formatter.formatToParts(new Date(2020, 0, 15));
205
+ return _mapInstanceProperty__default["default"](parts).call(parts, part => {
206
+ switch (part.type) {
207
+ case 'day':
208
+ return 'DD';
209
+ case 'month':
210
+ return 'MM';
211
+ case 'year':
212
+ return 'YYYY';
213
+ case 'literal':
214
+ return part.value;
215
+ default:
216
+ return '';
217
+ }
218
+ }).join('');
219
+ };
220
+ const getIntlTimePattern = locale => {
221
+ const formatter = new Intl.DateTimeFormat(locale, {
222
+ timeStyle: 'short'
223
+ });
224
+ // Use 2:30 AM so the hour is single-digit, allowing us to detect padding
225
+ const parts = formatter.formatToParts(new Date(2020, 0, 15, 2, 30));
226
+ const hasDayPeriod = _someInstanceProperty__default["default"](parts).call(parts, p => p.type === 'dayPeriod');
227
+ return _mapInstanceProperty__default["default"](parts).call(parts, part => {
228
+ switch (part.type) {
229
+ case 'hour':
230
+ {
231
+ const padded = part.value.length === 2;
232
+ if (hasDayPeriod) {
233
+ return padded ? 'hh' : 'h';
234
+ }
235
+ return padded ? 'HH' : 'H';
236
+ }
237
+ case 'minute':
238
+ return 'mm';
239
+ case 'dayPeriod':
240
+ return 'A';
241
+ case 'literal':
242
+ // Normalize unicode whitespace (e.g. U+202F narrow no-break space)
243
+ // to regular spaces for consistent placeholder text
244
+ return part.value.replace(/\s/g, ' ');
245
+ default:
246
+ return '';
247
+ }
248
+ }).join('');
249
+ };
196
250
  const localizedDateFormatPatternCache = new _Map__default["default"]();
197
251
  const getLocalizedDateTimeFormatPattern = function (locale) {
198
252
  var _context3, _context4;
@@ -201,21 +255,16 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
201
255
  if (localizedDateFormatPatternCache.has(key)) {
202
256
  return localizedDateFormatPatternCache.get(key);
203
257
  }
204
-
205
- // References:
206
- // https://momentjs.com/docs/#/i18n/locale-data/
207
- // https://momentjs.com/docs/#/displaying/ ("Localized formats" section)
208
- const localeData = moment__default["default"]().locale(locale).localeData();
209
258
  let localizedFormat = '';
210
259
  switch (formatType) {
211
260
  case 'date':
212
- localizedFormat = localeData.longDateFormat('L');
261
+ localizedFormat = getIntlDatePattern(locale);
213
262
  break;
214
263
  case 'time':
215
- localizedFormat = localeData.longDateFormat('LT');
264
+ localizedFormat = getIntlTimePattern(locale);
216
265
  break;
217
266
  case 'full':
218
- localizedFormat = _concatInstanceProperty__default["default"](_context4 = "".concat(localeData.longDateFormat('L'), " - ")).call(_context4, localeData.longDateFormat('LT'));
267
+ localizedFormat = _concatInstanceProperty__default["default"](_context4 = "".concat(getIntlDatePattern(locale), " - ")).call(_context4, getIntlTimePattern(locale));
219
268
  break;
220
269
  default:
221
270
  throw new Error("CalendarTime.getLocalizedDateTimeFormatPattern: Unknown format type '".concat(formatType, "'"));
@@ -229,7 +278,7 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
229
278
  const localeMappings = _Object$entries__default["default"](DATE_FORMAT_LOCALIZED_MAPPINGS[locale] || DATE_FORMAT_LOCALIZED_MAPPINGS[languageCode] || {});
230
279
 
231
280
  // In case we don't have a translation for the locale, we fallback to the
232
- // pattern from the moment locale data.
281
+ // Intl-derived pattern.
233
282
  let pattern = localizedFormat;
234
283
  if (localeMappings && localeMappings.length > 0) {
235
284
  pattern = _reduceInstanceProperty__default["default"](localeMappings).call(localeMappings, (localizedPattern, _ref) => {
@@ -244,7 +293,7 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
244
293
  };
245
294
 
246
295
  // NOTE: This string will be replaced on build time with the package version.
247
- var version = "20.3.0";
296
+ var version = "20.3.1";
248
297
 
249
298
  exports.changeDateInMonth = changeDateInMonth;
250
299
  exports.changeMonth = changeMonth;
@@ -5,6 +5,7 @@ import _sliceInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instan
5
5
  import _mapInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/map';
6
6
  import _Array$from from '@babel/runtime-corejs3/core-js-stable/array/from';
7
7
  import _startsWithInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/starts-with';
8
+ import _someInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/some';
8
9
  import _Map from '@babel/runtime-corejs3/core-js-stable/map';
9
10
  import _Object$entries from '@babel/runtime-corejs3/core-js-stable/object/entries';
10
11
  import _reduceInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/reduce';
@@ -177,6 +178,58 @@ const parseInputText = (text, locale, timeZone) => {
177
178
  }
178
179
  return '';
179
180
  };
181
+ const getIntlDatePattern = locale => {
182
+ const formatter = new Intl.DateTimeFormat(locale, {
183
+ year: 'numeric',
184
+ month: '2-digit',
185
+ day: '2-digit'
186
+ });
187
+ const parts = formatter.formatToParts(new Date(2020, 0, 15));
188
+ return _mapInstanceProperty(parts).call(parts, part => {
189
+ switch (part.type) {
190
+ case 'day':
191
+ return 'DD';
192
+ case 'month':
193
+ return 'MM';
194
+ case 'year':
195
+ return 'YYYY';
196
+ case 'literal':
197
+ return part.value;
198
+ default:
199
+ return '';
200
+ }
201
+ }).join('');
202
+ };
203
+ const getIntlTimePattern = locale => {
204
+ const formatter = new Intl.DateTimeFormat(locale, {
205
+ timeStyle: 'short'
206
+ });
207
+ // Use 2:30 AM so the hour is single-digit, allowing us to detect padding
208
+ const parts = formatter.formatToParts(new Date(2020, 0, 15, 2, 30));
209
+ const hasDayPeriod = _someInstanceProperty(parts).call(parts, p => p.type === 'dayPeriod');
210
+ return _mapInstanceProperty(parts).call(parts, part => {
211
+ switch (part.type) {
212
+ case 'hour':
213
+ {
214
+ const padded = part.value.length === 2;
215
+ if (hasDayPeriod) {
216
+ return padded ? 'hh' : 'h';
217
+ }
218
+ return padded ? 'HH' : 'H';
219
+ }
220
+ case 'minute':
221
+ return 'mm';
222
+ case 'dayPeriod':
223
+ return 'A';
224
+ case 'literal':
225
+ // Normalize unicode whitespace (e.g. U+202F narrow no-break space)
226
+ // to regular spaces for consistent placeholder text
227
+ return part.value.replace(/\s/g, ' ');
228
+ default:
229
+ return '';
230
+ }
231
+ }).join('');
232
+ };
180
233
  const localizedDateFormatPatternCache = new _Map();
181
234
  const getLocalizedDateTimeFormatPattern = function (locale) {
182
235
  var _context3, _context4;
@@ -185,21 +238,16 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
185
238
  if (localizedDateFormatPatternCache.has(key)) {
186
239
  return localizedDateFormatPatternCache.get(key);
187
240
  }
188
-
189
- // References:
190
- // https://momentjs.com/docs/#/i18n/locale-data/
191
- // https://momentjs.com/docs/#/displaying/ ("Localized formats" section)
192
- const localeData = moment().locale(locale).localeData();
193
241
  let localizedFormat = '';
194
242
  switch (formatType) {
195
243
  case 'date':
196
- localizedFormat = localeData.longDateFormat('L');
244
+ localizedFormat = getIntlDatePattern(locale);
197
245
  break;
198
246
  case 'time':
199
- localizedFormat = localeData.longDateFormat('LT');
247
+ localizedFormat = getIntlTimePattern(locale);
200
248
  break;
201
249
  case 'full':
202
- localizedFormat = _concatInstanceProperty(_context4 = "".concat(localeData.longDateFormat('L'), " - ")).call(_context4, localeData.longDateFormat('LT'));
250
+ localizedFormat = _concatInstanceProperty(_context4 = "".concat(getIntlDatePattern(locale), " - ")).call(_context4, getIntlTimePattern(locale));
203
251
  break;
204
252
  default:
205
253
  throw new Error("CalendarTime.getLocalizedDateTimeFormatPattern: Unknown format type '".concat(formatType, "'"));
@@ -213,7 +261,7 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
213
261
  const localeMappings = _Object$entries(DATE_FORMAT_LOCALIZED_MAPPINGS[locale] || DATE_FORMAT_LOCALIZED_MAPPINGS[languageCode] || {});
214
262
 
215
263
  // In case we don't have a translation for the locale, we fallback to the
216
- // pattern from the moment locale data.
264
+ // Intl-derived pattern.
217
265
  let pattern = localizedFormat;
218
266
  if (localeMappings && localeMappings.length > 0) {
219
267
  pattern = _reduceInstanceProperty(localeMappings).call(localeMappings, (localizedPattern, _ref) => {
@@ -228,6 +276,6 @@ const getLocalizedDateTimeFormatPattern = function (locale) {
228
276
  };
229
277
 
230
278
  // NOTE: This string will be replaced on build time with the package version.
231
- var version = "20.3.0";
279
+ var version = "20.3.1";
232
280
 
233
281
  export { changeDateInMonth, changeMonth, changeTime, createCalendarItems, createItemDateTimeToString, createSuggestedItems, formatDate, formatDefaultTime, formatTime, getCalendarDayLabel, getDateInMonth, getDaysInMonth, getLocalizedDateTimeFormatPattern, getMonthCalendarLabel, getPaddingDayCount, getPreviousDay, getStartOf, getToday, getWeekdayNames, getYearCalendarLabel, isSameDay, parseInputText, version };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@commercetools-uikit/calendar-time-utils",
3
3
  "description": "Utilities to work with time values for a calendar.",
4
- "version": "20.3.0",
4
+ "version": "20.3.1",
5
5
  "bugs": "https://github.com/commercetools/ui-kit/issues",
6
6
  "repository": {
7
7
  "type": "git",
@@ -21,7 +21,7 @@
21
21
  "dependencies": {
22
22
  "@babel/runtime": "^7.20.13",
23
23
  "@babel/runtime-corejs3": "^7.20.13",
24
- "@commercetools-uikit/utils": "20.3.0"
24
+ "@commercetools-uikit/utils": "20.3.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "moment-timezone": "0.6.0"