diff options
author | kpdecker <kpdecker@gmail.com> | 2013-08-24 22:13:37 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2013-08-24 22:20:12 -0500 |
commit | 6e6acaac0d676581ea45a1a4810907abee65b852 (patch) | |
tree | f49206878a1dccf4c938f398ba36fcd5659d1cb6 /lib/handlebars/base.js | |
parent | 4c02d3027c3b610f3f2a27b7ddbff091f8e53188 (diff) | |
download | handlebars.js-6e6acaac0d676581ea45a1a4810907abee65b852.zip handlebars.js-6e6acaac0d676581ea45a1a4810907abee65b852.tar.gz handlebars.js-6e6acaac0d676581ea45a1a4810907abee65b852.tar.bz2 |
Unify isFunction/isArray handling
Diffstat (limited to 'lib/handlebars/base.js')
-rw-r--r-- | lib/handlebars/base.js | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 4cf4543..1decfd2 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -20,9 +20,24 @@ Handlebars.helpers = {}; Handlebars.partials = {}; var toString = Object.prototype.toString, - functionType = '[object Function]', objectType = '[object Object]'; +// Sourced from lodash +// https://github.com/bestiejs/lodash/blob/master/LICENSE.txt +function isFunction(value) { + return typeof value === 'function'; +} +// fallback for older versions of Chrome and Safari +if (isFunction(/x/)) { + isFunction = function(value) { + return typeof value === 'function' && toString.call(value) === '[object Function]'; + }; +} + +function isArray(value) { + return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false; +}; + Handlebars.registerHelper = function(name, fn, inverse) { if (toString.call(name) === objectType) { if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); } @@ -52,15 +67,13 @@ Handlebars.registerHelper('helperMissing', function(arg) { Handlebars.registerHelper('blockHelperMissing', function(context, options) { var inverse = options.inverse || function() {}, fn = options.fn; - var type = toString.call(context); - - if(type === functionType) { context = context.call(this); } + if (isFunction(context)) { context = context.call(this); } if(context === true) { return fn(this); } else if(context === false || context == null) { return inverse(this); - } else if(type === "[object Array]") { + } else if (isArray(context)) { if(context.length > 0) { return Handlebars.helpers.each(context, options); } else { @@ -71,8 +84,6 @@ Handlebars.registerHelper('blockHelperMissing', function(context, options) { } }); -Handlebars.K = function() {}; - Handlebars.createFrame = function(object) { var obj = {}; Handlebars.Utils.extend(obj, object); @@ -101,15 +112,14 @@ Handlebars.registerHelper('each', function(context, options) { var fn = options.fn, inverse = options.inverse; var i = 0, ret = "", data; - var type = toString.call(context); - if(type === functionType) { context = context.call(this); } + if (isFunction(context)) { context = context.call(this); } if (options.data) { data = Handlebars.createFrame(options.data); } if(context && typeof context === 'object') { - if(context instanceof Array){ + if (isArray(context)) { for(var j = context.length; i<j; i++) { if (data) { data.index = i; } ret = ret + fn(context[i], { data: data }); @@ -133,8 +143,7 @@ Handlebars.registerHelper('each', function(context, options) { }); Handlebars.registerHelper('if', function(conditional, options) { - var type = toString.call(conditional); - if(type === functionType) { conditional = conditional.call(this); } + if (isFunction(conditional)) { conditional = conditional.call(this); } if(Handlebars.Utils.isEmpty(conditional)) { return options.inverse(this); @@ -148,8 +157,7 @@ Handlebars.registerHelper('unless', function(conditional, options) { }); Handlebars.registerHelper('with', function(context, options) { - var type = toString.call(context); - if(type === functionType) { context = context.call(this); } + if (isFunction(context)) { context = context.call(this); } if (!Handlebars.Utils.isEmpty(context)) return options.fn(context); }); |