Created
February 3, 2016 14:56
-
-
Save gmmorris/8113c0de6f87d9d53501 to your computer and use it in GitHub Desktop.
A small example of using destructuring for named function arguments and orderless optional arguments. This allows you to create a single function with different argument options without having to maintain complex argument ordering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isFunction = fn => typeof fn === 'function' | |
const isNumber = n => typeof n === 'number' && !isNaN(n) | |
function createFunctionPipe({ interval, fn, args = [] } = {}) { | |
if(!isFunction(fn)){ | |
throw Error('Invalid callback (fn) argument'); | |
} | |
if(isNumber(interval)) { | |
return setTimeout(() => fn(...args), interval); | |
} | |
return function() { | |
return fn(...args.concat(...arguments)); | |
} | |
} | |
createFunctionPipe({ | |
interval: 300, | |
fn : console.log, | |
args: [0,9] | |
}); | |
createFunctionPipe({ | |
fn : console.log, | |
args: [1,2] | |
})(3,4,5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment