Skip to content

Commit

Permalink
Firming up our expected order of arguments for the availability method
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Caron committed Nov 6, 2015
1 parent cec85c0 commit 045c775
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/AvailabilityStrategy.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
'use strict';

var AvailabilityStrategy = function() {

return function() {
var sku;
var storeIds;
return function(sku, storeIds) {
var options = {
path: '/',
qs: {}
};

var method;

for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (!isNaN(arg) && typeof arg === 'number') {
// SKU
sku = arg;
} else if (Array.isArray(arg)) {
// Store IDs
storeIds = arg.join(',');
} else if (typeof arg === 'function') {
// Result callback
options.callback = arg;
if (typeof sku !== 'number' && typeof sku !== 'string') {
throw 'First parameter of "availability" must be the SKU, and it must be either a number or a string';
}

if (typeof storeIds !== 'number' && Array.isArray(storeIds) === false) {
throw 'Second parameter of "availability" must be store id(s), and it must be either a number or array of numbers';
} else if (Array.isArray(storeIds)) {
storeIds = storeIds.join(',');
}

if (arguments.length === 4) {
options.qs = arguments[2];
options.callback = arguments[3];
} else if (arguments.length === 3) {
if (typeof arguments[2] === 'function') {
options.callback = arguments[2];
} else if (typeof arguments[2] === 'object') {
options.qs = arguments[2];
} else {
// Assume any object literal is a set of query params
options.qs = arg;
throw 'Unrecognized third parameter when calling "availability" method';
}
}

Expand Down

0 comments on commit 045c775

Please sign in to comment.