primitives.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const benchmark_1 = __importDefault(require("benchmark"));
  7. const Mocker_1 = require("../__tests__/Mocker");
  8. const index_1 = require("../index");
  9. const val = new Mocker_1.Mocker();
  10. const enumSuite = new benchmark_1.default.Suite("z.enum");
  11. const enumSchema = index_1.z.enum(["a", "b", "c"]);
  12. enumSuite
  13. .add("valid", () => {
  14. enumSchema.parse("a");
  15. })
  16. .add("invalid", () => {
  17. try {
  18. enumSchema.parse("x");
  19. }
  20. catch (e) { }
  21. })
  22. .on("cycle", (e) => {
  23. console.log(`z.enum: ${e.target}`);
  24. });
  25. const undefinedSuite = new benchmark_1.default.Suite("z.undefined");
  26. const undefinedSchema = index_1.z.undefined();
  27. undefinedSuite
  28. .add("valid", () => {
  29. undefinedSchema.parse(undefined);
  30. })
  31. .add("invalid", () => {
  32. try {
  33. undefinedSchema.parse(1);
  34. }
  35. catch (e) { }
  36. })
  37. .on("cycle", (e) => {
  38. console.log(`z.undefined: ${e.target}`);
  39. });
  40. const literalSuite = new benchmark_1.default.Suite("z.literal");
  41. const short = "short";
  42. const bad = "bad";
  43. const literalSchema = index_1.z.literal("short");
  44. literalSuite
  45. .add("valid", () => {
  46. literalSchema.parse(short);
  47. })
  48. .add("invalid", () => {
  49. try {
  50. literalSchema.parse(bad);
  51. }
  52. catch (e) { }
  53. })
  54. .on("cycle", (e) => {
  55. console.log(`z.literal: ${e.target}`);
  56. });
  57. const numberSuite = new benchmark_1.default.Suite("z.number");
  58. const numberSchema = index_1.z.number().int();
  59. numberSuite
  60. .add("valid", () => {
  61. numberSchema.parse(1);
  62. })
  63. .add("invalid type", () => {
  64. try {
  65. numberSchema.parse("bad");
  66. }
  67. catch (e) { }
  68. })
  69. .add("invalid number", () => {
  70. try {
  71. numberSchema.parse(0.5);
  72. }
  73. catch (e) { }
  74. })
  75. .on("cycle", (e) => {
  76. console.log(`z.number: ${e.target}`);
  77. });
  78. const dateSuite = new benchmark_1.default.Suite("z.date");
  79. const plainDate = index_1.z.date();
  80. const minMaxDate = index_1.z
  81. .date()
  82. .min(new Date("2021-01-01"))
  83. .max(new Date("2030-01-01"));
  84. dateSuite
  85. .add("valid", () => {
  86. plainDate.parse(new Date());
  87. })
  88. .add("invalid", () => {
  89. try {
  90. plainDate.parse(1);
  91. }
  92. catch (e) { }
  93. })
  94. .add("valid min and max", () => {
  95. minMaxDate.parse(new Date("2023-01-01"));
  96. })
  97. .add("invalid min", () => {
  98. try {
  99. minMaxDate.parse(new Date("2019-01-01"));
  100. }
  101. catch (e) { }
  102. })
  103. .add("invalid max", () => {
  104. try {
  105. minMaxDate.parse(new Date("2031-01-01"));
  106. }
  107. catch (e) { }
  108. })
  109. .on("cycle", (e) => {
  110. console.log(`z.date: ${e.target}`);
  111. });
  112. const symbolSuite = new benchmark_1.default.Suite("z.symbol");
  113. const symbolSchema = index_1.z.symbol();
  114. symbolSuite
  115. .add("valid", () => {
  116. symbolSchema.parse(val.symbol);
  117. })
  118. .add("invalid", () => {
  119. try {
  120. symbolSchema.parse(1);
  121. }
  122. catch (e) { }
  123. })
  124. .on("cycle", (e) => {
  125. console.log(`z.symbol: ${e.target}`);
  126. });
  127. exports.default = {
  128. suites: [
  129. enumSuite,
  130. undefinedSuite,
  131. literalSuite,
  132. numberSuite,
  133. dateSuite,
  134. symbolSuite,
  135. ],
  136. };