From 62efd014fcfa0aa11c2a70b2dce8d6d48378d074 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Thu, 18 Feb 2010 22:08:16 -0800 Subject: [PATCH] more robust type detection, thanks again Doug --- CHANGES.md | 3 +++ THANKS.md | 1 + mustache.js | 30 +++++++++++++++++++++--------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 543cedb..3c475eb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ # mustache.js Changes +## 0.2.3 (??-??-????) +* Added more robust type detection. + ## 0.2.2 (11-02-2010) * ctemplate compat: Partials are indicated by >, not <. diff --git a/THANKS.md b/THANKS.md index e6a7804..62ba334 100644 --- a/THANKS.md +++ b/THANKS.md @@ -8,3 +8,4 @@ Mustache.js wouldn't kick ass if it weren't for these fine souls * J Chris Anderson / jchris * Tom Robinson / tlrobinson * Aaron Quint / quirkey + * Douglas Crockford \ No newline at end of file diff --git a/mustache.js b/mustache.js index 122354b..e23103b 100644 --- a/mustache.js +++ b/mustache.js @@ -238,18 +238,30 @@ var Mustache = function() { }, is_object: function(a) { - return a && typeof a == "object" + return this.typeOf(a) === "object"; + }, + + is_array: function(a) { + return this.typeOf(a) === "array"; }, /* - Thanks Doug Crockford - JavaScript — The Good Parts lists an alternative that works better with - frames. Frames can suck it, we use the simple version. + Thanks Doug Crockford http://javascript.crockford.com/remedial.html */ - is_array: function(a) { - return (a && - typeof a === "object" && - a.constructor === Array); + typeOf: function(value) { + var s = typeof value; + if(s === "object") { + if(value) { + if(typeof value.length === "number" && + !(value.propertyIsEnumerable("length")) && + typeof value.splice === "function") { + s = "array"; + } + } else { + s = "null"; + } + } + return s; }, /* @@ -278,7 +290,7 @@ var Mustache = function() { return({ name: "mustache.js", - version: "0.2.2", + version: "0.2.3-dev", /* Turns a template and view into HTML