@prairielearn/formatter 1.4.0 → 1.4.2
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 +12 -0
- package/dist/date.d.ts +8 -2
- package/dist/date.js +55 -14
- package/dist/date.js.map +1 -1
- package/dist/date.test.js +281 -78
- package/dist/date.test.js.map +1 -1
- package/package.json +5 -5
- package/src/date.test.ts +408 -121
- package/src/date.ts +74 -10
package/dist/date.test.js
CHANGED
|
@@ -203,6 +203,8 @@ describe('date formatting', () => {
|
|
|
203
203
|
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 2));
|
|
204
204
|
const date = new Date(Date.UTC(2018, 0, 1, 13, 34, 7));
|
|
205
205
|
assert.equal(formatDateFriendly(date, 'UTC', { baseDate }), 'today, 1:34:07pm (UTC)');
|
|
206
|
+
assert.equal(formatDateFriendly(date, 'UTC', { baseDate, maxPrecision: 'minute' }), 'today, 1:34pm (UTC)');
|
|
207
|
+
assert.equal(formatDateFriendly(date, 'UTC', { baseDate, maxPrecision: 'hour' }), 'today, 1pm (UTC)');
|
|
206
208
|
});
|
|
207
209
|
it('should handle a time with minutes', () => {
|
|
208
210
|
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 2));
|
|
@@ -224,84 +226,285 @@ describe('date formatting', () => {
|
|
|
224
226
|
const date = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
225
227
|
assert.equal(formatDateFriendly(date, 'UTC', { baseDate }), 'today, 12pm (UTC)');
|
|
226
228
|
});
|
|
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
|
-
|
|
229
|
+
describe('maxPrecision option', () => {
|
|
230
|
+
it('should limit to hour precision when maxPrecision is "hour"', () => {
|
|
231
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 17));
|
|
232
|
+
const date = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
233
|
+
assert.equal(formatDateFriendly(date, 'UTC', {
|
|
234
|
+
baseDate,
|
|
235
|
+
maxPrecision: 'hour',
|
|
236
|
+
timeOnly: true,
|
|
237
|
+
includeTz: false,
|
|
238
|
+
}), '3pm');
|
|
239
|
+
});
|
|
240
|
+
it('should limit to minute precision when maxPrecision is "minute"', () => {
|
|
241
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 17));
|
|
242
|
+
const date = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
243
|
+
assert.equal(formatDateFriendly(date, 'UTC', {
|
|
244
|
+
baseDate,
|
|
245
|
+
maxPrecision: 'minute',
|
|
246
|
+
timeOnly: true,
|
|
247
|
+
includeTz: false,
|
|
248
|
+
}), '3:45pm');
|
|
249
|
+
});
|
|
250
|
+
it('should show second precision when maxPrecision is "second"', () => {
|
|
251
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 17));
|
|
252
|
+
const date = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
253
|
+
assert.equal(formatDateFriendly(date, 'UTC', {
|
|
254
|
+
baseDate,
|
|
255
|
+
maxPrecision: 'second',
|
|
256
|
+
timeOnly: true,
|
|
257
|
+
includeTz: false,
|
|
258
|
+
}), '3:45:17pm');
|
|
259
|
+
});
|
|
260
|
+
describe('minPrecision option', () => {
|
|
261
|
+
it('should sometimes show minutes when minPrecision is "hour"', () => {
|
|
262
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
263
|
+
const date1 = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
264
|
+
assert.equal(formatDateFriendly(date1, 'UTC', {
|
|
265
|
+
baseDate,
|
|
266
|
+
minPrecision: 'hour',
|
|
267
|
+
timeOnly: true,
|
|
268
|
+
includeTz: false,
|
|
269
|
+
}), '3pm');
|
|
270
|
+
const date2 = new Date(Date.UTC(2018, 0, 1, 15, 1, 0));
|
|
271
|
+
assert.equal(formatDateFriendly(date2, 'UTC', {
|
|
272
|
+
baseDate,
|
|
273
|
+
minPrecision: 'hour',
|
|
274
|
+
timeOnly: true,
|
|
275
|
+
includeTz: false,
|
|
276
|
+
}), '3:01pm');
|
|
277
|
+
const date3 = new Date(Date.UTC(2018, 0, 1, 15, 0, 1));
|
|
278
|
+
assert.equal(formatDateFriendly(date3, 'UTC', {
|
|
279
|
+
baseDate,
|
|
280
|
+
minPrecision: 'hour',
|
|
281
|
+
timeOnly: true,
|
|
282
|
+
includeTz: false,
|
|
283
|
+
}), '3:00:01pm');
|
|
284
|
+
});
|
|
285
|
+
it('should always show minutes when minPrecision is "minute"', () => {
|
|
286
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
287
|
+
const date = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
288
|
+
assert.equal(formatDateFriendly(date, 'UTC', {
|
|
289
|
+
baseDate,
|
|
290
|
+
minPrecision: 'minute',
|
|
291
|
+
timeOnly: true,
|
|
292
|
+
includeTz: false,
|
|
293
|
+
}), '3:00pm');
|
|
294
|
+
const date2 = new Date(Date.UTC(2018, 0, 1, 15, 1, 0));
|
|
295
|
+
assert.equal(formatDateFriendly(date2, 'UTC', {
|
|
296
|
+
baseDate,
|
|
297
|
+
minPrecision: 'minute',
|
|
298
|
+
timeOnly: true,
|
|
299
|
+
includeTz: false,
|
|
300
|
+
}), '3:01pm');
|
|
301
|
+
const date3 = new Date(Date.UTC(2018, 0, 1, 15, 0, 1));
|
|
302
|
+
assert.equal(formatDateFriendly(date3, 'UTC', {
|
|
303
|
+
baseDate,
|
|
304
|
+
minPrecision: 'minute',
|
|
305
|
+
timeOnly: true,
|
|
306
|
+
includeTz: false,
|
|
307
|
+
}), '3:00:01pm');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
describe('precision option combinations', () => {
|
|
311
|
+
it('should work with fixed precision (min=max)', () => {
|
|
312
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
313
|
+
const date1 = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
314
|
+
assert.equal(formatDateFriendly(date1, 'UTC', {
|
|
315
|
+
baseDate,
|
|
316
|
+
maxPrecision: 'hour',
|
|
317
|
+
minPrecision: 'hour',
|
|
318
|
+
timeOnly: true,
|
|
319
|
+
includeTz: false,
|
|
320
|
+
}), '3pm');
|
|
321
|
+
assert.equal(formatDateFriendly(date1, 'UTC', {
|
|
322
|
+
baseDate,
|
|
323
|
+
maxPrecision: 'minute',
|
|
324
|
+
minPrecision: 'minute',
|
|
325
|
+
timeOnly: true,
|
|
326
|
+
includeTz: false,
|
|
327
|
+
}), '3:00pm');
|
|
328
|
+
assert.equal(formatDateFriendly(date1, 'UTC', {
|
|
329
|
+
baseDate,
|
|
330
|
+
maxPrecision: 'second',
|
|
331
|
+
minPrecision: 'second',
|
|
332
|
+
timeOnly: true,
|
|
333
|
+
includeTz: false,
|
|
334
|
+
}), '3:00:00pm');
|
|
335
|
+
const date2 = new Date(Date.UTC(2018, 0, 1, 15, 30, 45));
|
|
336
|
+
assert.equal(formatDateFriendly(date2, 'UTC', {
|
|
337
|
+
baseDate,
|
|
338
|
+
maxPrecision: 'hour',
|
|
339
|
+
minPrecision: 'hour',
|
|
340
|
+
timeOnly: true,
|
|
341
|
+
includeTz: false,
|
|
342
|
+
}), '3pm');
|
|
343
|
+
assert.equal(formatDateFriendly(date2, 'UTC', {
|
|
344
|
+
baseDate,
|
|
345
|
+
maxPrecision: 'minute',
|
|
346
|
+
minPrecision: 'minute',
|
|
347
|
+
timeOnly: true,
|
|
348
|
+
includeTz: false,
|
|
349
|
+
}), '3:30pm');
|
|
350
|
+
assert.equal(formatDateFriendly(date2, 'UTC', {
|
|
351
|
+
baseDate,
|
|
352
|
+
maxPrecision: 'second',
|
|
353
|
+
minPrecision: 'second',
|
|
354
|
+
timeOnly: true,
|
|
355
|
+
includeTz: false,
|
|
356
|
+
}), '3:30:45pm');
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
describe('precision with full date formatting', () => {
|
|
360
|
+
it('should work with full date and time formatting', () => {
|
|
361
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
362
|
+
const date = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
363
|
+
assert.equal(formatDateFriendly(date, 'UTC', { baseDate, maxPrecision: 'minute' }), 'today, 3:45pm (UTC)');
|
|
364
|
+
});
|
|
365
|
+
it('should work with timeFirst option', () => {
|
|
366
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
367
|
+
const date = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
368
|
+
assert.equal(formatDateFriendly(date, 'UTC', {
|
|
369
|
+
baseDate,
|
|
370
|
+
minPrecision: 'minute',
|
|
371
|
+
timeFirst: true,
|
|
372
|
+
}), '3:00pm today (UTC)');
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
describe('formatDateRangeFriendly()', () => {
|
|
377
|
+
it('should handle two different dates', () => {
|
|
378
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
379
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
380
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
381
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate }), 'today, 12:34pm to Wed, Jan\u00a03, 10am (UTC)');
|
|
382
|
+
});
|
|
383
|
+
it('should handle the same date with different times', () => {
|
|
384
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
385
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
386
|
+
const end = new Date(Date.UTC(2018, 0, 1, 13, 0, 0));
|
|
387
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate }), 'today, 12:34pm to 1pm (UTC)');
|
|
388
|
+
});
|
|
389
|
+
it('should handle the same date with the same time', () => {
|
|
390
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
391
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
392
|
+
const end = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
393
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate }), 'today, 12:34pm (UTC)');
|
|
394
|
+
});
|
|
395
|
+
it('should handle two different dates with the time first', () => {
|
|
396
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
397
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
398
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
399
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate, timeFirst: true }), '12:34pm today to 10am Wed, Jan\u00a03 (UTC)');
|
|
400
|
+
});
|
|
401
|
+
it('should handle the same date with different times with the time first', () => {
|
|
402
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
403
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
404
|
+
const end = new Date(Date.UTC(2018, 0, 1, 13, 0, 0));
|
|
405
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate, timeFirst: true }), '12:34pm to 1pm today (UTC)');
|
|
406
|
+
});
|
|
407
|
+
it('should handle the same date with the same time and the time first', () => {
|
|
408
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
409
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
410
|
+
const end = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
411
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate, timeFirst: true }), '12:34pm today (UTC)');
|
|
412
|
+
});
|
|
413
|
+
it('should handle two different dates without the timezone', () => {
|
|
414
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
415
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
416
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
417
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', { baseDate, includeTz: false }), 'today, 12:34pm to Wed, Jan\u00a03, 10am');
|
|
418
|
+
});
|
|
419
|
+
it('should handle two different dates with only dates and without the timezone', () => {
|
|
420
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
421
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
422
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
423
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
424
|
+
baseDate,
|
|
425
|
+
dateOnly: true,
|
|
426
|
+
includeTz: false,
|
|
427
|
+
}), 'today to Wed, Jan\u00a03');
|
|
428
|
+
});
|
|
429
|
+
it('should handle two different dates with time first and without the timezone', () => {
|
|
430
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
431
|
+
const start = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
432
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
433
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
434
|
+
baseDate,
|
|
435
|
+
timeFirst: true,
|
|
436
|
+
includeTz: false,
|
|
437
|
+
}), '12:34pm today to 10am Wed, Jan\u00a03');
|
|
438
|
+
});
|
|
439
|
+
it('should handle two different dates in CST', () => {
|
|
440
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
441
|
+
const start = new Date(Date.UTC(2018, 0, 1, 0, 34, 0));
|
|
442
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
443
|
+
assert.equal(formatDateRangeFriendly(start, end, 'America/Chicago', { baseDate }), 'yesterday, 6:34pm to Wed, Jan\u00a03, 4am (CST)');
|
|
444
|
+
});
|
|
445
|
+
it('should handle two different dates in CST without the timezone', () => {
|
|
446
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 34, 0));
|
|
447
|
+
const start = new Date(Date.UTC(2018, 0, 1, 0, 34, 0));
|
|
448
|
+
const end = new Date(Date.UTC(2018, 0, 3, 10, 0, 0));
|
|
449
|
+
assert.equal(formatDateRangeFriendly(start, end, 'America/Chicago', {
|
|
450
|
+
baseDate,
|
|
451
|
+
includeTz: false,
|
|
452
|
+
}), 'yesterday, 6:34pm to Wed, Jan\u00a03, 4am');
|
|
453
|
+
});
|
|
454
|
+
describe('precision options', () => {
|
|
455
|
+
it('should apply maxPrecision to both start and end times', () => {
|
|
456
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
457
|
+
const start = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
458
|
+
const end = new Date(Date.UTC(2018, 0, 1, 17, 30, 45));
|
|
459
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
460
|
+
baseDate,
|
|
461
|
+
maxPrecision: 'minute',
|
|
462
|
+
includeTz: false,
|
|
463
|
+
}), 'today, 3:45pm to 5:30pm');
|
|
464
|
+
});
|
|
465
|
+
it('should apply minPrecision to both start and end times', () => {
|
|
466
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
467
|
+
const start = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
468
|
+
const end = new Date(Date.UTC(2018, 0, 1, 17, 0, 0));
|
|
469
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
470
|
+
baseDate,
|
|
471
|
+
minPrecision: 'minute',
|
|
472
|
+
includeTz: false,
|
|
473
|
+
}), 'today, 3:00pm to 5:00pm');
|
|
474
|
+
});
|
|
475
|
+
it('should handle precision options with different dates', () => {
|
|
476
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
477
|
+
const start = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
478
|
+
const end = new Date(Date.UTC(2018, 0, 2, 10, 30, 45));
|
|
479
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
480
|
+
baseDate,
|
|
481
|
+
maxPrecision: 'hour',
|
|
482
|
+
includeTz: false,
|
|
483
|
+
}), 'today, 3pm to tomorrow, 10am');
|
|
484
|
+
});
|
|
485
|
+
it('should handle precision options with same times', () => {
|
|
486
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
487
|
+
const start = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
488
|
+
const end = new Date(Date.UTC(2018, 0, 1, 15, 0, 0));
|
|
489
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
490
|
+
baseDate,
|
|
491
|
+
maxPrecision: 'second',
|
|
492
|
+
minPrecision: 'second',
|
|
493
|
+
includeTz: false,
|
|
494
|
+
}), 'today, 3:00:00pm');
|
|
495
|
+
});
|
|
496
|
+
it('should work with timeFirst and precision options', () => {
|
|
497
|
+
const baseDate = new Date(Date.UTC(2018, 0, 1, 12, 0, 0));
|
|
498
|
+
const start = new Date(Date.UTC(2018, 0, 1, 15, 45, 17));
|
|
499
|
+
const end = new Date(Date.UTC(2018, 0, 1, 17, 30, 45));
|
|
500
|
+
assert.equal(formatDateRangeFriendly(start, end, 'UTC', {
|
|
501
|
+
baseDate,
|
|
502
|
+
maxPrecision: 'minute',
|
|
503
|
+
timeFirst: true,
|
|
504
|
+
includeTz: false,
|
|
505
|
+
}), '3:45pm to 5:30pm today');
|
|
506
|
+
});
|
|
507
|
+
});
|
|
305
508
|
});
|
|
306
509
|
});
|
|
307
510
|
});
|