diff --git a/mustache.js b/mustache.js index 8ec1b44cc..ee14306f2 100644 --- a/mustache.js +++ b/mustache.js @@ -49,7 +49,7 @@ * Safe way of detecting whether or not the given thing is a primitive and * whether it has the given property */ - function primitiveHasOwnProperty (primitive, propName) { + function primitiveHasOwnProperty (primitive, propName) { return ( primitive != null && typeof primitive !== 'object' @@ -92,6 +92,7 @@ var equalsRe = /\s*=/; var curlyRe = /\s*\}/; var tagRe = /#|\^|\/|>|\{|&|=|!/; + var pipelineRe = /\|\>?/; /** * Breaks up the given `template` string into a tree of tokens. If the `tags` @@ -379,13 +380,39 @@ return new Context(view, this); }; + Context.prototype.resolvePipelineOperator = function resolvePipelineOperator (value, pipelines){ + var self = this; + var pipelineResolver = function pipelineResolver (val, func){ + var findFunction = function(instance, functionToFind, valueToPutInFindedFunction, depth){ + if(depth <= 0 || !instance) return null; + if(instance.view.hasOwnProperty(functionToFind)) return instance.view[func](valueToPutInFindedFunction); + + return findFunction(instance.parent, functionToFind, val, depth) + } + + var findedFunction = findFunction(self, func, val, 20) + + return findedFunction ? findedFunction : val + }; + return pipelines.reduce(pipelineResolver,value); + }; + /** * Returns the value of the given name in this context, traversing * up the context hierarchy if the value is absent in this context's view. */ Context.prototype.lookup = function lookup (name) { - var cache = this.cache; + // {{variable | pipelineOne |> pipelineTwo}} + // output: [variable,pipelineOne, pipelineTwo ] + // Can use | or |>. + var replacedName = name + .replace(new RegExp(spaceRe, 'g'),'') + .split(pipelineRe); + + name = replacedName.shift(); + pipelines = replacedName; + var cache = this.cache; var value; if (cache.hasOwnProperty(name)) { value = cache[name]; @@ -418,7 +445,7 @@ while (intermediateValue != null && index < names.length) { if (index === names.length - 1) lookupHit = ( - hasProperty(intermediateValue, names[index]) + hasProperty(intermediateValue, names[index]) || primitiveHasOwnProperty(intermediateValue, names[index]) ); @@ -460,9 +487,14 @@ cache[name] = value; } + if (isFunction(value)) value = value.call(this.view); + if (pipelines){ + value = this.resolvePipelineOperator(value, pipelines); + } + return value; }; diff --git a/mustache.min.js b/mustache.min.js index 7ad4c009c..e60ea823a 100644 --- a/mustache.min.js +++ b/mustache.min.js @@ -1 +1 @@ -(function defineMustache(global,factory){if(typeof exports==="object"&&exports&&typeof exports.nodeName!=="string"){factory(exports)}else if(typeof define==="function"&&define.amd){define(["exports"],factory)}else{global.Mustache={};factory(global.Mustache)}})(this,function mustacheFactory(mustache){var objectToString=Object.prototype.toString;var isArray=Array.isArray||function isArrayPolyfill(object){return objectToString.call(object)==="[object Array]"};function isFunction(object){return typeof object==="function"}function typeStr(obj){return isArray(obj)?"array":typeof obj}function escapeRegExp(string){return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function hasProperty(obj,propName){return obj!=null&&typeof obj==="object"&&propName in obj}function primitiveHasOwnProperty(primitive,propName){return primitive!=null&&typeof primitive!=="object"&&primitive.hasOwnProperty&&primitive.hasOwnProperty(propName)}var regExpTest=RegExp.prototype.test;function testRegExp(re,string){return regExpTest.call(re,string)}var nonSpaceRe=/\S/;function isWhitespace(string){return!testRegExp(nonSpaceRe,string)}var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function escapeHtml(string){return String(string).replace(/[&<>"'`=\/]/g,function fromEntityMap(s){return entityMap[s]})}var whiteRe=/\s*/;var spaceRe=/\s+/;var equalsRe=/\s*=/;var curlyRe=/\s*\}/;var tagRe=/#|\^|\/|>|\{|&|=|!/;function parseTemplate(template,tags){if(!template)return[];var sections=[];var tokens=[];var spaces=[];var hasTag=false;var nonSpace=false;function stripSpace(){if(hasTag&&!nonSpace){while(spaces.length)delete tokens[spaces.pop()]}else{spaces=[]}hasTag=false;nonSpace=false}var openingTagRe,closingTagRe,closingCurlyRe;function compileTags(tagsToCompile){if(typeof tagsToCompile==="string")tagsToCompile=tagsToCompile.split(spaceRe,2);if(!isArray(tagsToCompile)||tagsToCompile.length!==2)throw new Error("Invalid tags: "+tagsToCompile);openingTagRe=new RegExp(escapeRegExp(tagsToCompile[0])+"\\s*");closingTagRe=new RegExp("\\s*"+escapeRegExp(tagsToCompile[1]));closingCurlyRe=new RegExp("\\s*"+escapeRegExp("}"+tagsToCompile[1]))}compileTags(tags||mustache.tags);var scanner=new Scanner(template);var start,type,value,chr,token,openSection;while(!scanner.eos()){start=scanner.pos;value=scanner.scanUntil(openingTagRe);if(value){for(var i=0,valueLength=value.length;i0?sections[sections.length-1][4]:nestedTokens;break;default:collector.push(token)}}return nestedTokens}function Scanner(string){this.string=string;this.tail=string;this.pos=0}Scanner.prototype.eos=function eos(){return this.tail===""};Scanner.prototype.scan=function scan(re){var match=this.tail.match(re);if(!match||match.index!==0)return"";var string=match[0];this.tail=this.tail.substring(string.length);this.pos+=string.length;return string};Scanner.prototype.scanUntil=function scanUntil(re){var index=this.tail.search(re),match;switch(index){case-1:match=this.tail;this.tail="";break;case 0:match="";break;default:match=this.tail.substring(0,index);this.tail=this.tail.substring(index)}this.pos+=match.length;return match};function Context(view,parentContext){this.view=view;this.cache={".":this.view};this.parent=parentContext}Context.prototype.push=function push(view){return new Context(view,this)};Context.prototype.lookup=function lookup(name){var cache=this.cache;var value;if(cache.hasOwnProperty(name)){value=cache[name]}else{var context=this,intermediateValue,names,index,lookupHit=false;while(context){if(name.indexOf(".")>0){intermediateValue=context.view;names=name.split(".");index=0;while(intermediateValue!=null&&index")value=this.renderPartial(token,context,partials,tags);else if(symbol==="&")value=this.unescapedValue(token,context);else if(symbol==="name")value=this.escapedValue(token,context);else if(symbol==="text")value=this.rawValue(token);if(value!==undefined)buffer+=value}return buffer};Writer.prototype.renderSection=function renderSection(token,context,partials,originalTemplate){var self=this;var buffer="";var value=context.lookup(token[1]);function subRender(template){return self.render(template,context,partials)}if(!value)return;if(isArray(value)){for(var j=0,valueLength=value.length;j":">",'"':""","'":"'","/":"/","`":"`","=":"="};function escapeHtml(string){return String(string).replace(/[&<>"'`=\/]/g,function fromEntityMap(s){return entityMap[s]})}var whiteRe=/\s*/;var spaceRe=/\s+/;var equalsRe=/\s*=/;var curlyRe=/\s*\}/;var tagRe=/#|\^|\/|>|\{|&|=|!/;var pipelineRe=/\|\>?/;function parseTemplate(template,tags){if(!template)return[];var sections=[];var tokens=[];var spaces=[];var hasTag=false;var nonSpace=false;function stripSpace(){if(hasTag&&!nonSpace){while(spaces.length)delete tokens[spaces.pop()]}else{spaces=[]}hasTag=false;nonSpace=false}var openingTagRe,closingTagRe,closingCurlyRe;function compileTags(tagsToCompile){if(typeof tagsToCompile==="string")tagsToCompile=tagsToCompile.split(spaceRe,2);if(!isArray(tagsToCompile)||tagsToCompile.length!==2)throw new Error("Invalid tags: "+tagsToCompile);openingTagRe=new RegExp(escapeRegExp(tagsToCompile[0])+"\\s*");closingTagRe=new RegExp("\\s*"+escapeRegExp(tagsToCompile[1]));closingCurlyRe=new RegExp("\\s*"+escapeRegExp("}"+tagsToCompile[1]))}compileTags(tags||mustache.tags);var scanner=new Scanner(template);var start,type,value,chr,token,openSection;while(!scanner.eos()){start=scanner.pos;value=scanner.scanUntil(openingTagRe);if(value){for(var i=0,valueLength=value.length;i0?sections[sections.length-1][4]:nestedTokens;break;default:collector.push(token)}}return nestedTokens}function Scanner(string){this.string=string;this.tail=string;this.pos=0}Scanner.prototype.eos=function eos(){return this.tail===""};Scanner.prototype.scan=function scan(re){var match=this.tail.match(re);if(!match||match.index!==0)return"";var string=match[0];this.tail=this.tail.substring(string.length);this.pos+=string.length;return string};Scanner.prototype.scanUntil=function scanUntil(re){var index=this.tail.search(re),match;switch(index){case-1:match=this.tail;this.tail="";break;case 0:match="";break;default:match=this.tail.substring(0,index);this.tail=this.tail.substring(index)}this.pos+=match.length;return match};function Context(view,parentContext){this.view=view;this.cache={".":this.view};this.parent=parentContext}Context.prototype.push=function push(view){return new Context(view,this)};Context.prototype.resolvePipelineOperator=function resolvePipelineOperator(value,pipelines){var self=this;var pipelineResolver=function pipelineResolver(val,func){var findFunction=function(instance,functionToFind,valueToPutInFindedFunction,depth){if(depth<=0||!instance)return null;if(instance.view.hasOwnProperty(functionToFind))return instance.view[func](valueToPutInFindedFunction);return findFunction(instance.parent,functionToFind,val,depth)};var findedFunction=findFunction(self,func,val,20);return findedFunction?findedFunction:val};return pipelines.reduce(pipelineResolver,value)};Context.prototype.lookup=function lookup(name){var replacedName=name.replace(new RegExp(spaceRe,"g"),"").split(pipelineRe);name=replacedName.shift();pipelines=replacedName;var cache=this.cache;var value;if(cache.hasOwnProperty(name)){value=cache[name]}else{var context=this,intermediateValue,names,index,lookupHit=false;while(context){if(name.indexOf(".")>0){intermediateValue=context.view;names=name.split(".");index=0;while(intermediateValue!=null&&index")value=this.renderPartial(token,context,partials,tags);else if(symbol==="&")value=this.unescapedValue(token,context);else if(symbol==="name")value=this.escapedValue(token,context);else if(symbol==="text")value=this.rawValue(token);if(value!==undefined)buffer+=value}return buffer};Writer.prototype.renderSection=function renderSection(token,context,partials,originalTemplate){var self=this;var buffer="";var value=context.lookup(token[1]);function subRender(template){return self.render(template,context,partials)}if(!value)return;if(isArray(value)){for(var j=0,valueLength=value.length;j", "homepage": "https://github.com/janl/mustache.js", @@ -44,7 +44,7 @@ "eslint": "2.5.1", "jshint": "^2.9.5", "mocha": "^3.0.2", - "uglify-js": "^3.4.6", + "uglify-js": "^3.4.6" "zuul": "^3.11.0", "zuul-ngrok": "nolanlawson/zuul-ngrok#patch-1" }, diff --git a/test/_files/cli_output.txt b/test/_files/cli_output.txt new file mode 100644 index 000000000..66556fe0a --- /dev/null +++ b/test/_files/cli_output.txt @@ -0,0 +1 @@ +Howdy LeBron, CLI rox \ No newline at end of file diff --git a/test/_files/pipeline.js b/test/_files/pipeline.js new file mode 100644 index 000000000..543a57abb --- /dev/null +++ b/test/_files/pipeline.js @@ -0,0 +1,34 @@ +({ + name: 'name', + job: { + name: 'Developer' + }, + attributes:[ + { + name: 'good', + options: [ + 'outgoing', + 'shy' + ] + } + ], + numbers:[1,2,3], + sumOne:(value)=>{ + return value + 1; + }, + upper:(value)=>{ + return value.toUpperCase() + }, + lower: (value)=>{ + return value.toLowerCase() + }, + firstUpper:(value) =>{ + value = value.split('') + value[0] = value[0].toUpperCase() + value = value.join('') + return value; + }, + setNameFuncion:(value)=>{ + return value + ": This is a value" + } +}) diff --git a/test/_files/pipeline.mustache b/test/_files/pipeline.mustache new file mode 100644 index 000000000..3eba2da07 --- /dev/null +++ b/test/_files/pipeline.mustache @@ -0,0 +1,57 @@ +-- Use | +{{name | upper}} +{{name |upper}} +{{name| upper}} +{{name|upper}} +{{name|underscore}} +{{name|underscore }} +{{name|lower}} +{{name | lower}} +{{name |lower}} +{{name |lower | upper}} +{{name |lower | upper | lower | upper}} +{{name |lower | firstUpper}} +{{name | upper | firstUpper}} +{{job.name | upper}} +{{job.name | lower | firstUpper}} +{{#numbers}} +{{. | sumOne}} +{{/numbers}} +{{#numbers}} +{{. | sumOne | setNameFuncion}} +{{/numbers}} +{{#numbers}} +{{. | sumOne | setNameFuncion | lower}} +{{/numbers}} +-- Use |> +{{name |> upper}} +{{name |>upper}} +{{name|> upper}} +{{name|>upper}} +{{name|>underscore}} +{{name|>underscore }} +{{name|>lower}} +{{name |> lower}} +{{name |>lower}} +{{name |>lower |> upper}} +{{name |>lower |> upper |> lower |> upper}} +{{name |>lower |> firstUpper}} +{{name |> upper |> firstUpper}} +{{job.name |> upper}} +{{job.name |> lower |> firstUpper}} +{{#numbers}} +{{. |> sumOne}} +{{/numbers}} +{{#numbers}} +{{. |> sumOne |> setNameFuncion}} +{{/numbers}} +{{#numbers}} +{{. |> sumOne |> setNameFuncion |> lower}} +{{/numbers}} +-- Level Array +{{#attributes}} +{{#options}} +{{name |> upper}} +{{. |> upper}} +{{/options}} +{{/attributes}} \ No newline at end of file diff --git a/test/_files/pipeline.txt b/test/_files/pipeline.txt new file mode 100644 index 000000000..492c4f6b8 --- /dev/null +++ b/test/_files/pipeline.txt @@ -0,0 +1,55 @@ +-- Use | +NAME +NAME +NAME +NAME +name +name +name +name +name +NAME +NAME +Name +NAME +DEVELOPER +Developer +2 +3 +4 +2: This is a value +3: This is a value +4: This is a value +2: this is a value +3: this is a value +4: this is a value +-- Use |> +NAME +NAME +NAME +NAME +name +name +name +name +name +NAME +NAME +Name +NAME +DEVELOPER +Developer +2 +3 +4 +2: This is a value +3: This is a value +4: This is a value +2: this is a value +3: this is a value +4: this is a value +-- Level Array +GOOD +OUTGOING +GOOD +SHY