normalize-args.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. const Options = require("./options");
  3. module.exports = normalizeArgs;
  4. /**
  5. * Normalizes the given arguments, accounting for optional args.
  6. *
  7. * @param {Arguments} args
  8. * @returns {object}
  9. */
  10. function normalizeArgs (args) {
  11. let path, schema, options, callback;
  12. args = Array.prototype.slice.call(args);
  13. if (typeof args[args.length - 1] === "function") {
  14. // The last parameter is a callback function
  15. callback = args.pop();
  16. }
  17. if (typeof args[0] === "string") {
  18. // The first parameter is the path
  19. path = args[0];
  20. if (typeof args[2] === "object") {
  21. // The second parameter is the schema, and the third parameter is the options
  22. schema = args[1];
  23. options = args[2];
  24. }
  25. else {
  26. // The second parameter is the options
  27. schema = undefined;
  28. options = args[1];
  29. }
  30. }
  31. else {
  32. // The first parameter is the schema
  33. path = "";
  34. schema = args[0];
  35. options = args[1];
  36. }
  37. if (!(options instanceof Options)) {
  38. options = new Options(options);
  39. }
  40. return {
  41. path,
  42. schema,
  43. options,
  44. callback
  45. };
  46. }