diff options
author | Kevin Decker <kpdecker@gmail.com> | 2013-10-12 14:22:10 -0700 |
---|---|---|
committer | Kevin Decker <kpdecker@gmail.com> | 2013-10-12 14:22:10 -0700 |
commit | 583141de7cb61eb70eaa6b33c25f475f3048071b (patch) | |
tree | 47c419f82f2941fbde5ff5aa33a85b79d6772b4c /lib/handlebars/base.js | |
parent | 782aae95d0b430058e2f65b8eba1621453f9055e (diff) | |
parent | 3f96319f103d1e9dc4a6de220d2a9934e00df0b6 (diff) | |
download | handlebars.js-583141de7cb61eb70eaa6b33c25f475f3048071b.zip handlebars.js-583141de7cb61eb70eaa6b33c25f475f3048071b.tar.gz handlebars.js-583141de7cb61eb70eaa6b33c25f475f3048071b.tar.bz2 |
Merge pull request #628 from wycats/es6-modules
Convert code to ES6 modules
Diffstat (limited to 'lib/handlebars/base.js')
-rw-r--r-- | lib/handlebars/base.js | 240 |
1 files changed, 125 insertions, 115 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 1decfd2..29c8de8 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -1,32 +1,26 @@ /*jshint eqnull: true */ -module.exports.create = function() { +import { extend, isEmpty } from "./utils"; +import Exception from "./exception"; -var Handlebars = {}; +export var VERSION = "1.0.0"; +export var COMPILER_REVISION = 4; -// BEGIN(BROWSER) - -Handlebars.VERSION = "1.0.0"; -Handlebars.COMPILER_REVISION = 4; - -Handlebars.REVISION_CHANGES = { +export var REVISION_CHANGES = { 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it 2: '== 1.0.0-rc.3', 3: '== 1.0.0-rc.4', 4: '>= 1.0.0' }; -Handlebars.helpers = {}; -Handlebars.partials = {}; - var toString = Object.prototype.toString, objectType = '[object Object]'; // Sourced from lodash // https://github.com/bestiejs/lodash/blob/master/LICENSE.txt -function isFunction(value) { +var isFunction = function(value) { return typeof value === 'function'; -} +}; // fallback for older versions of Chrome and Safari if (isFunction(/x/)) { isFunction = function(value) { @@ -36,138 +30,154 @@ if (isFunction(/x/)) { 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'); } - Handlebars.Utils.extend(this.helpers, name); - } else { - if (inverse) { fn.not = inverse; } - this.helpers[name] = fn; - } -}; +export function HandlebarsEnvironment(helpers, partials) { + this.helpers = helpers || {}; + this.partials = partials || {}; -Handlebars.registerPartial = function(name, str) { - if (toString.call(name) === objectType) { - Handlebars.Utils.extend(this.partials, name); - } else { - this.partials[name] = str; - } -}; + registerDefaultHelpers(this); +} -Handlebars.registerHelper('helperMissing', function(arg) { - if(arguments.length === 2) { - return undefined; - } else { - throw new Error("Missing helper: '" + arg + "'"); - } -}); +HandlebarsEnvironment.prototype = { + constructor: HandlebarsEnvironment, -Handlebars.registerHelper('blockHelperMissing', function(context, options) { - var inverse = options.inverse || function() {}, fn = options.fn; + logger: logger, + log: log, - if (isFunction(context)) { context = context.call(this); } + registerHelper: function(name, fn, inverse) { + if (toString.call(name) === objectType) { + if (inverse || fn) { throw new Exception('Arg not supported with multiple helpers'); } + extend(this.helpers, name); + } else { + if (inverse) { fn.not = inverse; } + this.helpers[name] = fn; + } + }, - if(context === true) { - return fn(this); - } else if(context === false || context == null) { - return inverse(this); - } else if (isArray(context)) { - if(context.length > 0) { - return Handlebars.helpers.each(context, options); + registerPartial: function(name, str) { + if (toString.call(name) === objectType) { + extend(this.partials, name); } else { - return inverse(this); + this.partials[name] = str; } - } else { - return fn(context); } -}); - -Handlebars.createFrame = function(object) { - var obj = {}; - Handlebars.Utils.extend(obj, object); - return obj; }; -Handlebars.logger = { - DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3, +function registerDefaultHelpers(instance) { + instance.registerHelper('helperMissing', function(arg) { + if(arguments.length === 2) { + return undefined; + } else { + throw new Error("Missing helper: '" + arg + "'"); + } + }); + + instance.registerHelper('blockHelperMissing', function(context, options) { + var inverse = options.inverse || function() {}, fn = options.fn; - methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'}, + if (isFunction(context)) { context = context.call(this); } - // can be overridden in the host environment - log: function(level, obj) { - if (Handlebars.logger.level <= level) { - var method = Handlebars.logger.methodMap[level]; - if (typeof console !== 'undefined' && console[method]) { - console[method].call(console, obj); + if(context === true) { + return fn(this); + } else if(context === false || context == null) { + return inverse(this); + } else if (isArray(context)) { + if(context.length > 0) { + return instance.helpers.each(context, options); + } else { + return inverse(this); } + } else { + return fn(context); } - } -}; + }); -Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); }; + instance.registerHelper('each', function(context, options) { + var fn = options.fn, inverse = options.inverse; + var i = 0, ret = "", data; -Handlebars.registerHelper('each', function(context, options) { - var fn = options.fn, inverse = options.inverse; - var i = 0, ret = "", data; + if (isFunction(context)) { context = context.call(this); } - if (isFunction(context)) { context = context.call(this); } - - if (options.data) { - data = Handlebars.createFrame(options.data); - } + if (options.data) { + data = createFrame(options.data); + } - if(context && typeof context === 'object') { - if (isArray(context)) { - for(var j = context.length; i<j; i++) { - if (data) { data.index = i; } - ret = ret + fn(context[i], { data: data }); - } - } else { - for(var key in context) { - if(context.hasOwnProperty(key)) { - if(data) { data.key = key; } - ret = ret + fn(context[key], {data: data}); - i++; + if(context && typeof context === 'object') { + if (isArray(context)) { + for(var j = context.length; i<j; i++) { + if (data) { data.index = i; } + ret = ret + fn(context[i], { data: data }); + } + } else { + for(var key in context) { + if(context.hasOwnProperty(key)) { + if(data) { data.key = key; } + ret = ret + fn(context[key], {data: data}); + i++; + } } } } - } - if(i === 0){ - ret = inverse(this); - } + if(i === 0){ + ret = inverse(this); + } - return ret; -}); + return ret; + }); -Handlebars.registerHelper('if', function(conditional, options) { - if (isFunction(conditional)) { conditional = conditional.call(this); } + instance.registerHelper('if', function(conditional, options) { + if (isFunction(conditional)) { conditional = conditional.call(this); } - if(Handlebars.Utils.isEmpty(conditional)) { - return options.inverse(this); - } else { - return options.fn(this); - } -}); + if (isEmpty(conditional)) { + return options.inverse(this); + } else { + return options.fn(this); + } + }); -Handlebars.registerHelper('unless', function(conditional, options) { - return Handlebars.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn}); -}); + instance.registerHelper('unless', function(conditional, options) { + return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn}); + }); -Handlebars.registerHelper('with', function(context, options) { - if (isFunction(context)) { context = context.call(this); } + instance.registerHelper('with', function(context, options) { + if (isFunction(context)) { context = context.call(this); } - if (!Handlebars.Utils.isEmpty(context)) return options.fn(context); -}); + if (!isEmpty(context)) return options.fn(context); + }); -Handlebars.registerHelper('log', function(context, options) { - var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; - Handlebars.log(level, context); -}); + instance.registerHelper('log', function(context, options) { + var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; + instance.log(level, context); + }); +} + +export var logger = { + methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' }, + + // State enum + DEBUG: 0, + INFO: 1, + WARN: 2, + ERROR: 3, + level: 3, + + // can be overridden in the host environment + log: function(level, obj) { + if (logger.level <= level) { + var method = logger.methodMap[level]; + if (typeof console !== 'undefined' && console[method]) { + console[method].call(console, obj); + } + } + } +}; -// END(BROWSER) +export function log(level, obj) { logger.log(level, obj); } -return Handlebars; +export var createFrame = function(object) { + var obj = {}; + extend(obj, object); + return obj; }; |