react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / dist / cjs / handlebars / base.js
80713 views"use strict";1var Utils = require("./utils");2var Exception = require("./exception")["default"];34var VERSION = "3.0.0";5exports.VERSION = VERSION;var COMPILER_REVISION = 6;6exports.COMPILER_REVISION = COMPILER_REVISION;7var REVISION_CHANGES = {81: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it92: '== 1.0.0-rc.3',103: '== 1.0.0-rc.4',114: '== 1.x.x',125: '== 2.0.0-alpha.x',136: '>= 2.0.0-beta.1'14};15exports.REVISION_CHANGES = REVISION_CHANGES;16var isArray = Utils.isArray,17isFunction = Utils.isFunction,18toString = Utils.toString,19objectType = '[object Object]';2021function HandlebarsEnvironment(helpers, partials) {22this.helpers = helpers || {};23this.partials = partials || {};2425registerDefaultHelpers(this);26}2728exports.HandlebarsEnvironment = HandlebarsEnvironment;HandlebarsEnvironment.prototype = {29constructor: HandlebarsEnvironment,3031logger: logger,32log: log,3334registerHelper: function(name, fn) {35if (toString.call(name) === objectType) {36if (fn) { throw new Exception('Arg not supported with multiple helpers'); }37Utils.extend(this.helpers, name);38} else {39this.helpers[name] = fn;40}41},42unregisterHelper: function(name) {43delete this.helpers[name];44},4546registerPartial: function(name, partial) {47if (toString.call(name) === objectType) {48Utils.extend(this.partials, name);49} else {50if (typeof partial === 'undefined') {51throw new Exception('Attempting to register a partial as undefined');52}53this.partials[name] = partial;54}55},56unregisterPartial: function(name) {57delete this.partials[name];58}59};6061function registerDefaultHelpers(instance) {62instance.registerHelper('helperMissing', function(/* [args, ]options */) {63if(arguments.length === 1) {64// A missing field in a {{foo}} constuct.65return undefined;66} else {67// Someone is actually trying to call something, blow up.68throw new Exception("Missing helper: '" + arguments[arguments.length-1].name + "'");69}70});7172instance.registerHelper('blockHelperMissing', function(context, options) {73var inverse = options.inverse,74fn = options.fn;7576if(context === true) {77return fn(this);78} else if(context === false || context == null) {79return inverse(this);80} else if (isArray(context)) {81if(context.length > 0) {82if (options.ids) {83options.ids = [options.name];84}8586return instance.helpers.each(context, options);87} else {88return inverse(this);89}90} else {91if (options.data && options.ids) {92var data = createFrame(options.data);93data.contextPath = Utils.appendContextPath(options.data.contextPath, options.name);94options = {data: data};95}9697return fn(context, options);98}99});100101instance.registerHelper('each', function(context, options) {102if (!options) {103throw new Exception('Must pass iterator to #each');104}105106var fn = options.fn, inverse = options.inverse;107var i = 0, ret = "", data;108109var contextPath;110if (options.data && options.ids) {111contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';112}113114if (isFunction(context)) { context = context.call(this); }115116if (options.data) {117data = createFrame(options.data);118}119120function execIteration(key, i, last) {121if (data) {122data.key = key;123data.index = i;124data.first = i === 0;125data.last = !!last;126127if (contextPath) {128data.contextPath = contextPath + key;129}130}131132ret = ret + fn(context[key], {133data: data,134blockParams: Utils.blockParams([context[key], key], [contextPath + key, null])135});136}137138if(context && typeof context === 'object') {139if (isArray(context)) {140for(var j = context.length; i<j; i++) {141execIteration(i, i, i === context.length-1);142}143} else {144var priorKey;145146for(var key in context) {147if(context.hasOwnProperty(key)) {148// We're running the iterations one step out of sync so we can detect149// the last iteration without have to scan the object twice and create150// an itermediate keys array.151if (priorKey) {152execIteration(priorKey, i-1);153}154priorKey = key;155i++;156}157}158if (priorKey) {159execIteration(priorKey, i-1, true);160}161}162}163164if(i === 0){165ret = inverse(this);166}167168return ret;169});170171instance.registerHelper('if', function(conditional, options) {172if (isFunction(conditional)) { conditional = conditional.call(this); }173174// Default behavior is to render the positive path if the value is truthy and not empty.175// The `includeZero` option may be set to treat the condtional as purely not empty based on the176// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.177if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {178return options.inverse(this);179} else {180return options.fn(this);181}182});183184instance.registerHelper('unless', function(conditional, options) {185return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});186});187188instance.registerHelper('with', function(context, options) {189if (isFunction(context)) { context = context.call(this); }190191var fn = options.fn;192193if (!Utils.isEmpty(context)) {194if (options.data && options.ids) {195var data = createFrame(options.data);196data.contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]);197options = {data:data};198}199200return fn(context, options);201} else {202return options.inverse(this);203}204});205206instance.registerHelper('log', function(message, options) {207var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;208instance.log(level, message);209});210211instance.registerHelper('lookup', function(obj, field) {212return obj && obj[field];213});214}215216var logger = {217methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },218219// State enum220DEBUG: 0,221INFO: 1,222WARN: 2,223ERROR: 3,224level: 1,225226// Can be overridden in the host environment227log: function(level, message) {228if (typeof console !== 'undefined' && logger.level <= level) {229var method = logger.methodMap[level];230(console[method] || console.log).call(console, message);231}232}233};234exports.logger = logger;235var log = logger.log;236exports.log = log;237var createFrame = function(object) {238var frame = Utils.extend({}, object);239frame._parent = object;240return frame;241};242exports.createFrame = createFrame;243244