diff options
author | kpdecker <kpdecker@gmail.com> | 2013-09-02 18:19:18 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2013-09-02 18:19:18 -0500 |
commit | cb0c45b29fa9df0b912d843e3a695293f1d10cad (patch) | |
tree | cf50b624e59f3beee7d9e7c89be67d3dae7c6b8b /lib/handlebars/base.js | |
parent | 192887cedce6e6155bb1a079ab2802ff28fbd2bf (diff) | |
parent | 0fe78f379ab85e586381e167aecd7d5527984697 (diff) | |
download | handlebars.js-cb0c45b29fa9df0b912d843e3a695293f1d10cad.zip handlebars.js-cb0c45b29fa9df0b912d843e3a695293f1d10cad.tar.gz handlebars.js-cb0c45b29fa9df0b912d843e3a695293f1d10cad.tar.bz2 |
Merge branch 'master' into es6-modules
Conflicts:
Gruntfile.js
Rakefile
dist/handlebars.js
dist/handlebars.runtime.js
lib/handlebars.js
lib/handlebars/base.js
lib/handlebars/runtime.js
lib/handlebars/utils.js
package.json
Diffstat (limited to 'lib/handlebars/base.js')
-rw-r--r-- | lib/handlebars/base.js | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index a6cd7ff..b375554 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -15,9 +15,24 @@ export var REVISION_CHANGES = { }; 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; +}; + export function HandlebarsEnvironment(helpers, partials) { this.helpers = helpers || {}; this.partials = partials || {}; @@ -59,15 +74,13 @@ function registerDefaultHelpers(instance) { instance.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 instance.helpers.each(context, options); } else { @@ -82,15 +95,14 @@ function registerDefaultHelpers(instance) { 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 = 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 }); @@ -114,8 +126,7 @@ function registerDefaultHelpers(instance) { }); instance.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 (isEmpty(conditional)) { return options.inverse(this); @@ -129,8 +140,7 @@ function registerDefaultHelpers(instance) { }); instance.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 (!isEmpty(context)) return options.fn(context); }); @@ -161,9 +171,8 @@ export var logger = { export function log(level, obj) { logger.log(level, obj); }; -export var createFrame = Object.create || function(object) { - K.prototype = object; - var obj = new K(); - K.prototype = null; +export var createFrame = function(object) { + var obj = {}; + Handlebars.Utils.extend(obj, object); return obj; }; |