react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / dist / amd / handlebars / base.js
80713 viewsdefine(1["./utils","./exception","exports"],2function(__dependency1__, __dependency2__, __exports__) {3"use strict";4var Utils = __dependency1__;5var Exception = __dependency2__["default"];67var VERSION = "3.0.0";8__exports__.VERSION = VERSION;var COMPILER_REVISION = 6;9__exports__.COMPILER_REVISION = COMPILER_REVISION;10var REVISION_CHANGES = {111: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it122: '== 1.0.0-rc.3',133: '== 1.0.0-rc.4',144: '== 1.x.x',155: '== 2.0.0-alpha.x',166: '>= 2.0.0-beta.1'17};18__exports__.REVISION_CHANGES = REVISION_CHANGES;19var isArray = Utils.isArray,20isFunction = Utils.isFunction,21toString = Utils.toString,22objectType = '[object Object]';2324function HandlebarsEnvironment(helpers, partials) {25this.helpers = helpers || {};26this.partials = partials || {};2728registerDefaultHelpers(this);29}3031__exports__.HandlebarsEnvironment = HandlebarsEnvironment;HandlebarsEnvironment.prototype = {32constructor: HandlebarsEnvironment,3334logger: logger,35log: log,3637registerHelper: function(name, fn) {38if (toString.call(name) === objectType) {39if (fn) { throw new Exception('Arg not supported with multiple helpers'); }40Utils.extend(this.helpers, name);41} else {42this.helpers[name] = fn;43}44},45unregisterHelper: function(name) {46delete this.helpers[name];47},4849registerPartial: function(name, partial) {50if (toString.call(name) === objectType) {51Utils.extend(this.partials, name);52} else {53if (typeof partial === 'undefined') {54throw new Exception('Attempting to register a partial as undefined');55}56this.partials[name] = partial;57}58},59unregisterPartial: function(name) {60delete this.partials[name];61}62};6364function registerDefaultHelpers(instance) {65instance.registerHelper('helperMissing', function(/* [args, ]options */) {66if(arguments.length === 1) {67// A missing field in a {{foo}} constuct.68return undefined;69} else {70// Someone is actually trying to call something, blow up.71throw new Exception("Missing helper: '" + arguments[arguments.length-1].name + "'");72}73});7475instance.registerHelper('blockHelperMissing', function(context, options) {76var inverse = options.inverse,77fn = options.fn;7879if(context === true) {80return fn(this);81} else if(context === false || context == null) {82return inverse(this);83} else if (isArray(context)) {84if(context.length > 0) {85if (options.ids) {86options.ids = [options.name];87}8889return instance.helpers.each(context, options);90} else {91return inverse(this);92}93} else {94if (options.data && options.ids) {95var data = createFrame(options.data);96data.contextPath = Utils.appendContextPath(options.data.contextPath, options.name);97options = {data: data};98}99100return fn(context, options);101}102});103104instance.registerHelper('each', function(context, options) {105if (!options) {106throw new Exception('Must pass iterator to #each');107}108109var fn = options.fn, inverse = options.inverse;110var i = 0, ret = "", data;111112var contextPath;113if (options.data && options.ids) {114contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';115}116117if (isFunction(context)) { context = context.call(this); }118119if (options.data) {120data = createFrame(options.data);121}122123function execIteration(key, i, last) {124if (data) {125data.key = key;126data.index = i;127data.first = i === 0;128data.last = !!last;129130if (contextPath) {131data.contextPath = contextPath + key;132}133}134135ret = ret + fn(context[key], {136data: data,137blockParams: Utils.blockParams([context[key], key], [contextPath + key, null])138});139}140141if(context && typeof context === 'object') {142if (isArray(context)) {143for(var j = context.length; i<j; i++) {144execIteration(i, i, i === context.length-1);145}146} else {147var priorKey;148149for(var key in context) {150if(context.hasOwnProperty(key)) {151// We're running the iterations one step out of sync so we can detect152// the last iteration without have to scan the object twice and create153// an itermediate keys array.154if (priorKey) {155execIteration(priorKey, i-1);156}157priorKey = key;158i++;159}160}161if (priorKey) {162execIteration(priorKey, i-1, true);163}164}165}166167if(i === 0){168ret = inverse(this);169}170171return ret;172});173174instance.registerHelper('if', function(conditional, options) {175if (isFunction(conditional)) { conditional = conditional.call(this); }176177// Default behavior is to render the positive path if the value is truthy and not empty.178// The `includeZero` option may be set to treat the condtional as purely not empty based on the179// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.180if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {181return options.inverse(this);182} else {183return options.fn(this);184}185});186187instance.registerHelper('unless', function(conditional, options) {188return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});189});190191instance.registerHelper('with', function(context, options) {192if (isFunction(context)) { context = context.call(this); }193194var fn = options.fn;195196if (!Utils.isEmpty(context)) {197if (options.data && options.ids) {198var data = createFrame(options.data);199data.contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]);200options = {data:data};201}202203return fn(context, options);204} else {205return options.inverse(this);206}207});208209instance.registerHelper('log', function(message, options) {210var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;211instance.log(level, message);212});213214instance.registerHelper('lookup', function(obj, field) {215return obj && obj[field];216});217}218219var logger = {220methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },221222// State enum223DEBUG: 0,224INFO: 1,225WARN: 2,226ERROR: 3,227level: 1,228229// Can be overridden in the host environment230log: function(level, message) {231if (typeof console !== 'undefined' && logger.level <= level) {232var method = logger.methodMap[level];233(console[method] || console.log).call(console, message);234}235}236};237__exports__.logger = logger;238var log = logger.log;239__exports__.log = log;240var createFrame = function(object) {241var frame = Utils.extend({}, object);242frame._parent = object;243return frame;244};245__exports__.createFrame = createFrame;246});247248