string.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 index_1 = require("../index");
  8. const SUITE_NAME = "z.string";
  9. const suite = new benchmark_1.default.Suite(SUITE_NAME);
  10. const empty = "";
  11. const short = "short";
  12. const long = "long".repeat(256);
  13. const manual = (str) => {
  14. if (typeof str !== "string") {
  15. throw new Error("Not a string");
  16. }
  17. return str;
  18. };
  19. const stringSchema = index_1.z.string();
  20. const optionalStringSchema = index_1.z.string().optional();
  21. const optionalNullableStringSchema = index_1.z.string().optional().nullable();
  22. suite
  23. .add("empty string", () => {
  24. stringSchema.parse(empty);
  25. })
  26. .add("short string", () => {
  27. stringSchema.parse(short);
  28. })
  29. .add("long string", () => {
  30. stringSchema.parse(long);
  31. })
  32. .add("optional string", () => {
  33. optionalStringSchema.parse(long);
  34. })
  35. .add("nullable string", () => {
  36. optionalNullableStringSchema.parse(long);
  37. })
  38. .add("nullable (null) string", () => {
  39. optionalNullableStringSchema.parse(null);
  40. })
  41. .add("invalid: null", () => {
  42. try {
  43. stringSchema.parse(null);
  44. }
  45. catch (err) { }
  46. })
  47. .add("manual parser: long", () => {
  48. manual(long);
  49. })
  50. .on("cycle", (e) => {
  51. console.log(`${SUITE_NAME}: ${e.target}`);
  52. });
  53. exports.default = {
  54. suites: [suite],
  55. };