Skip to content

Instantly share code, notes, and snippets.

@tal
Last active August 29, 2015 14:01
Show Gist options
  • Save tal/70462593c883bc8a9485 to your computer and use it in GitHub Desktop.
Save tal/70462593c883bc8a9485 to your computer and use it in GitHub Desktop.
callableWithNmaed = (fn) ->
fn_s = fn.toString()
if m = fn_s.match(/function[\w\s]*\((.*?)\)/)
arg_names = m[1].split(/[\s,]+/)
(args) ->
call_args = for arg_name in arg_names
args[arg_name]
call_args.push args
fn.apply(null, call_args)
fn1 = (foo, bar) ->
foo+bar
fn2 = (bar, foo) ->
foo+bar
opts = {foo: 'foo', bar: 'bar'}
console.log fn1.toString(), callableWithNmaed(fn1)(opts)
console.log fn2.toString(), callableWithNmaed(fn2)(opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment