summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/base.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/handlebars/base.js')
-rw-r--r--lib/handlebars/base.js41
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;
};