Hi!
I have a module, a simple function that takes two parameters. If only one parameter is given, it returns an array as the result and all is fine.
If two paramenters is given an array is also the main result. But for some edge cases, it returns another array with some additional data.
If it was not for the edge (rare) cases that requires an additional array of data, I would have just returned an array, so the user would use my module like this:
var coolModule = require('cool-module');
var resultArray1 = coolModule('parameter1');
var resultArray2 = coolModule('parameter1', 'parameter2');
But since I have some edge cases that requires an additional array, how should I best return data? I see the following alternatives:
// Returned data is object:
var resultArray1 = coolModule('parameter1').resultArray;
var resultArray2 = coolModule('parameter1', 'parameter2').resultArray;
var resultArray3 = coolModule('parameter1', 'parameter2').resultEdgeCasesArray;
// Return data as array when only one paramenter, return data as object if two properties:
var resultArray1 = coolModule('parameter1');
var resultArray2 = coolModule('parameter1', 'parameter2').resultArray;
var resultArray3 = coolModule('parameter1', 'parameter2').resultEdgeCasesArray;
// Use 3 different methods:
var resultArray1 = coolModule.oneParameterMethod('parameter1');
var resultArray2 = coolModule.twoParameterMethod('parameter1', 'parameter2');
var resultArray3 = coolModule.edgeCasesMethod('parameter1', 'parameter2');
// Use an array, and for the edgecase have a special method. But then I would need to create an additional method name for the standard cases:
var resultArray1 = coolModule.standardMethod('parameter1');
var resultArray2 = coolModule.standardMethod('parameter1', 'parameter2');
var resultArray3 = coolModule.edgeCasesMethod('parameter1', 'parameter2');
// Use array in all cases, but for the second add the edge cases result as a property on the array:
var resultArray1 = coolModule('parameter1');
var resultArray2 = coolModule('parameter1', 'parameter2');
var resultArray3 = coolModule('parameter1', 'parameter2').resultEdgeCasesArray;
I would have preferred the last one, since in 99% of the cases the users of the module would just be interested in one array. But since this would limit the users way of working with the array (the user could not use for...in to traverse the array, but would use a normal for-loop), then I guess this is a bad option.
Which would you choose, or maybe there is a better (simplest/most logical/most used) approach?
Thanks you in advance for any thoughs!
Frode