Bläddra i källkod

Added pipeline helper, with tests

Can write with | and |> to use this function.
pull/691/head
Kieling 7 år sedan
förälder
incheckning
47e3003b87
5 ändrade filer med 159 tillägg och 5 borttagningar
  1. +33
    -4
      mustache.js
  2. +1
    -1
      mustache.min.js
  3. +25
    -0
      test/_files/pipeline.js
  4. +50
    -0
      test/_files/pipeline.mustache
  5. +50
    -0
      test/_files/pipeline.txt

+ 33
- 4
mustache.js Visa fil

@@ -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,14 +380,37 @@
return new Context(view, this);
};

Context.prototype.resolvePipelineOperator = function resolvePipelineOperator(value, pipelines){
var self = this;
return pipelines.reduce(function(val, func){
if(self.view.hasOwnProperty(func)){
return self.view[func](val);
}else if(self.parent && self.parent.view.hasOwnProperty(func)){
return self.parent.view[func](val);
}else{
return value;
}
}
,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)

var value;
pipelines = replacedName.slice(1);
name = replacedName[0];

var cache = this.cache;
var value
if (cache.hasOwnProperty(name)) {
value = cache[name];
} else {
@@ -418,7 +442,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 +484,14 @@
cache[name] = value;
}


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

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

return value;
};



+ 1
- 1
mustache.min.js
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 25
- 0
test/_files/pipeline.js Visa fil

@@ -0,0 +1,25 @@
({
name: 'name',
job: {
name: 'Developer'
},
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"
}
})

+ 50
- 0
test/_files/pipeline.mustache Visa fil

@@ -0,0 +1,50 @@
-- 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}}

+ 50
- 0
test/_files/pipeline.txt Visa fil

@@ -0,0 +1,50 @@
-- 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

Laddar…
Avbryt
Spara