node-feedparser icon indicating copy to clipboard operation
node-feedparser copied to clipboard

feedparser and firebase - option to save dates as strings

Open dsl101 opened this issue 7 years ago • 2 comments

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')

dsl101 avatar Oct 06 '16 09:10 dsl101