Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d50829b

Browse files
jessehouchinsbtford
authored andcommitted
chore($resource): refactor confusing case statement
1 parent 6addac3 commit d50829b

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

src/ngResource/resource.js

+26-38
Original file line numberDiff line numberDiff line change
@@ -503,45 +503,33 @@ angular.module('ngResource', ['ng']).
503503
forEach(actions, function (action, name) {
504504
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
505505

506-
Resource[name] = function (a1, a2, a3, a4) {
507-
var params = {}, data, success, error;
508-
509-
/* jshint -W086 */ /* (purposefully fall through case statements) */
510-
switch (arguments.length) {
511-
case 4:
512-
error = a4;
513-
success = a3;
514-
//fallthrough
515-
case 3:
516-
case 2:
517-
if (isFunction(a2)) {
518-
if (isFunction(a1)) {
519-
success = a1;
520-
error = a2;
521-
break;
522-
}
523-
524-
success = a2;
525-
error = a3;
526-
//fallthrough
527-
} else {
528-
params = a1;
529-
data = a2;
530-
success = a3;
531-
break;
532-
}
533-
case 1:
534-
if (isFunction(a1)) success = a1;
535-
else if (hasBody) data = a1;
536-
else params = a1;
537-
break;
538-
case 0: break;
539-
default:
540-
throw $resourceMinErr('badargs',
541-
"Expected up to 4 arguments [params, data, success, error], got {0} arguments",
542-
arguments.length);
506+
Resource[name] = function () {
507+
if (arguments.length > 4) {
508+
throw $resourceMinErr('badargs',
509+
"Expected up to 4 arguments " +
510+
"[params, data, success, error], " +
511+
"got {0} arguments",
512+
arguments.length);
513+
}
514+
515+
var params, data, success, error;
516+
517+
// Find success and error callbacks
518+
for (var i = 0; i < arguments.length; i++) {
519+
if (isFunction(arguments[i])) {
520+
if (success) error = arguments[i];
521+
else success = arguments[i];
522+
arguments[i] = undefined; // reset to avoid setting data or params to a function
523+
}
524+
}
525+
526+
// Set data and params
527+
if (arguments.length <= 2 && hasBody) {
528+
data = arguments[0];
529+
} else {
530+
params = arguments[0];
531+
data = arguments[1];
543532
}
544-
/* jshint +W086 */ /* (purposefully fall through case statements) */
545533

546534
var isInstanceCall = this instanceof Resource;
547535
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data));

0 commit comments

Comments
 (0)