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

Added piped helpers #261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Added piped helpers #261

wants to merge 2 commits into from

Conversation

techhead
Copy link

@techhead techhead commented Oct 1, 2012

Implementation of piped helpers based on the discussion found here: #139

See the new tests.

({
    numbers: [1,2,3,4],
    sum: function() {
        var total = 0;
        for (var i=0,len=this.length; i<len; i++) {
            total += this[i];
        }
        return total;
    }
})
{{numbers|sum}}
({
    timestamp: 1,
    iso8601Date: function() {
        return new Date(this).toISOString();
    }
})
{{timestamp | iso8601Date}}

Helpers are simply lambdas.

{{a | b}} is identical to {{#a}}{{b}}{{/a}} when 'a' is a single value

It is not identical when 'a' is a list. The list will become the context of the lambda to allow for 'reduce' type functions.

@techhead
Copy link
Author

techhead commented Oct 1, 2012

I have to make a correction to the statement

{{a | b}} is identical to {{#a}}{{b}}{{/a}} when 'a' is a single value

The two are only equivalent when 'a' is a single truthy value.

@techhead
Copy link
Author

techhead commented Oct 2, 2012

@mjijackson I've incorporated the changes that we talked about.

@techhead techhead mentioned this pull request Oct 2, 2012
@groue
Copy link

groue commented Nov 2, 2012

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 {{ last(people).name }}, and filters that take several arguments, as in {{ localize(date, format) }}.

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?

@raphaelkieling
Copy link

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);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants