Created
November 13, 2017 01:41
-
-
Save danny-andrews/6085ff3cd9a8442513400d78a50d6a89 to your computer and use it in GitHub Desktop.
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
function namedCurry(source_fn, arg_order){ | |
function receiver( received_values, n_received, defaults ){ | |
received_values = (received_values || []).slice() | |
n_received = n_received || 0 | |
Object.keys(defaults).forEach(function( input_arg ){ | |
var value = defaults[ input_arg ] | |
var required_index = arg_order.indexOf( input_arg ) | |
var is_known_argument = required_index > -1 | |
var existing_value = received_values[required_index] | |
if( is_known_argument ){ | |
if( typeof existing_value == "undefined" ){ | |
n_received++ | |
} | |
received_values[required_index] = value; | |
} | |
}) | |
if( n_received >= arg_order.length ){ | |
return source_fn.apply(null, received_values ) | |
} else { | |
return receiver.bind( null, received_values, n_received ) | |
} | |
} | |
return receiver.bind(null, null, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment