react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / lib / handlebars / base.js
80698 viewsmodule Utils from "./utils";1import Exception from "./exception";23export var VERSION = "3.0.0";4export var COMPILER_REVISION = 6;56export var REVISION_CHANGES = {71: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it82: '== 1.0.0-rc.3',93: '== 1.0.0-rc.4',104: '== 1.x.x',115: '== 2.0.0-alpha.x',126: '>= 2.0.0-beta.1'13};1415var isArray = Utils.isArray,16isFunction = Utils.isFunction,17toString = Utils.toString,18objectType = '[object Object]';1920export function HandlebarsEnvironment(helpers, partials) {21this.helpers = helpers || {};22this.partials = partials || {};2324registerDefaultHelpers(this);25}2627HandlebarsEnvironment.prototype = {28constructor: HandlebarsEnvironment,2930logger: logger,31log: log,3233registerHelper: function(name, fn) {34if (toString.call(name) === objectType) {35if (fn) { throw new Exception('Arg not supported with multiple helpers'); }36Utils.extend(this.helpers, name);37} else {38this.helpers[name] = fn;39}40},41unregisterHelper: function(name) {42delete this.helpers[name];43},4445registerPartial: function(name, partial) {46if (toString.call(name) === objectType) {47Utils.extend(this.partials, name);48} else {49if (typeof partial === 'undefined') {50throw new Exception('Attempting to register a partial as undefined');51}52this.partials[name] = partial;53}54},55unregisterPartial: function(name) {56delete this.partials[name];57}58};5960function registerDefaultHelpers(instance) {61instance.registerHelper('helperMissing', function(/* [args, ]options */) {62if(arguments.length === 1) {63// A missing field in a {{foo}} constuct.64return undefined;65} else {66// Someone is actually trying to call something, blow up.67throw new Exception("Missing helper: '" + arguments[arguments.length-1].name + "'");68}69});7071instance.registerHelper('blockHelperMissing', function(context, options) {72var inverse = options.inverse,73fn = options.fn;7475if(context === true) {76return fn(this);77} else if(context === false || context == null) {78return inverse(this);79} else if (isArray(context)) {80if(context.length > 0) {81if (options.ids) {82options.ids = [options.name];83}8485return instance.helpers.each(context, options);86} else {87return inverse(this);88}89} else {90if (options.data && options.ids) {91var data = createFrame(options.data);92data.contextPath = Utils.appendContextPath(options.data.contextPath, options.name);93options = {data: data};94}9596return fn(context, options);97}98});99100instance.registerHelper('each', function(context, options) {101if (!options) {102throw new Exception('Must pass iterator to #each');103}104105var fn = options.fn, inverse = options.inverse;106var i = 0, ret = "", data;107108var contextPath;109if (options.data && options.ids) {110contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';111}112113if (isFunction(context)) { context = context.call(this); }114115if (options.data) {116data = createFrame(options.data);117}118119function execIteration(key, i, last) {120if (data) {121data.key = key;122data.index = i;123data.first = i === 0;124data.last = !!last;125126if (contextPath) {127data.contextPath = contextPath + key;128}129}130131ret = ret + fn(context[key], {132data: data,133blockParams: Utils.blockParams([context[key], key], [contextPath + key, null])134});135}136137if(context && typeof context === 'object') {138if (isArray(context)) {139for(var j = context.length; i<j; i++) {140execIteration(i, i, i === context.length-1);141}142} else {143var priorKey;144145for(var key in context) {146if(context.hasOwnProperty(key)) {147// We're running the iterations one step out of sync so we can detect148// the last iteration without have to scan the object twice and create149// an itermediate keys array.150if (priorKey) {151execIteration(priorKey, i-1);152}153priorKey = key;154i++;155}156}157if (priorKey) {158execIteration(priorKey, i-1, true);159}160}161}162163if(i === 0){164ret = inverse(this);165}166167return ret;168});169170instance.registerHelper('if', function(conditional, options) {171if (isFunction(conditional)) { conditional = conditional.call(this); }172173// Default behavior is to render the positive path if the value is truthy and not empty.174// The `includeZero` option may be set to treat the condtional as purely not empty based on the175// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.176if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {177return options.inverse(this);178} else {179return options.fn(this);180}181});182183instance.registerHelper('unless', function(conditional, options) {184return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});185});186187instance.registerHelper('with', function(context, options) {188if (isFunction(context)) { context = context.call(this); }189190var fn = options.fn;191192if (!Utils.isEmpty(context)) {193if (options.data && options.ids) {194var data = createFrame(options.data);195data.contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]);196options = {data:data};197}198199return fn(context, options);200} else {201return options.inverse(this);202}203});204205instance.registerHelper('log', function(message, options) {206var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;207instance.log(level, message);208});209210instance.registerHelper('lookup', function(obj, field) {211return obj && obj[field];212});213}214215export var logger = {216methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },217218// State enum219DEBUG: 0,220INFO: 1,221WARN: 2,222ERROR: 3,223level: 1,224225// Can be overridden in the host environment226log: function(level, message) {227if (typeof console !== 'undefined' && logger.level <= level) {228var method = logger.methodMap[level];229(console[method] || console.log).call(console, message);230}231}232};233234export var log = logger.log;235236export var createFrame = function(object) {237var frame = Utils.extend({}, object);238frame._parent = object;239return frame;240};241242243