@wordpress/date 4.39.0 → 4.40.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 4.40.0 (2023-08-16)
6
+
5
7
  ## 4.39.0 (2023-08-10)
6
8
 
7
9
  ## 4.38.0 (2023-07-20)
package/build/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
@@ -16,15 +15,10 @@ exports.gmdateI18n = gmdateI18n;
16
15
  exports.humanTimeDiff = humanTimeDiff;
17
16
  exports.isInTheFuture = isInTheFuture;
18
17
  exports.setSettings = setSettings;
19
-
20
18
  var _moment = _interopRequireDefault(require("moment"));
21
-
22
19
  require("moment-timezone/moment-timezone");
23
-
24
20
  require("moment-timezone/moment-timezone-utils");
25
-
26
21
  var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
27
-
28
22
  /**
29
23
  * External dependencies
30
24
  */
@@ -34,7 +28,6 @@ var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
34
28
  */
35
29
 
36
30
  /** @typedef {import('moment').Moment} Moment */
37
-
38
31
  /** @typedef {import('moment').LocaleSpecification} MomentLocaleSpecification */
39
32
 
40
33
  /**
@@ -61,7 +54,6 @@ var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
61
54
  */
62
55
 
63
56
  /* eslint-disable jsdoc/valid-types */
64
-
65
57
  /**
66
58
  * @typedef L10nSettings
67
59
  * @property {string} locale Moment locale.
@@ -73,7 +65,6 @@ var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
73
65
  * @property {MomentLocaleSpecification['relativeTime']} relative Relative time config.
74
66
  * @property {0|1|2|3|4|5|6} startOfWeek Day that the week starts on.
75
67
  */
76
-
77
68
  /* eslint-enable jsdoc/valid-types */
78
69
 
79
70
  /**
@@ -82,14 +73,16 @@ var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
82
73
  * @property {FormatsConfig} formats Date/time formats config.
83
74
  * @property {TimezoneConfig} timezone Timezone settings.
84
75
  */
85
- const WP_ZONE = 'WP'; // This regular expression tests positive for UTC offsets as described in ISO 8601.
76
+
77
+ const WP_ZONE = 'WP';
78
+
79
+ // This regular expression tests positive for UTC offsets as described in ISO 8601.
86
80
  // See: https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
81
+ const VALID_UTC_OFFSET = /^[+-][0-1][0-9](:?[0-9][0-9])?$/;
87
82
 
88
- const VALID_UTC_OFFSET = /^[+-][0-1][0-9](:?[0-9][0-9])?$/; // Changes made here will likely need to be made in `lib/client-assets.php` as
83
+ // Changes made here will likely need to be made in `lib/client-assets.php` as
89
84
  // well because it uses the `setSettings()` function to change these settings.
90
-
91
85
  /** @type {DateSettings} */
92
-
93
86
  let settings = {
94
87
  l10n: {
95
88
  locale: 'en',
@@ -133,16 +126,17 @@ let settings = {
133
126
  abbr: ''
134
127
  }
135
128
  };
129
+
136
130
  /**
137
131
  * Adds a locale to moment, using the format supplied by `wp_localize_script()`.
138
132
  *
139
133
  * @param {DateSettings} dateSettings Settings, including locale data.
140
134
  */
141
-
142
135
  function setSettings(dateSettings) {
143
136
  settings = dateSettings;
144
- setupWPTimezone(); // Does moment already have a locale with the right name?
137
+ setupWPTimezone();
145
138
 
139
+ // Does moment already have a locale with the right name?
146
140
  if (_moment.default.locales().includes(dateSettings.l10n.locale)) {
147
141
  // Is that locale misconfigured, e.g. because we are on a site running
148
142
  // WordPress < 6.0?
@@ -154,12 +148,12 @@ function setSettings(dateSettings) {
154
148
  // We have a properly configured locale, so no need to create one.
155
149
  return;
156
150
  }
157
- } // defineLocale() will modify the current locale, so back it up.
158
-
159
-
160
- const currentLocale = _moment.default.locale(); // Create locale.
151
+ }
161
152
 
153
+ // defineLocale() will modify the current locale, so back it up.
154
+ const currentLocale = _moment.default.locale();
162
155
 
156
+ // Create locale.
163
157
  _moment.default.defineLocale(dateSettings.l10n.locale, {
164
158
  // Inherit anything missing from English. We don't load
165
159
  // moment-with-locales.js so English is all there is.
@@ -168,15 +162,12 @@ function setSettings(dateSettings) {
168
162
  monthsShort: dateSettings.l10n.monthsShort,
169
163
  weekdays: dateSettings.l10n.weekdays,
170
164
  weekdaysShort: dateSettings.l10n.weekdaysShort,
171
-
172
165
  meridiem(hour, minute, isLowercase) {
173
166
  if (hour < 12) {
174
167
  return isLowercase ? dateSettings.l10n.meridiem.am : dateSettings.l10n.meridiem.AM;
175
168
  }
176
-
177
169
  return isLowercase ? dateSettings.l10n.meridiem.pm : dateSettings.l10n.meridiem.PM;
178
170
  },
179
-
180
171
  longDateFormat: {
181
172
  LT: dateSettings.formats.time,
182
173
  LTS: _moment.default.localeData('en').longDateFormat('LTS'),
@@ -188,29 +179,27 @@ function setSettings(dateSettings) {
188
179
  // From human_time_diff?
189
180
  // Set to `(number, withoutSuffix, key, isFuture) => {}` instead.
190
181
  relativeTime: dateSettings.l10n.relative
191
- }); // Restore the locale to what it was.
192
-
182
+ });
193
183
 
184
+ // Restore the locale to what it was.
194
185
  _moment.default.locale(currentLocale);
195
186
  }
187
+
196
188
  /**
197
189
  * Returns the currently defined date settings.
198
190
  *
199
191
  * @return {DateSettings} Settings, including locale data.
200
192
  */
201
-
202
-
203
193
  function getSettings() {
204
194
  return settings;
205
195
  }
196
+
206
197
  /**
207
198
  * Returns the currently defined date settings.
208
199
  *
209
200
  * @deprecated
210
201
  * @return {DateSettings} Settings, including locale data.
211
202
  */
212
-
213
-
214
203
  function __experimentalGetSettings() {
215
204
  (0, _deprecated.default)('wp.date.__experimentalGetSettings', {
216
205
  since: '6.1',
@@ -218,7 +207,6 @@ function __experimentalGetSettings() {
218
207
  });
219
208
  return getSettings();
220
209
  }
221
-
222
210
  function setupWPTimezone() {
223
211
  // Create WP timezone based off dateSettings.
224
212
  _moment.default.tz.add(_moment.default.tz.pack({
@@ -227,30 +215,28 @@ function setupWPTimezone() {
227
215
  untils: [null],
228
216
  offsets: [-settings.timezone.offset * 60 || 0]
229
217
  }));
230
- } // Date constants.
218
+ }
231
219
 
220
+ // Date constants.
232
221
  /**
233
222
  * Number of seconds in one minute.
234
223
  *
235
224
  * @type {number}
236
225
  */
237
-
238
-
239
226
  const MINUTE_IN_SECONDS = 60;
240
227
  /**
241
228
  * Number of minutes in one hour.
242
229
  *
243
230
  * @type {number}
244
231
  */
245
-
246
232
  const HOUR_IN_MINUTES = 60;
247
233
  /**
248
234
  * Number of seconds in one hour.
249
235
  *
250
236
  * @type {number}
251
237
  */
252
-
253
238
  const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
239
+
254
240
  /**
255
241
  * Map of PHP formats to Moment.js formats.
256
242
  *
@@ -261,7 +247,6 @@ const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
261
247
  * This should only be used through {@link wp.date.format}, not
262
248
  * directly.
263
249
  */
264
-
265
250
  const formatMap = {
266
251
  // Day.
267
252
  d: 'DD',
@@ -269,7 +254,6 @@ const formatMap = {
269
254
  j: 'D',
270
255
  l: 'dddd',
271
256
  N: 'E',
272
-
273
257
  /**
274
258
  * Gets the ordinal suffix.
275
259
  *
@@ -283,9 +267,7 @@ const formatMap = {
283
267
  const withOrdinal = momentDate.format('Do');
284
268
  return withOrdinal.replace(num, '');
285
269
  },
286
-
287
270
  w: 'd',
288
-
289
271
  /**
290
272
  * Gets the day of the year (zero-indexed).
291
273
  *
@@ -297,7 +279,6 @@ const formatMap = {
297
279
  // DDD - 1.
298
280
  return (parseInt(momentDate.format('DDD'), 10) - 1).toString();
299
281
  },
300
-
301
282
  // Week.
302
283
  W: 'W',
303
284
  // Month.
@@ -305,7 +286,6 @@ const formatMap = {
305
286
  m: 'MM',
306
287
  M: 'MMM',
307
288
  n: 'M',
308
-
309
289
  /**
310
290
  * Gets the days in the month.
311
291
  *
@@ -316,9 +296,7 @@ const formatMap = {
316
296
  t(momentDate) {
317
297
  return momentDate.daysInMonth();
318
298
  },
319
-
320
299
  // Year.
321
-
322
300
  /**
323
301
  * Gets whether the current year is a leap year.
324
302
  *
@@ -329,14 +307,12 @@ const formatMap = {
329
307
  L(momentDate) {
330
308
  return momentDate.isLeapYear() ? '1' : '0';
331
309
  },
332
-
333
310
  o: 'GGGG',
334
311
  Y: 'YYYY',
335
312
  y: 'YY',
336
313
  // Time.
337
314
  a: 'a',
338
315
  A: 'A',
339
-
340
316
  /**
341
317
  * Gets the current time in Swatch Internet Time (.beats).
342
318
  *
@@ -347,11 +323,10 @@ const formatMap = {
347
323
  B(momentDate) {
348
324
  const timezoned = (0, _moment.default)(momentDate).utcOffset(60);
349
325
  const seconds = parseInt(timezoned.format('s'), 10),
350
- minutes = parseInt(timezoned.format('m'), 10),
351
- hours = parseInt(timezoned.format('H'), 10);
326
+ minutes = parseInt(timezoned.format('m'), 10),
327
+ hours = parseInt(timezoned.format('H'), 10);
352
328
  return parseInt(((seconds + minutes * MINUTE_IN_SECONDS + hours * HOUR_IN_SECONDS) / 86.4).toString(), 10);
353
329
  },
354
-
355
330
  g: 'h',
356
331
  G: 'H',
357
332
  h: 'hh',
@@ -362,7 +337,6 @@ const formatMap = {
362
337
  v: 'SSS',
363
338
  // Timezone.
364
339
  e: 'zz',
365
-
366
340
  /**
367
341
  * Gets whether the timezone is in DST currently.
368
342
  *
@@ -373,11 +347,9 @@ const formatMap = {
373
347
  I(momentDate) {
374
348
  return momentDate.isDST() ? '1' : '0';
375
349
  },
376
-
377
350
  O: 'ZZ',
378
351
  P: 'Z',
379
352
  T: 'z',
380
-
381
353
  /**
382
354
  * Gets the timezone offset in seconds.
383
355
  *
@@ -392,12 +364,9 @@ const formatMap = {
392
364
  const parts = offset.substring(1).split(':').map(n => parseInt(n, 10));
393
365
  return sign * (parts[0] * HOUR_IN_MINUTES + parts[1]) * MINUTE_IN_SECONDS;
394
366
  },
395
-
396
367
  // Full date/time.
397
368
  c: 'YYYY-MM-DDTHH:mm:ssZ',
398
-
399
369
  // .toISOString.
400
-
401
370
  /**
402
371
  * Formats the date as RFC2822.
403
372
  *
@@ -408,9 +377,9 @@ const formatMap = {
408
377
  r(momentDate) {
409
378
  return momentDate.locale('en').format('ddd, DD MMM YYYY HH:mm:ss ZZ');
410
379
  },
411
-
412
380
  U: 'X'
413
381
  };
382
+
414
383
  /**
415
384
  * Formats a date. Does not alter the date's timezone.
416
385
  *
@@ -421,27 +390,21 @@ const formatMap = {
421
390
  *
422
391
  * @return {string} Formatted date.
423
392
  */
424
-
425
393
  function format(dateFormat, dateValue = new Date()) {
426
394
  let i, char;
427
395
  const newFormat = [];
428
396
  const momentDate = (0, _moment.default)(dateValue);
429
-
430
397
  for (i = 0; i < dateFormat.length; i++) {
431
- char = dateFormat[i]; // Is this an escape?
432
-
398
+ char = dateFormat[i];
399
+ // Is this an escape?
433
400
  if ('\\' === char) {
434
401
  // Add next character, then move on.
435
402
  i++;
436
403
  newFormat.push('[' + dateFormat[i] + ']');
437
404
  continue;
438
405
  }
439
-
440
406
  if (char in formatMap) {
441
- const formatter = formatMap[
442
- /** @type {keyof formatMap} */
443
- char];
444
-
407
+ const formatter = formatMap[/** @type {keyof formatMap} */char];
445
408
  if (typeof formatter !== 'string') {
446
409
  // If the format is a function, call it.
447
410
  newFormat.push('[' + formatter(momentDate) + ']');
@@ -452,12 +415,12 @@ function format(dateFormat, dateValue = new Date()) {
452
415
  } else {
453
416
  newFormat.push('[' + char + ']');
454
417
  }
455
- } // Join with [] between to separate characters, and replace
418
+ }
419
+ // Join with [] between to separate characters, and replace
456
420
  // unneeded separators with static text.
457
-
458
-
459
421
  return momentDate.format(newFormat.join('[]'));
460
422
  }
423
+
461
424
  /**
462
425
  * Formats a date (like `date()` in PHP).
463
426
  *
@@ -474,12 +437,11 @@ function format(dateFormat, dateValue = new Date()) {
474
437
  *
475
438
  * @return {string} Formatted date in English.
476
439
  */
477
-
478
-
479
440
  function date(dateFormat, dateValue = new Date(), timezone) {
480
441
  const dateMoment = buildMoment(dateValue, timezone);
481
442
  return format(dateFormat, dateMoment);
482
443
  }
444
+
483
445
  /**
484
446
  * Formats a date (like `date()` in PHP), in the UTC timezone.
485
447
  *
@@ -490,12 +452,11 @@ function date(dateFormat, dateValue = new Date(), timezone) {
490
452
  *
491
453
  * @return {string} Formatted date in English.
492
454
  */
493
-
494
-
495
455
  function gmdate(dateFormat, dateValue = new Date()) {
496
456
  const dateMoment = (0, _moment.default)(dateValue).utc();
497
457
  return format(dateFormat, dateMoment);
498
458
  }
459
+
499
460
  /**
500
461
  * Formats a date (like `wp_date()` in PHP), translating it into site's locale.
501
462
  *
@@ -517,21 +478,18 @@ function gmdate(dateFormat, dateValue = new Date()) {
517
478
  *
518
479
  * @return {string} Formatted date.
519
480
  */
520
-
521
-
522
481
  function dateI18n(dateFormat, dateValue = new Date(), timezone) {
523
482
  if (true === timezone) {
524
483
  return gmdateI18n(dateFormat, dateValue);
525
484
  }
526
-
527
485
  if (false === timezone) {
528
486
  timezone = undefined;
529
487
  }
530
-
531
488
  const dateMoment = buildMoment(dateValue, timezone);
532
489
  dateMoment.locale(settings.l10n.locale);
533
490
  return format(dateFormat, dateMoment);
534
491
  }
492
+
535
493
  /**
536
494
  * Formats a date (like `wp_date()` in PHP), translating it into site's locale
537
495
  * and using the UTC timezone.
@@ -543,13 +501,12 @@ function dateI18n(dateFormat, dateValue = new Date(), timezone) {
543
501
  *
544
502
  * @return {string} Formatted date.
545
503
  */
546
-
547
-
548
504
  function gmdateI18n(dateFormat, dateValue = new Date()) {
549
505
  const dateMoment = (0, _moment.default)(dateValue).utc();
550
506
  dateMoment.locale(settings.l10n.locale);
551
507
  return format(dateFormat, dateMoment);
552
508
  }
509
+
553
510
  /**
554
511
  * Check whether a date is considered in the future according to the WordPress settings.
555
512
  *
@@ -557,15 +514,12 @@ function gmdateI18n(dateFormat, dateValue = new Date()) {
557
514
  *
558
515
  * @return {boolean} Is in the future.
559
516
  */
560
-
561
-
562
517
  function isInTheFuture(dateValue) {
563
518
  const now = _moment.default.tz(WP_ZONE);
564
-
565
519
  const momentObject = _moment.default.tz(dateValue, WP_ZONE);
566
-
567
520
  return momentObject.isAfter(now);
568
521
  }
522
+
569
523
  /**
570
524
  * Create and return a JavaScript Date Object from a date string in the WP timezone.
571
525
  *
@@ -573,15 +527,13 @@ function isInTheFuture(dateValue) {
573
527
  *
574
528
  * @return {Date} Date
575
529
  */
576
-
577
-
578
530
  function getDate(dateString) {
579
531
  if (!dateString) {
580
532
  return _moment.default.tz(WP_ZONE).toDate();
581
533
  }
582
-
583
534
  return _moment.default.tz(dateString, WP_ZONE).toDate();
584
535
  }
536
+
585
537
  /**
586
538
  * Returns a human-readable time difference between two dates, like human_time_diff() in PHP.
587
539
  *
@@ -590,14 +542,12 @@ function getDate(dateString) {
590
542
  *
591
543
  * @return {string} Human-readable time difference.
592
544
  */
593
-
594
-
595
545
  function humanTimeDiff(from, to) {
596
546
  const fromMoment = _moment.default.tz(from, WP_ZONE);
597
-
598
547
  const toMoment = to ? _moment.default.tz(to, WP_ZONE) : _moment.default.tz(WP_ZONE);
599
548
  return fromMoment.from(toMoment);
600
549
  }
550
+
601
551
  /**
602
552
  * Creates a moment instance using the given timezone or, if none is provided, using global settings.
603
553
  *
@@ -612,28 +562,21 @@ function humanTimeDiff(from, to) {
612
562
  *
613
563
  * @return {Moment} a moment instance.
614
564
  */
615
-
616
-
617
565
  function buildMoment(dateValue, timezone = '') {
618
566
  const dateMoment = (0, _moment.default)(dateValue);
619
-
620
567
  if (timezone && !isUTCOffset(timezone)) {
621
568
  // The ! isUTCOffset() check guarantees that timezone is a string.
622
- return dateMoment.tz(
623
- /** @type {string} */
624
- timezone);
569
+ return dateMoment.tz( /** @type {string} */timezone);
625
570
  }
626
-
627
571
  if (timezone && isUTCOffset(timezone)) {
628
572
  return dateMoment.utcOffset(timezone);
629
573
  }
630
-
631
574
  if (settings.timezone.string) {
632
575
  return dateMoment.tz(settings.timezone.string);
633
576
  }
634
-
635
577
  return dateMoment.utcOffset(+settings.timezone.offset);
636
578
  }
579
+
637
580
  /**
638
581
  * Returns whether a certain UTC offset is valid or not.
639
582
  *
@@ -641,15 +584,11 @@ function buildMoment(dateValue, timezone = '') {
641
584
  *
642
585
  * @return {boolean} whether a certain UTC offset is valid or not.
643
586
  */
644
-
645
-
646
587
  function isUTCOffset(offset) {
647
588
  if ('number' === typeof offset) {
648
589
  return true;
649
590
  }
650
-
651
591
  return VALID_UTC_OFFSET.test(offset);
652
592
  }
653
-
654
593
  setupWPTimezone();
655
594
  //# sourceMappingURL=index.js.map