util.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getParsedType = exports.ZodParsedType = exports.objectUtil = exports.util = void 0;
  4. var util;
  5. (function (util) {
  6. util.assertEqual = (val) => val;
  7. function assertIs(_arg) { }
  8. util.assertIs = assertIs;
  9. function assertNever(_x) {
  10. throw new Error();
  11. }
  12. util.assertNever = assertNever;
  13. util.arrayToEnum = (items) => {
  14. const obj = {};
  15. for (const item of items) {
  16. obj[item] = item;
  17. }
  18. return obj;
  19. };
  20. util.getValidEnumValues = (obj) => {
  21. const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
  22. const filtered = {};
  23. for (const k of validKeys) {
  24. filtered[k] = obj[k];
  25. }
  26. return util.objectValues(filtered);
  27. };
  28. util.objectValues = (obj) => {
  29. return util.objectKeys(obj).map(function (e) {
  30. return obj[e];
  31. });
  32. };
  33. util.objectKeys = typeof Object.keys === "function"
  34. ? (obj) => Object.keys(obj)
  35. : (object) => {
  36. const keys = [];
  37. for (const key in object) {
  38. if (Object.prototype.hasOwnProperty.call(object, key)) {
  39. keys.push(key);
  40. }
  41. }
  42. return keys;
  43. };
  44. util.find = (arr, checker) => {
  45. for (const item of arr) {
  46. if (checker(item))
  47. return item;
  48. }
  49. return undefined;
  50. };
  51. util.isInteger = typeof Number.isInteger === "function"
  52. ? (val) => Number.isInteger(val)
  53. : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
  54. function joinValues(array, separator = " | ") {
  55. return array
  56. .map((val) => (typeof val === "string" ? `'${val}'` : val))
  57. .join(separator);
  58. }
  59. util.joinValues = joinValues;
  60. util.jsonStringifyReplacer = (_, value) => {
  61. if (typeof value === "bigint") {
  62. return value.toString();
  63. }
  64. return value;
  65. };
  66. })(util = exports.util || (exports.util = {}));
  67. var objectUtil;
  68. (function (objectUtil) {
  69. objectUtil.mergeShapes = (first, second) => {
  70. return {
  71. ...first,
  72. ...second,
  73. };
  74. };
  75. })(objectUtil = exports.objectUtil || (exports.objectUtil = {}));
  76. exports.ZodParsedType = util.arrayToEnum([
  77. "string",
  78. "nan",
  79. "number",
  80. "integer",
  81. "float",
  82. "boolean",
  83. "date",
  84. "bigint",
  85. "symbol",
  86. "function",
  87. "undefined",
  88. "null",
  89. "array",
  90. "object",
  91. "unknown",
  92. "promise",
  93. "void",
  94. "never",
  95. "map",
  96. "set",
  97. ]);
  98. const getParsedType = (data) => {
  99. const t = typeof data;
  100. switch (t) {
  101. case "undefined":
  102. return exports.ZodParsedType.undefined;
  103. case "string":
  104. return exports.ZodParsedType.string;
  105. case "number":
  106. return isNaN(data) ? exports.ZodParsedType.nan : exports.ZodParsedType.number;
  107. case "boolean":
  108. return exports.ZodParsedType.boolean;
  109. case "function":
  110. return exports.ZodParsedType.function;
  111. case "bigint":
  112. return exports.ZodParsedType.bigint;
  113. case "symbol":
  114. return exports.ZodParsedType.symbol;
  115. case "object":
  116. if (Array.isArray(data)) {
  117. return exports.ZodParsedType.array;
  118. }
  119. if (data === null) {
  120. return exports.ZodParsedType.null;
  121. }
  122. if (data.then &&
  123. typeof data.then === "function" &&
  124. data.catch &&
  125. typeof data.catch === "function") {
  126. return exports.ZodParsedType.promise;
  127. }
  128. if (typeof Map !== "undefined" && data instanceof Map) {
  129. return exports.ZodParsedType.map;
  130. }
  131. if (typeof Set !== "undefined" && data instanceof Set) {
  132. return exports.ZodParsedType.set;
  133. }
  134. if (typeof Date !== "undefined" && data instanceof Date) {
  135. return exports.ZodParsedType.date;
  136. }
  137. return exports.ZodParsedType.object;
  138. default:
  139. return exports.ZodParsedType.unknown;
  140. }
  141. };
  142. exports.getParsedType = getParsedType;