Reputation: 73
I have a function called 'func':
const otherFunc = (arg1, arg2) => {...}
const func = (condition1, condition2) => {
condition1 || condition2 ? otherFunc(value, true) : otherFunc(false)
}
The previous way works, but i'm wondering if is there is a way to avoid using two different calls to otherFunc. I tried this but is not correct syntax:
const func = (condition1, condition2) => {
otherFunc((condition1 || condition2) && ...[value, true])
}
Edit: It is a simple function to chain if and elses:
function is(value, done) {
return {
else: (v) => is(done ? value : v, done),
if: (condition) => (done || condition ? is(value, true) : is(null)),
get: value,
};
}
const n = 50000;
console.log(
is('small')
.if(n < 1000)
.else('big')
.if(n > 10000)
.else('medium-big')
.if(n > 5000)
.else('medium-small').get,
);
Upvotes: 0
Views: 218
Reputation: 370759
If you can put the arguments into an array, you can then alternate between the arrays to spread into the argument list.
otherFunc(
...((condition1 || condition2) ? [value, true] : [false])
);
That said, it isn't very readable. I'd really prefer
if (condition1 || condition2) {
otherFunc(value, true);
} else {
otherFunc(false);
}
Good maintainable code isn't a golfing competition - being as DRY as absolutely possible isn't always the best approach.
Upvotes: 3