normalize.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { format } from "./isomorphic.node";
  2. /**
  3. * Normalizes Ono options, accounting for defaults and optional options.
  4. */
  5. export function normalizeOptions(options) {
  6. options = options || {};
  7. return {
  8. concatMessages: options.concatMessages === undefined ? true : Boolean(options.concatMessages),
  9. format: options.format === undefined ? format
  10. : (typeof options.format === "function" ? options.format : false),
  11. };
  12. }
  13. /**
  14. * Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
  15. */
  16. export function normalizeArgs(args, options) {
  17. let originalError;
  18. let props;
  19. let formatArgs;
  20. let message = "";
  21. // Determine which arguments were actually specified
  22. if (typeof args[0] === "string") {
  23. formatArgs = args;
  24. }
  25. else if (typeof args[1] === "string") {
  26. if (args[0] instanceof Error) {
  27. originalError = args[0];
  28. }
  29. else {
  30. props = args[0];
  31. }
  32. formatArgs = args.slice(1);
  33. }
  34. else {
  35. originalError = args[0];
  36. props = args[1];
  37. formatArgs = args.slice(2);
  38. }
  39. // If there are any format arguments, then format the error message
  40. if (formatArgs.length > 0) {
  41. if (options.format) {
  42. message = options.format.apply(undefined, formatArgs);
  43. }
  44. else {
  45. message = formatArgs.join(" ");
  46. }
  47. }
  48. if (options.concatMessages && originalError && originalError.message) {
  49. // The inner-error's message will be added to the new message
  50. message += (message ? " \n" : "") + originalError.message;
  51. }
  52. return { originalError, props, message };
  53. }
  54. //# sourceMappingURL=normalize.js.map