Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parseDate option #273

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/feedparser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've already parsed the date string into a Date object. With this option enabled, we'll actually be stringifying the date. So, let's rename this option: maybe stringifydate. (If you have a different suggestion, that's ok, but let's also ensure the option names are consistent -- one word, all lowercase.) (I know, I know, there's resume_saxerror which has an underscore -- 🤷‍♂️ )

Also, we need to preserve the current behavior as the default, so if the option is not provided, it's value should be false.

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
Expand Down Expand Up @@ -430,6 +431,7 @@ FeedParser.prototype.handleMeta = function handleMeta (node, type, options) {

var meta = {}
, normalize = !options || (options && options.normalize)
, parseDate = !options || (options && options.parseDate)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseDate = !options || (options && options.parseDate)

This logic will need to be flipped: stringifydate should be true only if options && options.stringifydate is true.

;

if (normalize) {
Expand Down Expand Up @@ -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.toISOString();
if (meta.pubdate === null || name == 'pubdate' || name == 'published')
meta.pubdate = meta.pubDate = date;
if (meta.date === null || name == 'lastbuilddate' || name == 'modified' || name == 'updated')
Expand Down Expand Up @@ -777,6 +780,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){

var item = {}
, normalize = !options || (options && options.normalize)
, parseDate = !options || (options && options.parseDate)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

;

if (normalize) {
Expand Down Expand Up @@ -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 (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')
Expand Down