2016-09-13 19:22:27 +00:00
|
|
|
import assert from 'assert';
|
2016-11-29 22:46:46 +00:00
|
|
|
import moment from 'moment-timezone';
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2019-01-31 00:35:33 +00:00
|
|
|
import cleanDatePublished, { cleanDateString } from './date-published';
|
2016-09-01 16:28:39 +00:00
|
|
|
|
|
|
|
describe('cleanDatePublished(dateString)', () => {
|
2016-11-29 22:46:46 +00:00
|
|
|
it('returns a date', () => {
|
2016-09-13 19:22:27 +00:00
|
|
|
const datePublished = cleanDatePublished('published: 1/1/2020');
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2019-01-21 09:53:50 +00:00
|
|
|
assert.equal(datePublished, moment('1/1/2020', 'MM/DD/YYYY').toISOString());
|
2016-09-13 19:22:27 +00:00
|
|
|
});
|
2016-09-01 16:28:39 +00:00
|
|
|
|
|
|
|
it('returns null if date is invalid', () => {
|
2016-09-13 19:22:27 +00:00
|
|
|
const datePublished = cleanDatePublished('blargh');
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2016-09-13 19:22:27 +00:00
|
|
|
assert.equal(datePublished, null);
|
|
|
|
});
|
2016-11-29 22:46:46 +00:00
|
|
|
|
|
|
|
it('handles timezones', () => {
|
|
|
|
// The JS date parser is forgiving, but
|
|
|
|
// it needs am/pm separated from a time
|
2019-01-17 00:03:36 +00:00
|
|
|
const datePublished = cleanDatePublished('November 29, 2016: 8:18 AM ET', {
|
|
|
|
timezone: 'America/New_York',
|
|
|
|
});
|
2016-11-29 22:46:46 +00:00
|
|
|
assert.equal(datePublished, '2016-11-29T13:18:00.000Z');
|
|
|
|
});
|
2017-02-08 18:00:17 +00:00
|
|
|
|
|
|
|
it('accepts a custom date format', () => {
|
|
|
|
// The JS date parser is forgiving, but
|
|
|
|
// it needs am/pm separated from a time
|
2019-01-17 00:03:36 +00:00
|
|
|
const datePublished = cleanDatePublished('Mon Aug 03 12:45:00 EDT 2015', {
|
|
|
|
timezone: 'America/New_York',
|
|
|
|
format: 'ddd MMM DD HH:mm:ss zz YYYY',
|
|
|
|
});
|
2017-02-08 18:00:17 +00:00
|
|
|
assert.equal(datePublished, '2015-08-03T16:45:00.000Z');
|
|
|
|
});
|
2016-09-13 19:22:27 +00:00
|
|
|
});
|
2016-09-01 16:28:39 +00:00
|
|
|
|
|
|
|
describe('cleanDateString(dateString)', () => {
|
|
|
|
it('removes "published" text from an datePublished string', () => {
|
2016-09-13 19:22:27 +00:00
|
|
|
const datePublished = cleanDateString('published: 1/1/2020');
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2016-09-13 19:22:27 +00:00
|
|
|
assert.equal(datePublished, '1/1/2020');
|
|
|
|
});
|
2016-09-01 16:28:39 +00:00
|
|
|
|
|
|
|
it('trims whitespace', () => {
|
2016-09-13 19:22:27 +00:00
|
|
|
const datePublished = cleanDateString(' 1/1/2020 ');
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2016-09-13 19:22:27 +00:00
|
|
|
assert.equal(datePublished, '1/1/2020');
|
|
|
|
});
|
2016-09-01 16:28:39 +00:00
|
|
|
|
|
|
|
it('puts a space b/w a time and am/pm', () => {
|
|
|
|
// The JS date parser is forgiving, but
|
|
|
|
// it needs am/pm separated from a time
|
2016-09-13 19:22:27 +00:00
|
|
|
const date1 = cleanDateString('1/1/2020 8:30am');
|
|
|
|
assert.equal(date1, '1/1/2020 8:30 am');
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2016-09-13 19:22:27 +00:00
|
|
|
const date2 = cleanDateString('8:30PM 1/1/2020');
|
|
|
|
assert.equal(date2, '8:30 PM 1/1/2020');
|
|
|
|
});
|
2016-09-01 16:28:39 +00:00
|
|
|
|
2016-09-09 13:58:27 +00:00
|
|
|
it('cleans the dots from a.m. or p.m.', () => {
|
|
|
|
// The JS date parser is forgiving, but
|
|
|
|
// it needs a.m./p.m. without dots
|
2016-09-13 19:22:27 +00:00
|
|
|
const date1 = cleanDateString('1/1/2020 8:30 a.m.');
|
|
|
|
assert.equal(date1, '1/1/2020 8:30 am');
|
|
|
|
});
|
2016-09-09 13:58:27 +00:00
|
|
|
|
2016-09-08 23:29:57 +00:00
|
|
|
it('can handle some tough timestamps', () => {
|
|
|
|
// The JS date parser is forgiving, but
|
|
|
|
// it needs am/pm separated from a time
|
2019-01-17 00:03:36 +00:00
|
|
|
const date1 = cleanDateString(
|
|
|
|
'This page was last modified on 15 April 2016, at 10:59.'
|
|
|
|
);
|
2016-09-13 19:22:27 +00:00
|
|
|
assert.equal(date1, '15 Apr 2016 10:59');
|
|
|
|
});
|
2016-11-29 00:58:21 +00:00
|
|
|
|
|
|
|
it('massages the T out', () => {
|
|
|
|
// The JS date parser is forgiving, but
|
|
|
|
// it needs am/pm separated from a time
|
|
|
|
const date1 = cleanDateString('2016-11-22T08:57-500');
|
|
|
|
assert.equal(date1, '2016 11 22 08:57 -500');
|
|
|
|
});
|
2016-09-13 19:22:27 +00:00
|
|
|
});
|