31
loading...
This website collects cookies to deliver better user experience
runAll
function you could implement as following:const runAll = (...fns) => fns.forEach(func => func?.())
If you don't know what this (func?.()
) does it just runs function only when it's defined
const handleRefresh = runAll(closeModal, clearSearch, clearFilter, clearSort)
handleRefresh()
const chain = (...fns) => (...args) => fns.reduce((arg, func) => func(arg), args)
If you want to have more math alike nesting you use reduceRight
so if you combine a,b,c
it's interpreted as a(b(c))
instead of c(b(a))
.
const increment = (x) => +x + 1
const decrement = (x) => +x - 1
const square = x => x * x
const chained = chain(increment, square, decrement)
console.log(chained(2)); // 8
const combine = (...fns) => (...args) => fns.reduce((results, func) => ({ ...results, ...func?.(...args) }), {})
const funcA = (prop) => ({
common: 'A',
propA: 'A ' + prop
})
const funcB = (prop) => ({
common: 'B',
propB: 'B ' + prop
})
const funcC = (prop) => ({
common: 'C',
propC: 'C ' + prop
})
const combined = combine(funcA, funcB, funcC)
console.log(combined('demo')) // { common: 'C', propA: 'A demo', propB: 'B demo', propC: 'C demo' }
common
that was shared between all functions got overridden by funcC
.It's the same as just calling all functions and doing {...resA, ...resB, ...redC}