-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
397 lines (372 loc) · 10.3 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
/**
* External dependencies
*/
import momentLib from 'moment';
import 'moment-timezone/moment-timezone';
import 'moment-timezone/moment-timezone-utils';
const WP_ZONE = 'WP';
// 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_US',
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' },
},
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: {
future: dateSettings.l10n.relative.future,
past: dateSettings.l10n.relative.past,
s: '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',
},
} );
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), in the site'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 date( dateFormat, dateValue = new Date() ) {
const offset = settings.timezone.offset * HOUR_IN_MINUTES;
const dateMoment = momentLib( dateValue ).utcOffset( offset, true );
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.
*/
export function gmdate( dateFormat, dateValue = new Date() ) {
const dateMoment = momentLib( dateValue ).utc();
return format( dateFormat, dateMoment );
}
/**
* Formats a date (like `dateI18n()` 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 {boolean} gmt True for GMT/UTC, false for
* site's timezone.
*
* @return {string} Formatted date.
*/
export function dateI18n( dateFormat, dateValue = new Date(), gmt = false ) {
// Defaults.
const offset = gmt ? 0 : settings.timezone.offset * HOUR_IN_MINUTES;
// Convert to moment object.
const dateMoment = momentLib( dateValue ).utcOffset( offset, true );
// Set the locale.
dateMoment.locale( settings.l10n.locale );
// Format and return.
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();
}
setupWPTimezone();