Alex
Alex

Reputation: 12079

coffeescript chaining calls

Couldn't manage chaining calls using coffee script. I'm trying to reproduce this in coffee script:

function htmlEscape(str) {
    return String(str)
        .replace(/&/g, '&')
        .replace(/"/g, '"')
        .replace(/'/g, ''')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
}

I'm trying this way:

htmlEscape = (str) ->
    String(str)
    .replace (a,b)
    .replace (c,d)

receiving an Parse error on line 13: Unexpected ',' error. Could anyone help me with the proper chaining syntax?

Upvotes: 4

Views: 2030

Answers (1)

tokland
tokland

Reputation: 67900

You must remove these spaces (and probably put an space after the comma):

htmlEscape = (str) ->
    String(str) 
    .replace(a, b) 
    .replace(c, d)

Or:

htmlEscape = (str) ->
    String(str).
      replace(a, b). 
      replace(c, d)

I like the second. Note you can abstract what you are doing using reduce.

Upvotes: 4

Related Questions