feat: allow parser to define custom date formats (#141)

* feat: allow parser to define custom date formats

* feat: updating macrumors to test/verify format working correctly
pull/142/head
Adam Pash 8 years ago committed by David A. Viramontes
parent 11f466ccb3
commit 08b5bb7ff1

@ -23,29 +23,29 @@ export function cleanDateString(dateString) {
.trim(); .trim();
} }
export function createDate(dateString, timezone) { export function createDate(dateString, timezone, format) {
if (TIME_WITH_OFFSET_RE.test(dateString)) { if (TIME_WITH_OFFSET_RE.test(dateString)) {
return moment(new Date(dateString)); return moment(new Date(dateString));
} }
return timezone ? return timezone ?
moment.tz(dateString, parseFormat(dateString), timezone) : moment.tz(dateString, format || parseFormat(dateString), timezone) :
moment(dateString, parseFormat(dateString)); moment(dateString, format || parseFormat(dateString));
} }
// Take a date published string, and hopefully return a date out of // Take a date published string, and hopefully return a date out of
// it. Return none if we fail. // it. Return none if we fail.
export default function cleanDatePublished(dateString, { timezone } = {}) { export default function cleanDatePublished(dateString, { timezone, format } = {}) {
// If string is in milliseconds or seconds, convert to int and return // If string is in milliseconds or seconds, convert to int and return
if (MS_DATE_STRING.test(dateString) || SEC_DATE_STRING.test(dateString)) { if (MS_DATE_STRING.test(dateString) || SEC_DATE_STRING.test(dateString)) {
return new Date(parseInt(dateString, 10)).toISOString(); return new Date(parseInt(dateString, 10)).toISOString();
} }
let date = createDate(dateString, timezone); let date = createDate(dateString, timezone, format);
if (!date.isValid()) { if (!date.isValid()) {
dateString = cleanDateString(dateString); dateString = cleanDateString(dateString);
date = createDate(dateString, timezone); date = createDate(dateString, timezone, format);
} }
return date.isValid() ? date.toISOString() : null; return date.isValid() ? date.toISOString() : null;

@ -30,6 +30,14 @@ describe('cleanDatePublished(dateString)', () => {
cleanDatePublished('November 29, 2016: 8:18 AM ET', { timezone: 'America/New_York' }); cleanDatePublished('November 29, 2016: 8:18 AM ET', { timezone: 'America/New_York' });
assert.equal(datePublished, '2016-11-29T13:18:00.000Z'); assert.equal(datePublished, '2016-11-29T13:18:00.000Z');
}); });
it('accepts a custom date format', () => {
// The JS date parser is forgiving, but
// it needs am/pm separated from a time
const datePublished =
cleanDatePublished('Mon Aug 03 12:45:00 EDT 2015', { timezone: 'America/New_York', format: 'ddd MMM DD HH:mm:ss zz YYYY' });
assert.equal(datePublished, '2015-08-03T16:45:00.000Z');
});
}); });
describe('cleanDateString(dateString)', () => { describe('cleanDateString(dateString)', () => {

@ -19,15 +19,8 @@ export const WwwMacrumorsComExtractor = {
'.article .byline', '.article .byline',
], ],
transforms: { // Wednesday January 18, 2017 11:44 am PST
'*': ($node, $) => { format: 'dddd MMMM D, YYYY h:mm A zz',
const $parent = $node.parent();
const text = $parent.text().split(' by')[0];
$parent.empty().text(text);
$('body').after($node);
},
},
timezone: 'America/Los_Angeles', timezone: 'America/Los_Angeles',
}, },

Loading…
Cancel
Save