Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline Operator #691

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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])
);

Expand Down Expand Up @@ -460,9 +487,14 @@
cache[name] = value;
}


if (isFunction(value))
value = value.call(this.view);

if (pipelines){
value = this.resolvePipelineOperator(value, pipelines);
}

return value;
};

Expand Down
2 changes: 1 addition & 1 deletion mustache.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mustache",
"version": "3.0.1",
"name": "terun-mustache",
"version": "3.0.3",
"description": "Logic-less {{mustache}} templates with JavaScript",
"author": "mustache.js Authors <http://github.com/janl/mustache.js>",
"homepage": "https://github.com/janl/mustache.js",
Expand Down Expand Up @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions test/_files/cli_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Howdy LeBron, CLI rox
34 changes: 34 additions & 0 deletions test/_files/pipeline.js
Original file line number Diff line number Diff line change
@@ -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"
}
})
57 changes: 57 additions & 0 deletions test/_files/pipeline.mustache
Original file line number Diff line number Diff line change
@@ -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}}
55 changes: 55 additions & 0 deletions test/_files/pipeline.txt
Original file line number Diff line number Diff line change
@@ -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