Skip to content

Commit

Permalink
fix: make util tree shakeable
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmisar authored and colinhacks committed Apr 24, 2024
1 parent 0a3d764 commit 0712d80
Show file tree
Hide file tree
Showing 70 changed files with 489 additions and 460 deletions.
3 changes: 2 additions & 1 deletion deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TypeOf, ZodType } from "./index.ts";
import { util } from "./helpers/index.ts";
import { Primitive } from "./helpers/typeAliases.ts";
import { util, ZodParsedType } from "./helpers/util.ts";
import { ZodParsedType } from "./helpers/util.ts";

type allKeys<T> = T extends any ? keyof T : never;

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/all-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const Test = z.object({
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/anyunknown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("check any inference", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const minTwo = z.string().array().min(2);
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("type guard", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/branded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("branded types", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/catch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { z } from "../index.ts";
import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";

test("basic catch", () => {
expect(z.string().catch("default").parse(undefined)).toBe("default");
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { z } from "../index.ts";
import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";

test("basic defaults", () => {
expect(z.string().default("default").parse(undefined)).toBe("default");
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("create enum", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/firstparty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("first party switch", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const args1 = z.tuple([z.string()]);
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/generics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("generics", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/instanceof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("instanceof", async () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
import { ZodIssueCode } from "../index.ts";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/nativeEnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

test("nativeEnum test with consts", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const Test = z.object({
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/partials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
import { ZodNullable, ZodOptional } from "../index.ts";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/pickomit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const fish = z.object({
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/primitive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
import { Mocker } from "./Mocker.ts";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const promSchema = z.promise(
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/readonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

enum testEnum {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const booleanRecord = z.record(z.boolean());
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/refine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
import { ZodIssueCode } from "../ZodError.ts";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
import { ZodIssueCode } from "../index.ts";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";

const stringToNumber = z.string().transform((arg) => parseFloat(arg));
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
import { ZodError } from "../ZodError.ts";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/void.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { util } from "../helpers/util.ts";
import { util } from "../helpers/index.ts";
import * as z from "../index.ts";
test("void", () => {
const v = z.void();
Expand Down
28 changes: 13 additions & 15 deletions deno/lib/helpers/enumUtil.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
export namespace enumUtil {
type UnionToIntersectionFn<T> = (
T extends unknown ? (k: () => T) => void : never
) extends (k: infer Intersection) => void
? Intersection
: never;
type UnionToIntersectionFn<T> = (
T extends unknown ? (k: () => T) => void : never
) extends (k: infer Intersection) => void
? Intersection
: never;

type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last
? Last
: never;
type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last
? Last
: never;

type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]
? Tuple
: UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>;
type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]
? Tuple
: UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>;

type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never;
type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never;

export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>;
}
export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>;
12 changes: 5 additions & 7 deletions deno/lib/helpers/errorUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export namespace errorUtil {
export type ErrMessage = string | { message?: string };
export const errToObj = (message?: ErrMessage) =>
typeof message === "string" ? { message } : message || {};
export const toString = (message?: ErrMessage): string | undefined =>
typeof message === "string" ? message : message?.message;
}
export type ErrMessage = string | { message?: string };
export const errToObj = (message?: ErrMessage) =>
typeof message === "string" ? { message } : message || {};
export const toString = (message?: ErrMessage): string | undefined =>
typeof message === "string" ? message : message?.message;
5 changes: 5 additions & 0 deletions deno/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * as enumUtil from "./enumUtil.ts";
export * as errorUtil from "./errorUtil.ts";
export * as objectUtil from "./objectUtil.ts";
export * as partialUtil from "./partialUtil.ts";
export * as util from "./util.ts";
40 changes: 40 additions & 0 deletions deno/lib/helpers/objectUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export type MergeShapes<U, V> = {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

// type optionalKeys<T extends object> = {
// [k in keyof T]: undefined extends T[k] ? k : never;
// }[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

// type alkjsdf = addQuestionMarks<{ a: any }>;

export type addQuestionMarks<
T extends object,
R extends keyof T = requiredKeys<T>
// O extends keyof T = optionalKeys<T>
> = Pick<Required<T>, R> & Partial<T>;
// = { [k in O]?: T[k] } & { [k in R]: T[k] };

export type identity<T> = T;
export type flatten<T> = identity<{ [k in keyof T]: T[k] }>;

export type noNeverKeys<T> = {
[k in keyof T]: [T[k]] extends [never] ? never : k;
}[keyof T];

export type noNever<T> = identity<{
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
}>;

export const mergeShapes = <U, T>(first: U, second: T): T & U => {
return {
...first,
...second, // second overwrites first
};
};

export type extendShape<A, B> = flatten<Omit<A, keyof B> & B>;
Loading

0 comments on commit 0712d80

Please sign in to comment.