From f44cee4da00eb1dc91234f47f859b5bc100fcb8f Mon Sep 17 00:00:00 2001 From: dsl101 Date: Tue, 13 Nov 2018 12:50:40 +0000 Subject: [PATCH 1/2] Add option to parse dates as strings Finally, after only two years! I got back to the project I was working on, and needed this functionality, so made a PR against the latest code. Assume nothing else has changed which might affect it, but happy to --- lib/feedparser/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/feedparser/index.js b/lib/feedparser/index.js index 7915725..7088efc 100644 --- a/lib/feedparser/index.js +++ b/lib/feedparser/index.js @@ -71,6 +71,7 @@ function FeedParser (options) { if (!('strict' in this.options)) this.options.strict = false; if (!('normalize' in this.options)) this.options.normalize = true; if (!('addmeta' in this.options)) this.options.addmeta = true; + if (!('parseDate' in this.options)) this.options.parseDate = true; if (!('resume_saxerror' in this.options)) this.options.resume_saxerror = true; if ('MAX_BUFFER_LENGTH' in this.options) { sax.MAX_BUFFER_LENGTH = this.options.MAX_BUFFER_LENGTH; // set to Infinity to have unlimited buffers @@ -430,6 +431,7 @@ FeedParser.prototype.handleMeta = function handleMeta (node, type, options) { var meta = {} , normalize = !options || (options && options.normalize) + , parseDate = !options || (options && options.parseDate) ; if (normalize) { @@ -461,6 +463,7 @@ FeedParser.prototype.handleMeta = function handleMeta (node, type, options) { case('dc:date'): var date = _.get(el) ? new Date(_.get(el)) : null; if (!date) break; + if (parseDate) date = date.toString(); if (meta.pubdate === null || name == 'pubdate' || name == 'published') meta.pubdate = meta.pubDate = date; if (meta.date === null || name == 'lastbuilddate' || name == 'modified' || name == 'updated') @@ -777,6 +780,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){ var item = {} , normalize = !options || (options && options.normalize) + , parseDate = !options || (options && options.parseDate) ; if (normalize) { @@ -815,6 +819,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){ case('dc:date'): var date = _.get(el) ? new Date(_.get(el)) : null; if (!date) break; + if (dateAsString) date = date.toString(); if (item.pubdate === null || name == 'pubdate' || name == 'published' || name == 'issued') item.pubdate = item.pubDate = date; if (item.date === null || name == 'modified' || name == 'updated') From 44ace0be578100fb7022eccf7189103870e78984 Mon Sep 17 00:00:00 2001 From: dsl101 Date: Tue, 13 Nov 2018 14:06:59 +0000 Subject: [PATCH 2/2] Fix typo and switch to ISO format Fixed one left-over option name, and switched to ISO string, so easier to handle internationally. Probably. --- lib/feedparser/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/feedparser/index.js b/lib/feedparser/index.js index 7088efc..2d8fea0 100644 --- a/lib/feedparser/index.js +++ b/lib/feedparser/index.js @@ -463,7 +463,7 @@ FeedParser.prototype.handleMeta = function handleMeta (node, type, options) { case('dc:date'): var date = _.get(el) ? new Date(_.get(el)) : null; if (!date) break; - if (parseDate) date = date.toString(); + if (parseDate) date = date.toISOString(); if (meta.pubdate === null || name == 'pubdate' || name == 'published') meta.pubdate = meta.pubDate = date; if (meta.date === null || name == 'lastbuilddate' || name == 'modified' || name == 'updated') @@ -819,7 +819,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){ case('dc:date'): var date = _.get(el) ? new Date(_.get(el)) : null; if (!date) break; - if (dateAsString) date = date.toString(); + if (parseDate) date = date.toISOString(); if (item.pubdate === null || name == 'pubdate' || name == 'published' || name == 'issued') item.pubdate = item.pubDate = date; if (item.date === null || name == 'modified' || name == 'updated')