instanceof.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const CONSTRUCTORS = {
  4. Object,
  5. Array,
  6. Function,
  7. Number,
  8. String,
  9. Date,
  10. RegExp,
  11. };
  12. /* istanbul ignore else */
  13. if (typeof Buffer != "undefined")
  14. CONSTRUCTORS.Buffer = Buffer;
  15. /* istanbul ignore else */
  16. if (typeof Promise != "undefined")
  17. CONSTRUCTORS.Promise = Promise;
  18. const getDef = Object.assign(_getDef, { CONSTRUCTORS });
  19. function _getDef() {
  20. return {
  21. keyword: "instanceof",
  22. schemaType: ["string", "array"],
  23. compile(schema) {
  24. if (typeof schema == "string") {
  25. const C = getConstructor(schema);
  26. return (data) => data instanceof C;
  27. }
  28. if (Array.isArray(schema)) {
  29. const constructors = schema.map(getConstructor);
  30. return (data) => {
  31. for (const C of constructors) {
  32. if (data instanceof C)
  33. return true;
  34. }
  35. return false;
  36. };
  37. }
  38. /* istanbul ignore next */
  39. throw new Error("ajv implementation error");
  40. },
  41. metaSchema: {
  42. anyOf: [{ type: "string" }, { type: "array", items: { type: "string" } }],
  43. },
  44. };
  45. }
  46. function getConstructor(c) {
  47. const C = CONSTRUCTORS[c];
  48. if (C)
  49. return C;
  50. throw new Error(`invalid "instanceof" keyword value ${c}`);
  51. }
  52. exports.default = getDef;
  53. module.exports = getDef;
  54. //# sourceMappingURL=instanceof.js.map