Skip to content

Commit

Permalink
Rewrite Function#optionize for clarity and to use the built-in `lengt…
Browse files Browse the repository at this point in the history
…h` property for determing the arity of a function.
  • Loading branch information
savetheclocktower committed Nov 13, 2009
1 parent 4a54e32 commit f6b4dfa
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/extensions/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@
* logOptions(1,2,3, {hello: "world"}) -> logs 3
**/
Function.prototype.optionize = function(){
var self = this, argumentNames = self.argumentNames(), optionIndex = argumentNames.length - 1,
method = function(){
var args = $A(arguments), options = typeof args.last() == 'object' ? args.pop() : {},
prefilledArgs = (optionIndex == 0 ? [] :
((args.length > 0 ? args : [null]).inGroupsOf(optionIndex).flatten())).concat(options);
return self.apply(this, prefilledArgs);
var self = this, argumentNames = self.argumentNames(), optionIndex = this.length - 1;

var method = function() {
var args = $A(arguments);

var options = (typeof args.last() === 'object') ? args.pop() : {};
var prefilledArgs = [];
if (optionIndex > 0) {
prefilledArgs = ((args.length > 0 ? args : [null]).inGroupsOf(
optionIndex).flatten()).concat(options);
}

return self.apply(this, prefilledArgs);
};
method.argumentNames = function(){ return argumentNames };
method.argumentNames = function() { return argumentNames; };
return method;
};

Expand Down

0 comments on commit f6b4dfa

Please sign in to comment.