singleton.js 1.5 KB

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