-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
527 lines (491 loc) · 13.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/**
* External dependencies
*/
import momentLib from 'moment';
import 'moment-timezone/moment-timezone';
import 'moment-timezone/moment-timezone-utils';
/** @typedef {import('moment').Moment} Moment */
const WP_ZONE = 'WP';
// This regular expression tests positive for UTC offsets as described in ISO 8601.
// See: https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
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
// well because it uses the `setSettings()` function to change these settings.
let settings = {
l10n: {
locale: 'en',
months: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
],
monthsShort: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
],
weekdays: [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
],
weekdaysShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
meridiem: { am: 'am', pm: 'pm', AM: 'AM', PM: 'PM' },
relative: {
future: '%s from now',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years',
},
},
formats: {
time: 'g: i a',
date: 'F j, Y',
datetime: 'F j, Y g: i a',
datetimeAbbreviated: 'M j, Y g: i a',
},
timezone: { offset: '0', string: '' },
};
/**
* Adds a locale to moment, using the format supplied by `wp_localize_script()`.
*
* @param {Object} dateSettings Settings, including locale data.
*/
export function setSettings( dateSettings ) {
settings = dateSettings;
// Backup and restore current locale.
const currentLocale = momentLib.locale();
momentLib.updateLocale( dateSettings.l10n.locale, {
// Inherit anything missing from the default locale.
parentLocale: currentLocale,
months: dateSettings.l10n.months,
monthsShort: dateSettings.l10n.monthsShort,
weekdays: dateSettings.l10n.weekdays,
weekdaysShort: dateSettings.l10n.weekdaysShort,
meridiem( hour, minute, isLowercase ) {
if ( hour < 12 ) {
return isLowercase
? dateSettings.l10n.meridiem.am
: dateSettings.l10n.meridiem.AM;
}
return isLowercase
? dateSettings.l10n.meridiem.pm
: dateSettings.l10n.meridiem.PM;
},
longDateFormat: {
LT: dateSettings.formats.time,
LTS: null,
L: null,
LL: dateSettings.formats.date,
LLL: dateSettings.formats.datetime,
LLLL: null,
},
// From human_time_diff?
// Set to `(number, withoutSuffix, key, isFuture) => {}` instead.
relativeTime: dateSettings.l10n.relative,
} );
momentLib.locale( currentLocale );
setupWPTimezone();
}
/**
* Returns the currently defined date settings.
*
* @return {Object} Settings, including locale data.
*/
export function __experimentalGetSettings() {
return settings;
}
function setupWPTimezone() {
// Create WP timezone based off dateSettings.
momentLib.tz.add(
momentLib.tz.pack( {
name: WP_ZONE,
abbrs: [ WP_ZONE ],
untils: [ null ],
offsets: [ -settings.timezone.offset * 60 || 0 ],
} )
);
}
// Date constants.
/**
* Number of seconds in one minute.
*
* @type {number}
*/
const MINUTE_IN_SECONDS = 60;
/**
* Number of minutes in one hour.
*
* @type {number}
*/
const HOUR_IN_MINUTES = 60;
/**
* Number of seconds in one hour.
*
* @type {number}
*/
const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
/**
* Map of PHP formats to Moment.js formats.
*
* These are used internally by {@link wp.date.format}, and are either
* a string representing the corresponding Moment.js format code, or a
* function which returns the formatted string.
*
* This should only be used through {@link wp.date.format}, not
* directly.
*
* @type {Object}
*/
const formatMap = {
// Day
d: 'DD',
D: 'ddd',
j: 'D',
l: 'dddd',
N: 'E',
/**
* Gets the ordinal suffix.
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
S( momentDate ) {
// Do - D
const num = momentDate.format( 'D' );
const withOrdinal = momentDate.format( 'Do' );
return withOrdinal.replace( num, '' );
},
w: 'd',
/**
* Gets the day of the year (zero-indexed).
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
z( momentDate ) {
// DDD - 1
return '' + parseInt( momentDate.format( 'DDD' ), 10 ) - 1;
},
// Week
W: 'W',
// Month
F: 'MMMM',
m: 'MM',
M: 'MMM',
n: 'M',
/**
* Gets the days in the month.
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
t( momentDate ) {
return momentDate.daysInMonth();
},
// Year
/**
* Gets whether the current year is a leap year.
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
L( momentDate ) {
return momentDate.isLeapYear() ? '1' : '0';
},
o: 'GGGG',
Y: 'YYYY',
y: 'YY',
// Time
a: 'a',
A: 'A',
/**
* Gets the current time in Swatch Internet Time (.beats).
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
B( momentDate ) {
const timezoned = momentLib( momentDate ).utcOffset( 60 );
const seconds = parseInt( timezoned.format( 's' ), 10 ),
minutes = parseInt( timezoned.format( 'm' ), 10 ),
hours = parseInt( timezoned.format( 'H' ), 10 );
return parseInt(
( seconds +
minutes * MINUTE_IN_SECONDS +
hours * HOUR_IN_SECONDS ) /
86.4,
10
);
},
g: 'h',
G: 'H',
h: 'hh',
H: 'HH',
i: 'mm',
s: 'ss',
u: 'SSSSSS',
v: 'SSS',
// Timezone
e: 'zz',
/**
* Gets whether the timezone is in DST currently.
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
I( momentDate ) {
return momentDate.isDST() ? '1' : '0';
},
O: 'ZZ',
P: 'Z',
T: 'z',
/**
* Gets the timezone offset in seconds.
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
Z( momentDate ) {
// Timezone offset in seconds.
const offset = momentDate.format( 'Z' );
const sign = offset[ 0 ] === '-' ? -1 : 1;
const parts = offset.substring( 1 ).split( ':' );
return (
sign *
( parts[ 0 ] * HOUR_IN_MINUTES + parts[ 1 ] ) *
MINUTE_IN_SECONDS
);
},
// Full date/time
c: 'YYYY-MM-DDTHH:mm:ssZ', // .toISOString
r: 'ddd, D MMM YYYY HH:mm:ss ZZ',
U: 'X',
};
/**
* Formats a date. Does not alter the date's timezone.
*
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
* @param {Date|string|Moment|null} dateValue Date object or string,
* parsable by moment.js.
*
* @return {string} Formatted date.
*/
export function format( dateFormat, dateValue = new Date() ) {
let i, char;
let newFormat = [];
const momentDate = momentLib( dateValue );
for ( i = 0; i < dateFormat.length; i++ ) {
char = dateFormat[ i ];
// Is this an escape?
if ( '\\' === char ) {
// Add next character, then move on.
i++;
newFormat.push( '[' + dateFormat[ i ] + ']' );
continue;
}
if ( char in formatMap ) {
if ( typeof formatMap[ char ] !== 'string' ) {
// If the format is a function, call it.
newFormat.push( '[' + formatMap[ char ]( momentDate ) + ']' );
} else {
// Otherwise, add as a formatting string.
newFormat.push( formatMap[ char ] );
}
} else {
newFormat.push( '[' + char + ']' );
}
}
// Join with [] between to separate characters, and replace
// unneeded separators with static text.
newFormat = newFormat.join( '[]' );
return momentDate.format( newFormat );
}
/**
* Formats a date (like `date()` in PHP).
*
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
* @param {Date|string|Moment|null} dateValue Date object or string, parsable
* by moment.js.
* @param {string|number|null} timezone Timezone to output result in or a
* UTC offset. Defaults to timezone from
* site.
*
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*
* @return {string} Formatted date in English.
*/
export function date( dateFormat, dateValue = new Date(), timezone ) {
const dateMoment = buildMoment( dateValue, timezone );
return format( dateFormat, dateMoment );
}
/**
* Formats a date (like `date()` in PHP), in the UTC timezone.
*
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
* @param {Date|string|Moment|null} dateValue Date object or string,
* parsable by moment.js.
*
* @return {string} Formatted date in English.
*/
export function gmdate( dateFormat, dateValue = new Date() ) {
const dateMoment = momentLib( dateValue ).utc();
return format( dateFormat, dateMoment );
}
/**
* Formats a date (like `wp_date()` in PHP), translating it into site's locale.
*
* Backward Compatibility Notice: if `timezone` is set to `true`, the function
* behaves like `gmdateI18n`.
*
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
* @param {Date|string|Moment|null} dateValue Date object or string, parsable by
* moment.js.
* @param {string|number|boolean|null} timezone Timezone to output result in or a
* UTC offset. Defaults to timezone from
* site. Notice: `boolean` is effectively
* deprecated, but still supported for
* backward compatibility reasons.
*
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*
* @return {string} Formatted date.
*/
export function dateI18n( dateFormat, dateValue = new Date(), timezone ) {
if ( true === timezone ) {
return gmdateI18n( dateFormat, dateValue );
}
if ( false === timezone ) {
timezone = undefined;
}
const dateMoment = buildMoment( dateValue, timezone );
dateMoment.locale( settings.l10n.locale );
return format( dateFormat, dateMoment );
}
/**
* Formats a date (like `wp_date()` in PHP), translating it into site's locale
* and using the UTC timezone.
*
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
* @param {Date|string|Moment|null} dateValue Date object or string,
* parsable by moment.js.
*
* @return {string} Formatted date.
*/
export function gmdateI18n( dateFormat, dateValue = new Date() ) {
const dateMoment = momentLib( dateValue ).utc();
dateMoment.locale( settings.l10n.locale );
return format( dateFormat, dateMoment );
}
/**
* Check whether a date is considered in the future according to the WordPress settings.
*
* @param {string} dateValue Date String or Date object in the Defined WP Timezone.
*
* @return {boolean} Is in the future.
*/
export function isInTheFuture( dateValue ) {
const now = momentLib.tz( WP_ZONE );
const momentObject = momentLib.tz( dateValue, WP_ZONE );
return momentObject.isAfter( now );
}
/**
* Create and return a JavaScript Date Object from a date string in the WP timezone.
*
* @param {string?} dateString Date formatted in the WP timezone.
*
* @return {Date} Date
*/
export function getDate( dateString ) {
if ( ! dateString ) {
return momentLib.tz( WP_ZONE ).toDate();
}
return momentLib.tz( dateString, WP_ZONE ).toDate();
}
/**
* Creates a moment instance using the given timezone or, if none is provided, using global settings.
*
* @param {Date|string|Moment|null} dateValue Date object or string, parsable
* by moment.js.
* @param {string|number|null} timezone Timezone to output result in or a
* UTC offset. Defaults to timezone from
* site.
*
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*
* @return {Moment} a moment instance.
*/
function buildMoment( dateValue, timezone = '' ) {
const dateMoment = momentLib( dateValue );
if ( timezone && ! isUTCOffset( timezone ) ) {
return dateMoment.tz( timezone );
}
if ( timezone && isUTCOffset( timezone ) ) {
return dateMoment.utcOffset( timezone );
}
if ( settings.timezone.string ) {
return dateMoment.tz( settings.timezone.string );
}
return dateMoment.utcOffset( settings.timezone.offset );
}
/**
* Returns whether a certain UTC offset is valid or not.
*
* @param {number|string} offset a UTC offset.
*
* @return {boolean} whether a certain UTC offset is valid or not.
*/
function isUTCOffset( offset ) {
if ( 'number' === typeof offset ) {
return true;
}
return VALID_UTC_OFFSET.test( offset );
}
setupWPTimezone();