danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 192 forks source link

feedparser and firebase - option to save dates as strings #180

Open dsl101 opened 8 years ago

dsl101 commented 8 years ago

I'm using firebase as a backend for storage, and it has the interesting feature that it can't store javascript Date objects. Whilst I'm in the process of discussing this with firebase (I think it probably should, but who knows what they'll decide), I made the following local changes to feedparser to provide an option to return any dates in the item and metadata as strings. Sorry I didn't make a branch, so can't do this as a pull request, but in case it helps anyone else.

New option:

dateAsString: Defaults to false. Returns dates as strings rather than Date() objects

main.js Diff:

@@ -107,6 +107,7 @@
   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 (!('dateAsString' in this.options)) this.options.dateAsString = 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
@@ -403,6 +404,7 @@

   var meta = {}
     , normalize = !options || (options && options.normalize)
+    , dateAsString = !options || (options && options.dateAsString)
     ;

   if (normalize) {
@@ -434,6 +436,7 @@
       case('dc:date'):
         var date = utils.get(el) ? new Date(utils.get(el)) : null;
         if (!date) break;
+        if (dateAsString) 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')
@@ -738,6 +741,7 @@

   var item = {}
     , normalize = !options || (options && options.normalize)
+    , dateAsString = !options || (options && options.dateAsString)
     ;

   if (normalize) {
@@ -776,6 +780,7 @@
       case('dc:date'):
         var date = utils.get(el) ? new Date(utils.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')
danmactough commented 6 years ago

I think an option to parse date fields as strings instead of dates is a useful enhancement. PR welcome!

I would prefer the name parseDate for the option (and it should default to true).

danmactough commented 6 years ago

I would prefer the name parseDate for the option (and it should default to true).

Both of these opinions of mine are terrible. Sorry, @dsl101.