1

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.

4
  • 1
    "(row, cell, value) => value => wrapFormatting(value) will immediately invoke the function" - why do you think so? Commented Jul 28, 2017 at 16:14
  • @Bergi because of this bit wrapFormatting(value) Commented Jul 28, 2017 at 16:15
  • Well but your function(row, cell, value){ … contains the exact same bit. Why wouldn't it immediately invoke it there? Commented Jul 28, 2017 at 16:17
  • @Bergi I was having a brain fart and thought function fooBar(... and function(... did somehting different. Commented Jul 28, 2017 at 16:37

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Me neither. Won't that immediately call the wrapFormatting?
@Pureferret no, it defines a function. If you're uncomfortable with arrow functions, you might as well use the equivalent function syntax that you have presented in your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.