AncientSwordRage
AncientSwordRage

Reputation: 7615

How do you uncurry/reverse curry in Javascript ES6?

I have a function I have to wrap another function if it exists or replace it if it doesn't. This means the number of arguments changes depending on the circumstance.

Here's what I mean -

List of columns:

const myColumns = [
    {name: 'My Field', formatter: function(row, cell, value, colDef, data){
        ... do stuff to value...
        return value;
    },
    {name: 'My Other Field'}
]

Some have formatters and some don't. Here's my partial solution for 'wrapping' the function:

return myColumns.map(columns => {
    function wrapFormatting(value){
        return `<span class="foobar">${value}</value>`
    }
    if( column.formatters ) {
        column.formatters = _.flow(column.formatter, wrapFormatting);
    } else {
        column.formatter = (row, cell, value) => value => wrapFormatting;
    }
})

My naive expectation is that in the else block formatter we 'reverse curry'/uncurry the three arguments and it would end up looking like this:

column.formatter = function(row, cell, value){
    wrapFormatting(value);
}

But eslint is telling me that value is a) already declared in the upper scope and b) defined but never used. If I change one of them (so I have (row, cell, value) => val => wrapFormatting I have them both as 'defined but never used'.

I feel like I've missed a trick, because I can't have (row, cell, value) => value => wrapFormatting(value) as that will immediately invoke the function, and it won't be called when column.formatter is called.

Upvotes: 1

Views: 479

Answers (1)

Bergi
Bergi

Reputation: 664548

You are looking for

column.formatter = (row, cell, value) => wrapFormatting(value);

Not sure why you thought you'd have value as parameter twice and never call wrapFormatting but return it.

Upvotes: 3

Related Questions