Skip to content

Commit a313942

Browse files
committed
Add pipeline operator code janl#691
1 parent 884dee3 commit a313942

File tree

2 files changed

+7514
-5778
lines changed

2 files changed

+7514
-5778
lines changed

mustache.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var spaceRe = /\s+/;
8181
var equalsRe = /\s*=/;
8282
var curlyRe = /\s*\}/;
8383
var tagRe = /#|\^|\/|>|\{|&|=|!/;
84+
var pipelineRe = /\|\>/;
8485

8586
/**
8687
* Breaks up the given `template` string into a tree of tokens. If the `tags`
@@ -398,11 +399,46 @@ Context.prototype.push = function push(view) {
398399
return new Context(view, this);
399400
};
400401

402+
Context.prototype.resolvePipelineOperator = function resolvePipelineOperator(
403+
value,
404+
pipelines,
405+
) {
406+
var self = this;
407+
var pipelineResolver = function pipelineResolver(val, func) {
408+
var findFunction = function (
409+
instance,
410+
functionToFind,
411+
valueToPutInFoundFunction,
412+
depth,
413+
) {
414+
if (depth <= 0 || !instance) return null;
415+
if (instance.view.hasOwnProperty(functionToFind))
416+
return instance.view[func](valueToPutInFoundFunction);
417+
418+
return findFunction(instance.parent, functionToFind, val, depth);
419+
};
420+
421+
var foundFunction = findFunction(self, func, val, 20);
422+
423+
return foundFunction ? foundFunction : val;
424+
};
425+
return pipelines.reduce(pipelineResolver, value);
426+
};
427+
401428
/**
402429
* Returns the value of the given name in this context, traversing
403430
* up the context hierarchy if the value is absent in this context's view.
404431
*/
405432
Context.prototype.lookup = function lookup(name) {
433+
// {{variable |> pipelineOne |> pipelineTwo}}
434+
// output: [variable,pipelineOne, pipelineTwo ]
435+
var replacedName = name
436+
.replace(new RegExp(spaceRe, "g"), "")
437+
.split(pipelineRe);
438+
439+
name = replacedName.shift();
440+
var pipelines = replacedName;
441+
406442
var cache = this.cache;
407443

408444
var value;
@@ -484,6 +520,10 @@ Context.prototype.lookup = function lookup(name) {
484520

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

523+
if (pipelines) {
524+
value = this.resolvePipelineOperator(value, pipelines);
525+
}
526+
487527
return value;
488528
};
489529

0 commit comments

Comments
 (0)