From c5f8fbc755ef035fa90e6f017ab3de6b95a41a8e Mon Sep 17 00:00:00 2001 From: Nathan Vander Wilt Date: Sat, 4 Dec 2010 18:08:30 -0800 Subject: [PATCH] fixed reliance on ES5 Date implementation for compatibility with CouchDBX 1.0.1.1 --- lib/date.js | 11 +++++++++++ lists/comments.js | 5 +++-- lists/index.js | 7 ++++--- views/comments/map.js | 2 +- views/recent-posts/map.js | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 lib/date.js diff --git a/lib/date.js b/lib/date.js new file mode 100644 index 0000000..31e3108 --- /dev/null +++ b/lib/date.js @@ -0,0 +1,11 @@ +// Simple workaround for older JavaScript engines that +// do not understand the One True Date Format. +// This doesn't totally mimic new Date(), just string parsing. +exports.newDate = function (rfc3399) { + var temp = Date.parse(rfc3399); + if (isNaN(temp)) { + // this technique is borrowed from jquery.couch.app.util's $.prettyDate + temp = rfc3399.replace(/-/g,"/").replace("T", " ").replace("Z", " +0000").replace(/(\d*\:\d*:\d*)\.\d*/g,"$1"); + } + return new Date(temp); +} diff --git a/lists/comments.js b/lists/comments.js index 770a3e3..c1d4f8e 100644 --- a/lists/comments.js +++ b/lists/comments.js @@ -4,6 +4,7 @@ function(head, req) { var List = require("vendor/couchapp/lib/list"); var path = require("vendor/couchapp/lib/path").init(req); var Atom = require("vendor/couchapp/lib/atom"); + var newDate = require("lib/date").newDate; var indexPath = path.list('index','recent-posts',{descending:true, limit:10}); var feedPath = path.list('index','recent-posts',{descending:true, limit:10, format:"atom"}); @@ -21,7 +22,7 @@ function(head, req) { // generate the feed header var feedHeader = Atom.header({ - updated : (row ? new Date(row.value.created_at) : new Date()), + updated : (row ? newDate(row.value.created_at) : new Date()), title : ddoc.blog.title + " comments", feed_id : path.absolute(indexPath), feed_link : path.absolute(commentsFeed) @@ -40,7 +41,7 @@ function(head, req) { entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)), title : "comment on "+v.post_id, content : markdown.encode(Mustache.escape(v.comment)), - updated : new Date(v.created_at), + updated : newDate(v.created_at), author : v.commenter.nickname || v.commenter.name, alternate : path.absolute(path.list('post','post-page', {startkey:[v.post_id]})) }); diff --git a/lists/index.js b/lists/index.js index 531300b..c7f3d58 100644 --- a/lists/index.js +++ b/lists/index.js @@ -4,7 +4,8 @@ function(head, req) { var List = require("vendor/couchapp/lib/list"); var path = require("vendor/couchapp/lib/path").init(req); var Atom = require("vendor/couchapp/lib/atom"); - + var newDate = require("lib/date").newDate; + var indexPath = path.list('index','recent-posts',{descending:true, limit:10}); var feedPath = path.list('index','recent-posts',{descending:true, limit:10, format:"atom"}); var commentsFeed = path.list('comments','comments',{descending:true, limit:10, format:"atom"}); @@ -74,7 +75,7 @@ function(head, req) { // generate the feed header var feedHeader = Atom.header({ - updated : (row ? new Date(row.value.created_at) : new Date()), + updated : (row ? newDate(row.value.created_at) : new Date()), title : ddoc.blog.title, feed_id : path.absolute(indexPath), feed_link : path.absolute(feedPath), @@ -98,7 +99,7 @@ function(head, req) { entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)), title : row.value.title, content : html, - updated : new Date(row.value.created_at), + updated : newDate(row.value.created_at), author : row.value.author, alternate : path.absolute(path.show('post', row.id)) }); diff --git a/views/comments/map.js b/views/comments/map.js index 60380b0..7875bb7 100644 --- a/views/comments/map.js +++ b/views/comments/map.js @@ -5,6 +5,6 @@ function(doc) { // todo normalize this schema-ness doc.commenter.gravatar = hex_md5(doc.commenter.email); } - emit(new Date(doc.created_at), doc); + emit(doc.created_at, doc); } }; \ No newline at end of file diff --git a/views/recent-posts/map.js b/views/recent-posts/map.js index 8e193ed..7ea4541 100644 --- a/views/recent-posts/map.js +++ b/views/recent-posts/map.js @@ -1,5 +1,5 @@ function(doc) { if (doc.type == "post") { - emit(new Date(doc.created_at), doc); + emit(doc.created_at, doc); } };