| // functions | |
| export function assertEqual(val) { | |
| return val; | |
| } | |
| export function assertNotEqual(val) { | |
| return val; | |
| } | |
| export function assertIs(_arg) { } | |
| export function assertNever(_x) { | |
| throw new Error(); | |
| } | |
| export function assert(_) { } | |
| export function getEnumValues(entries) { | |
| const numericValues = Object.values(entries).filter((v) => typeof v === "number"); | |
| const values = Object.entries(entries) | |
| .filter(([k, _]) => numericValues.indexOf(+k) === -1) | |
| .map(([_, v]) => v); | |
| return values; | |
| } | |
| export function joinValues(array, separator = "|") { | |
| return array.map((val) => stringifyPrimitive(val)).join(separator); | |
| } | |
| export function jsonStringifyReplacer(_, value) { | |
| if (typeof value === "bigint") | |
| return value.toString(); | |
| return value; | |
| } | |
| export function cached(getter) { | |
| const set = false; | |
| return { | |
| get value() { | |
| if (!set) { | |
| const value = getter(); | |
| Object.defineProperty(this, "value", { value }); | |
| return value; | |
| } | |
| throw new Error("cached value already set"); | |
| }, | |
| }; | |
| } | |
| export function nullish(input) { | |
| return input === null || input === undefined; | |
| } | |
| export function cleanRegex(source) { | |
| const start = source.startsWith("^") ? 1 : 0; | |
| const end = source.endsWith("$") ? source.length - 1 : source.length; | |
| return source.slice(start, end); | |
| } | |
| export function floatSafeRemainder(val, step) { | |
| const valDecCount = (val.toString().split(".")[1] || "").length; | |
| const stepString = step.toString(); | |
| let stepDecCount = (stepString.split(".")[1] || "").length; | |
| if (stepDecCount === 0 && /\d?e-\d?/.test(stepString)) { | |
| const match = stepString.match(/\d?e-(\d?)/); | |
| if (match?.[1]) { | |
| stepDecCount = Number.parseInt(match[1]); | |
| } | |
| } | |
| const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount; | |
| const valInt = Number.parseInt(val.toFixed(decCount).replace(".", "")); | |
| const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", "")); | |
| return (valInt % stepInt) / 10 ** decCount; | |
| } | |
| const EVALUATING = Symbol("evaluating"); | |
| export function defineLazy(object, key, getter) { | |
| let value = undefined; | |
| Object.defineProperty(object, key, { | |
| get() { | |
| if (value === EVALUATING) { | |
| // Circular reference detected, return undefined to break the cycle | |
| return undefined; | |
| } | |
| if (value === undefined) { | |
| value = EVALUATING; | |
| value = getter(); | |
| } | |
| return value; | |
| }, | |
| set(v) { | |
| Object.defineProperty(object, key, { | |
| value: v, | |
| // configurable: true, | |
| }); | |
| // object[key] = v; | |
| }, | |
| configurable: true, | |
| }); | |
| } | |
| export function objectClone(obj) { | |
| return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj)); | |
| } | |
| export function assignProp(target, prop, value) { | |
| Object.defineProperty(target, prop, { | |
| value, | |
| writable: true, | |
| enumerable: true, | |
| configurable: true, | |
| }); | |
| } | |
| export function mergeDefs(...defs) { | |
| const mergedDescriptors = {}; | |
| for (const def of defs) { | |
| const descriptors = Object.getOwnPropertyDescriptors(def); | |
| Object.assign(mergedDescriptors, descriptors); | |
| } | |
| return Object.defineProperties({}, mergedDescriptors); | |
| } | |
| export function cloneDef(schema) { | |
| return mergeDefs(schema._zod.def); | |
| } | |
| export function getElementAtPath(obj, path) { | |
| if (!path) | |
| return obj; | |
| return path.reduce((acc, key) => acc?.[key], obj); | |
| } | |
| export function promiseAllObject(promisesObj) { | |
| const keys = Object.keys(promisesObj); | |
| const promises = keys.map((key) => promisesObj[key]); | |
| return Promise.all(promises).then((results) => { | |
| const resolvedObj = {}; | |
| for (let i = 0; i < keys.length; i++) { | |
| resolvedObj[keys[i]] = results[i]; | |
| } | |
| return resolvedObj; | |
| }); | |
| } | |
| export function randomString(length = 10) { | |
| const chars = "abcdefghijklmnopqrstuvwxyz"; | |
| let str = ""; | |
| for (let i = 0; i < length; i++) { | |
| str += chars[Math.floor(Math.random() * chars.length)]; | |
| } | |
| return str; | |
| } | |
| export function esc(str) { | |
| return JSON.stringify(str); | |
| } | |
| export const captureStackTrace = ("captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => { }); | |
| export function isObject(data) { | |
| return typeof data === "object" && data !== null && !Array.isArray(data); | |
| } | |
| export const allowsEval = cached(() => { | |
| // @ts-ignore | |
| if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) { | |
| return false; | |
| } | |
| try { | |
| const F = Function; | |
| new F(""); | |
| return true; | |
| } | |
| catch (_) { | |
| return false; | |
| } | |
| }); | |
| export function isPlainObject(o) { | |
| if (isObject(o) === false) | |
| return false; | |
| // modified constructor | |
| const ctor = o.constructor; | |
| if (ctor === undefined) | |
| return true; | |
| // modified prototype | |
| const prot = ctor.prototype; | |
| if (isObject(prot) === false) | |
| return false; | |
| // ctor doesn't have static `isPrototypeOf` | |
| if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| export function shallowClone(o) { | |
| if (isPlainObject(o)) | |
| return { ...o }; | |
| if (Array.isArray(o)) | |
| return [...o]; | |
| return o; | |
| } | |
| export function numKeys(data) { | |
| let keyCount = 0; | |
| for (const key in data) { | |
| if (Object.prototype.hasOwnProperty.call(data, key)) { | |
| keyCount++; | |
| } | |
| } | |
| return keyCount; | |
| } | |
| export const getParsedType = (data) => { | |
| const t = typeof data; | |
| switch (t) { | |
| case "undefined": | |
| return "undefined"; | |
| case "string": | |
| return "string"; | |
| case "number": | |
| return Number.isNaN(data) ? "nan" : "number"; | |
| case "boolean": | |
| return "boolean"; | |
| case "function": | |
| return "function"; | |
| case "bigint": | |
| return "bigint"; | |
| case "symbol": | |
| return "symbol"; | |
| case "object": | |
| if (Array.isArray(data)) { | |
| return "array"; | |
| } | |
| if (data === null) { | |
| return "null"; | |
| } | |
| if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") { | |
| return "promise"; | |
| } | |
| if (typeof Map !== "undefined" && data instanceof Map) { | |
| return "map"; | |
| } | |
| if (typeof Set !== "undefined" && data instanceof Set) { | |
| return "set"; | |
| } | |
| if (typeof Date !== "undefined" && data instanceof Date) { | |
| return "date"; | |
| } | |
| // @ts-ignore | |
| if (typeof File !== "undefined" && data instanceof File) { | |
| return "file"; | |
| } | |
| return "object"; | |
| default: | |
| throw new Error(`Unknown data type: ${t}`); | |
| } | |
| }; | |
| export const propertyKeyTypes = new Set(["string", "number", "symbol"]); | |
| export const primitiveTypes = new Set(["string", "number", "bigint", "boolean", "symbol", "undefined"]); | |
| export function escapeRegex(str) { | |
| return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| } | |
| // zod-specific utils | |
| export function clone(inst, def, params) { | |
| const cl = new inst._zod.constr(def ?? inst._zod.def); | |
| if (!def || params?.parent) | |
| cl._zod.parent = inst; | |
| return cl; | |
| } | |
| export function normalizeParams(_params) { | |
| const params = _params; | |
| if (!params) | |
| return {}; | |
| if (typeof params === "string") | |
| return { error: () => params }; | |
| if (params?.message !== undefined) { | |
| if (params?.error !== undefined) | |
| throw new Error("Cannot specify both `message` and `error` params"); | |
| params.error = params.message; | |
| } | |
| delete params.message; | |
| if (typeof params.error === "string") | |
| return { ...params, error: () => params.error }; | |
| return params; | |
| } | |
| export function createTransparentProxy(getter) { | |
| let target; | |
| return new Proxy({}, { | |
| get(_, prop, receiver) { | |
| target ?? (target = getter()); | |
| return Reflect.get(target, prop, receiver); | |
| }, | |
| set(_, prop, value, receiver) { | |
| target ?? (target = getter()); | |
| return Reflect.set(target, prop, value, receiver); | |
| }, | |
| has(_, prop) { | |
| target ?? (target = getter()); | |
| return Reflect.has(target, prop); | |
| }, | |
| deleteProperty(_, prop) { | |
| target ?? (target = getter()); | |
| return Reflect.deleteProperty(target, prop); | |
| }, | |
| ownKeys(_) { | |
| target ?? (target = getter()); | |
| return Reflect.ownKeys(target); | |
| }, | |
| getOwnPropertyDescriptor(_, prop) { | |
| target ?? (target = getter()); | |
| return Reflect.getOwnPropertyDescriptor(target, prop); | |
| }, | |
| defineProperty(_, prop, descriptor) { | |
| target ?? (target = getter()); | |
| return Reflect.defineProperty(target, prop, descriptor); | |
| }, | |
| }); | |
| } | |
| export function stringifyPrimitive(value) { | |
| if (typeof value === "bigint") | |
| return value.toString() + "n"; | |
| if (typeof value === "string") | |
| return `"${value}"`; | |
| return `${value}`; | |
| } | |
| export function optionalKeys(shape) { | |
| return Object.keys(shape).filter((k) => { | |
| return shape[k]._zod.optin === "optional" && shape[k]._zod.optout === "optional"; | |
| }); | |
| } | |
| export const NUMBER_FORMAT_RANGES = { | |
| safeint: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER], | |
| int32: [-2147483648, 2147483647], | |
| uint32: [0, 4294967295], | |
| float32: [-3.4028234663852886e38, 3.4028234663852886e38], | |
| float64: [-Number.MAX_VALUE, Number.MAX_VALUE], | |
| }; | |
| export const BIGINT_FORMAT_RANGES = { | |
| int64: [/* @__PURE__*/ BigInt("-9223372036854775808"), /* @__PURE__*/ BigInt("9223372036854775807")], | |
| uint64: [/* @__PURE__*/ BigInt(0), /* @__PURE__*/ BigInt("18446744073709551615")], | |
| }; | |
| export function pick(schema, mask) { | |
| const currDef = schema._zod.def; | |
| const def = mergeDefs(schema._zod.def, { | |
| get shape() { | |
| const newShape = {}; | |
| for (const key in mask) { | |
| if (!(key in currDef.shape)) { | |
| throw new Error(`Unrecognized key: "${key}"`); | |
| } | |
| if (!mask[key]) | |
| continue; | |
| newShape[key] = currDef.shape[key]; | |
| } | |
| assignProp(this, "shape", newShape); // self-caching | |
| return newShape; | |
| }, | |
| checks: [], | |
| }); | |
| return clone(schema, def); | |
| } | |
| export function omit(schema, mask) { | |
| const currDef = schema._zod.def; | |
| const def = mergeDefs(schema._zod.def, { | |
| get shape() { | |
| const newShape = { ...schema._zod.def.shape }; | |
| for (const key in mask) { | |
| if (!(key in currDef.shape)) { | |
| throw new Error(`Unrecognized key: "${key}"`); | |
| } | |
| if (!mask[key]) | |
| continue; | |
| delete newShape[key]; | |
| } | |
| assignProp(this, "shape", newShape); // self-caching | |
| return newShape; | |
| }, | |
| checks: [], | |
| }); | |
| return clone(schema, def); | |
| } | |
| export function extend(schema, shape) { | |
| if (!isPlainObject(shape)) { | |
| throw new Error("Invalid input to extend: expected a plain object"); | |
| } | |
| const checks = schema._zod.def.checks; | |
| const hasChecks = checks && checks.length > 0; | |
| if (hasChecks) { | |
| throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead."); | |
| } | |
| const def = mergeDefs(schema._zod.def, { | |
| get shape() { | |
| const _shape = { ...schema._zod.def.shape, ...shape }; | |
| assignProp(this, "shape", _shape); // self-caching | |
| return _shape; | |
| }, | |
| checks: [], | |
| }); | |
| return clone(schema, def); | |
| } | |
| export function safeExtend(schema, shape) { | |
| if (!isPlainObject(shape)) { | |
| throw new Error("Invalid input to safeExtend: expected a plain object"); | |
| } | |
| const def = { | |
| ...schema._zod.def, | |
| get shape() { | |
| const _shape = { ...schema._zod.def.shape, ...shape }; | |
| assignProp(this, "shape", _shape); // self-caching | |
| return _shape; | |
| }, | |
| checks: schema._zod.def.checks, | |
| }; | |
| return clone(schema, def); | |
| } | |
| export function merge(a, b) { | |
| const def = mergeDefs(a._zod.def, { | |
| get shape() { | |
| const _shape = { ...a._zod.def.shape, ...b._zod.def.shape }; | |
| assignProp(this, "shape", _shape); // self-caching | |
| return _shape; | |
| }, | |
| get catchall() { | |
| return b._zod.def.catchall; | |
| }, | |
| checks: [], // delete existing checks | |
| }); | |
| return clone(a, def); | |
| } | |
| export function partial(Class, schema, mask) { | |
| const def = mergeDefs(schema._zod.def, { | |
| get shape() { | |
| const oldShape = schema._zod.def.shape; | |
| const shape = { ...oldShape }; | |
| if (mask) { | |
| for (const key in mask) { | |
| if (!(key in oldShape)) { | |
| throw new Error(`Unrecognized key: "${key}"`); | |
| } | |
| if (!mask[key]) | |
| continue; | |
| // if (oldShape[key]!._zod.optin === "optional") continue; | |
| shape[key] = Class | |
| ? new Class({ | |
| type: "optional", | |
| innerType: oldShape[key], | |
| }) | |
| : oldShape[key]; | |
| } | |
| } | |
| else { | |
| for (const key in oldShape) { | |
| // if (oldShape[key]!._zod.optin === "optional") continue; | |
| shape[key] = Class | |
| ? new Class({ | |
| type: "optional", | |
| innerType: oldShape[key], | |
| }) | |
| : oldShape[key]; | |
| } | |
| } | |
| assignProp(this, "shape", shape); // self-caching | |
| return shape; | |
| }, | |
| checks: [], | |
| }); | |
| return clone(schema, def); | |
| } | |
| export function required(Class, schema, mask) { | |
| const def = mergeDefs(schema._zod.def, { | |
| get shape() { | |
| const oldShape = schema._zod.def.shape; | |
| const shape = { ...oldShape }; | |
| if (mask) { | |
| for (const key in mask) { | |
| if (!(key in shape)) { | |
| throw new Error(`Unrecognized key: "${key}"`); | |
| } | |
| if (!mask[key]) | |
| continue; | |
| // overwrite with non-optional | |
| shape[key] = new Class({ | |
| type: "nonoptional", | |
| innerType: oldShape[key], | |
| }); | |
| } | |
| } | |
| else { | |
| for (const key in oldShape) { | |
| // overwrite with non-optional | |
| shape[key] = new Class({ | |
| type: "nonoptional", | |
| innerType: oldShape[key], | |
| }); | |
| } | |
| } | |
| assignProp(this, "shape", shape); // self-caching | |
| return shape; | |
| }, | |
| checks: [], | |
| }); | |
| return clone(schema, def); | |
| } | |
| // invalid_type | too_big | too_small | invalid_format | not_multiple_of | unrecognized_keys | invalid_union | invalid_key | invalid_element | invalid_value | custom | |
| export function aborted(x, startIndex = 0) { | |
| if (x.aborted === true) | |
| return true; | |
| for (let i = startIndex; i < x.issues.length; i++) { | |
| if (x.issues[i]?.continue !== true) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| export function prefixIssues(path, issues) { | |
| return issues.map((iss) => { | |
| var _a; | |
| (_a = iss).path ?? (_a.path = []); | |
| iss.path.unshift(path); | |
| return iss; | |
| }); | |
| } | |
| export function unwrapMessage(message) { | |
| return typeof message === "string" ? message : message?.message; | |
| } | |
| export function finalizeIssue(iss, ctx, config) { | |
| const full = { ...iss, path: iss.path ?? [] }; | |
| // for backwards compatibility | |
| if (!iss.message) { | |
| const message = unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? | |
| unwrapMessage(ctx?.error?.(iss)) ?? | |
| unwrapMessage(config.customError?.(iss)) ?? | |
| unwrapMessage(config.localeError?.(iss)) ?? | |
| "Invalid input"; | |
| full.message = message; | |
| } | |
| // delete (full as any).def; | |
| delete full.inst; | |
| delete full.continue; | |
| if (!ctx?.reportInput) { | |
| delete full.input; | |
| } | |
| return full; | |
| } | |
| export function getSizableOrigin(input) { | |
| if (input instanceof Set) | |
| return "set"; | |
| if (input instanceof Map) | |
| return "map"; | |
| // @ts-ignore | |
| if (input instanceof File) | |
| return "file"; | |
| return "unknown"; | |
| } | |
| export function getLengthableOrigin(input) { | |
| if (Array.isArray(input)) | |
| return "array"; | |
| if (typeof input === "string") | |
| return "string"; | |
| return "unknown"; | |
| } | |
| export function issue(...args) { | |
| const [iss, input, inst] = args; | |
| if (typeof iss === "string") { | |
| return { | |
| message: iss, | |
| code: "custom", | |
| input, | |
| inst, | |
| }; | |
| } | |
| return { ...iss }; | |
| } | |
| export function cleanEnum(obj) { | |
| return Object.entries(obj) | |
| .filter(([k, _]) => { | |
| // return true if NaN, meaning it's not a number, thus a string key | |
| return Number.isNaN(Number.parseInt(k, 10)); | |
| }) | |
| .map((el) => el[1]); | |
| } | |
| // Codec utility functions | |
| export function base64ToUint8Array(base64) { | |
| const binaryString = atob(base64); | |
| const bytes = new Uint8Array(binaryString.length); | |
| for (let i = 0; i < binaryString.length; i++) { | |
| bytes[i] = binaryString.charCodeAt(i); | |
| } | |
| return bytes; | |
| } | |
| export function uint8ArrayToBase64(bytes) { | |
| let binaryString = ""; | |
| for (let i = 0; i < bytes.length; i++) { | |
| binaryString += String.fromCharCode(bytes[i]); | |
| } | |
| return btoa(binaryString); | |
| } | |
| export function base64urlToUint8Array(base64url) { | |
| const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/"); | |
| const padding = "=".repeat((4 - (base64.length % 4)) % 4); | |
| return base64ToUint8Array(base64 + padding); | |
| } | |
| export function uint8ArrayToBase64url(bytes) { | |
| return uint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); | |
| } | |
| export function hexToUint8Array(hex) { | |
| const cleanHex = hex.replace(/^0x/, ""); | |
| if (cleanHex.length % 2 !== 0) { | |
| throw new Error("Invalid hex string length"); | |
| } | |
| const bytes = new Uint8Array(cleanHex.length / 2); | |
| for (let i = 0; i < cleanHex.length; i += 2) { | |
| bytes[i / 2] = Number.parseInt(cleanHex.slice(i, i + 2), 16); | |
| } | |
| return bytes; | |
| } | |
| export function uint8ArrayToHex(bytes) { | |
| return Array.from(bytes) | |
| .map((b) => b.toString(16).padStart(2, "0")) | |
| .join(""); | |
| } | |
| // instanceof | |
| export class Class { | |
| constructor(..._args) { } | |
| } | |
Xet Storage Details
- Size:
- 19.5 kB
- Xet hash:
- dde58cfa5aa60b30e2ee27239776d7f9ce04cb9880ea68bdbb5ce37d0a2cc9df
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.