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
Added piped helpers #261
base: master
Are you sure you want to change the base?
Added piped helpers #261
Conversation
I have to make a correction to the statement
The two are only equivalent when 'a' is a single truthy value. |
@mjijackson I've incorporated the changes that we talked about. |
There have been a debate at mustache/spec, but it looks dead now: mustache/spec#41 GRMustache, the Objective-C implementation, has chosen the parenthesis syntax, {{ f(x) }}, for "filters" : https://github.com/groue/GRMustache/blob/master/Guides/filters.md. Highlights are the possibility to build other expressions on top of filtered values, like I'd be happy that janl/mustache.js considers this option, regardless of the trend for pipes today. You can read arguments against pipes at https://github.com/groue/GRMustache/blob/master/Articles/WhyMustacheFilters.md. Since very few Mustache implementations have shipped today with piped helpers, I think it's still time to prevent this bad idea to spread. @janl, @mjijackson, what do you think? |
It's working? i did need and create this: // Split and trim pipes
function splitPipe(value){
if(nullOrUndefined(value)) return []
const pipe = "|";
let trim = (string) => string.trim();
if(value.includes(pipe)) return value.split(pipe).map(trim);
return [value];
}
// Call the functions, `pipelineFunctions` is a object with funcions, because i create default funcions.
function pipe (value, pipes){
if(nullOrUndefined(value)) return value;
if(nullOrUndefined(pipes)) return value;
return pipes.reduce((ant,functionName)=>{
if(pipelineFunctions.hasOwnProperty(functionName)) return pipelineFunctions[functionName](ant);
return ant;
},value)
}
Mustache.Writer.prototype.escapedValue = function escapedValue (token, context) {
let [,tokenValue] = token
let [name, ...pipes] = splitPipe(tokenValue)
let contextValue = context.lookup(name);
let finalValue = pipe(contextValue, pipes)
if (finalValue != null)
return Mustache.escape(finalValue);
}; |
Implementation of piped helpers based on the discussion found here: #139
See the new tests.
Helpers are simply lambdas.
It is not identical when 'a' is a list. The list will become the context of the lambda to allow for 'reduce' type functions.