diff options
author | Yehuda Katz <wycats@gmail.com> | 2013-07-01 13:59:58 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2013-07-01 13:59:58 -0700 |
commit | 88ee4757e77f97afb206132eddb64e688700eb37 (patch) | |
tree | 08f7db62a3151dfbd44bfd6f1ec4ec6737a2bdb6 /lib/handlebars/base.js | |
parent | 8e2416dabb2056c07357a55b9259322f0d794ada (diff) | |
download | handlebars.js-88ee4757e77f97afb206132eddb64e688700eb37.zip handlebars.js-88ee4757e77f97afb206132eddb64e688700eb37.tar.gz handlebars.js-88ee4757e77f97afb206132eddb64e688700eb37.tar.bz2 |
Initial work on ES6 modules
Diffstat (limited to 'lib/handlebars/base.js')
-rw-r--r-- | lib/handlebars/base.js | 246 |
1 files changed, 124 insertions, 122 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 44a369c..9849678 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -1,166 +1,168 @@ /*jshint eqnull: true */ -module.exports.create = function() { +import { Exception, extend } from "handlebars/utils"; -var Handlebars = {}; +var K = function() { return this; }; -// BEGIN(BROWSER) +export VERSION = "1.0.0"; +export COMPILER_REVISION = 4; -Handlebars.VERSION = "1.0.0"; -Handlebars.COMPILER_REVISION = 4; - -Handlebars.REVISION_CHANGES = { +export 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 = {}; +// TODO: Make this a class +export default function(helpers, partials) { -var toString = Object.prototype.toString, - functionType = '[object Function]', - objectType = '[object Object]'; + var exports = {}; -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; - } -}; -Handlebars.registerPartial = function(name, str) { - if (toString.call(name) === objectType) { - Handlebars.Utils.extend(this.partials, name); - } else { - this.partials[name] = str; - } -}; - -Handlebars.registerHelper('helperMissing', function(arg) { - if(arguments.length === 2) { - return undefined; - } else { - throw new Error("Missing helper: '" + arg + "'"); - } -}); + helpers = helpers || {}; + partials = partials || {}; -Handlebars.registerHelper('blockHelperMissing', function(context, options) { - var inverse = options.inverse || function() {}, fn = options.fn; + var toString = Object.prototype.toString, + functionType = '[object Function]', + objectType = '[object Object]'; - var type = toString.call(context); + exports.registerHelper(name, fn, inverse) { + if (toString.call(name) === objectType) { + if (inverse || fn) { throw new Exception('Arg not supported with multiple helpers'); } + extend(helpers, name); + } else { + if (inverse) { fn.not = inverse; } + helpers[name] = fn; + } + }; - if(type === functionType) { context = context.call(this); } + exports.registerPartial(name, str) { + if (toString.call(name) === objectType) { + extend(partials, name); + } else { + partials[name] = str; + } + }; - if(context === true) { - return fn(this); - } else if(context === false || context == null) { - return inverse(this); - } else if(type === "[object Array]") { - if(context.length > 0) { - return Handlebars.helpers.each(context, options); + exports.registerHelper('helperMissing', function(arg) { + if(arguments.length === 2) { + return undefined; } else { - return inverse(this); + throw new Error("Missing helper: '" + arg + "'"); } - } else { - return fn(context); - } -}); + }); -Handlebars.K = function() {}; + exports.registerHelper('blockHelperMissing', function(context, options) { + var inverse = options.inverse || function() {}, fn = options.fn; -Handlebars.createFrame = Object.create || function(object) { - Handlebars.K.prototype = object; - var obj = new Handlebars.K(); - Handlebars.K.prototype = null; - return obj; -}; - -Handlebars.logger = { - DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3, + var type = toString.call(context); - methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'}, + if(type === functionType) { 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(type === "[object Array]") { + if(context.length > 0) { + return Handlebars.helpers.each(context, options); + } else { + return inverse(this); } + } else { + return fn(context); } - } -}; - -Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); }; + }); -Handlebars.registerHelper('each', function(context, options) { - var fn = options.fn, inverse = options.inverse; - var i = 0, ret = "", data; + exports.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); } + var type = toString.call(context); + if(type === functionType) { context = context.call(this); } - if (options.data) { - data = Handlebars.createFrame(options.data); - } + if (options.data) { + data = Handlebars.createFrame(options.data); + } - if(context && typeof context === 'object') { - if(context instanceof Array){ - 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(context instanceof Array){ + 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) { - var type = toString.call(conditional); - if(type === functionType) { conditional = conditional.call(this); } + exports.registerHelper('if', function(conditional, options) { + var type = toString.call(conditional); + if(type === functionType) { conditional = conditional.call(this); } - if(!conditional || Handlebars.Utils.isEmpty(conditional)) { - return options.inverse(this); - } else { - return options.fn(this); - } -}); + if(!conditional || Handlebars.Utils.isEmpty(conditional)) { + return options.inverse(this); + } else { + return options.fn(this); + } + }); + + exports.registerHelper('unless', function(conditional, options) { + return Handlebars.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn}); + }); + + exports.registerHelper('with', function(context, options) { + var type = toString.call(context); + if(type === functionType) { context = context.call(this); } -Handlebars.registerHelper('unless', function(conditional, options) { - return Handlebars.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn}); -}); + if (!Handlebars.Utils.isEmpty(context)) return options.fn(context); + }); -Handlebars.registerHelper('with', function(context, options) { - var type = toString.call(context); - if(type === functionType) { context = context.call(this); } + exports.registerHelper('log', function(context, options) { + var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; + Handlebars.log(level, context); + }); - if (!Handlebars.Utils.isEmpty(context)) return options.fn(context); -}); + return Handlebars; +} -Handlebars.registerHelper('log', function(context, options) { - var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; - Handlebars.log(level, context); -}); +var levels = { + DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3 +} -// END(BROWSER) +var methodMap = { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' }; -return Handlebars; +export logger = { + // 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); + } + } + } +}; + +export function log(level, obj) { logger.log(level, obj); }; + +export createFrame = Object.create || function(object) { + K.prototype = object; + var obj = new K(); + K.prototype = null; + return obj; }; |