node.jsleiningenclojurescriptmomentjscljsbuild

ClojureScript + momentjs (node.js) fails with .format("dddd, MMMM Do YYYY, h:mm:ss a")


I'm trying to show current datetime format to console on node.js + clojure deploying momentjs .

The working node js code:

var moment = require("./lib/moment/moment.js");
console.log(moment().format("dddd, MMMM Do YYYY, h:mm:ss a"));

Console output:

$ node app1              
Friday, July 5th 2013, 9:57:07 am

so, I tried a clojureScript code as below:

(ns rxcljs.core
  (:use [cljs.nodejs :only [require]])
)
(def log #(.log js/console %))
(def moment (require "./lib/moment/moment.js"))

(->> (-> (moment) 
          (.format "dddd, MMMM Do YYYY, h:mm:ss a")
      )
     (log) 
 ) 

The Console output becomes

$ node app            
FridaynullundefinedJulyundefined5thundefined2013nullundefined9null56null31undefinedam

Datetime is partially presented with bunch of null and undefined for some reason.

Compiled js code:

var rxcljs = {core:{}};
rxcljs.core.log = function(a) {
  return console.log(a)
}; 
rxcljs.core.moment = cljs.nodejs.require.call(null, "./lib/moment/moment.js");
rxcljs.core.log.call(null, rxcljs.core.moment.call(null).format("dddd, MMMM Do YYYY, h:mm:ss a"));

The point the problem occurred looks not that problematic:

rxcljs.core.moment.call(null).format("dddd, MMMM Do YYYY, h:mm:ss a")

Any idea? Thanks.


Solution

  • This is due to a known incompatibility of Moment.js 2.0.0 with ClojureScript, fixed in this commit by David Altenburg. Here's the commit message:

    Format function now uses "instanceof" on a var rather than "typeof" on that var's call to determine if the variable is a function.

    This fixes an incompatibility with ClojureScript, which defines String.prototype.call as a function.

    The version currently in the develop branch works fine.