singleton.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ono = void 0;
  4. const constructor_1 = require("./constructor");
  5. const singleton = ono;
  6. exports.ono = singleton;
  7. ono.error = new constructor_1.Ono(Error);
  8. ono.eval = new constructor_1.Ono(EvalError);
  9. ono.range = new constructor_1.Ono(RangeError);
  10. ono.reference = new constructor_1.Ono(ReferenceError);
  11. ono.syntax = new constructor_1.Ono(SyntaxError);
  12. ono.type = new constructor_1.Ono(TypeError);
  13. ono.uri = new constructor_1.Ono(URIError);
  14. const onoMap = ono;
  15. /**
  16. * Creates a new error with the specified message, properties, and/or inner error.
  17. * If an inner error is provided, then the new error will match its type, if possible.
  18. */
  19. function ono(...args) {
  20. let originalError = args[0];
  21. // Is the first argument an Error-like object?
  22. if (typeof originalError === "object" && typeof originalError.name === "string") {
  23. // Try to find an Ono singleton method that matches this error type
  24. for (let typedOno of Object.values(onoMap)) {
  25. if (typeof typedOno === "function" && typedOno.name === "ono") {
  26. let species = typedOno[Symbol.species];
  27. if (species && species !== Error && (originalError instanceof species || originalError.name === species.name)) {
  28. // Create an error of the same type
  29. return typedOno.apply(undefined, args);
  30. }
  31. }
  32. }
  33. }
  34. // By default, create a base Error object
  35. return ono.error.apply(undefined, args);
  36. }
  37. //# sourceMappingURL=singleton.js.map