Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Feb 24, 2024
1 parent 096d3ed commit b5e5765
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 69 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ isSameType(z.string(), z.string()); // true
isSameType(z.string(), z.number()); // false
isSameType(
z.object({ name: z.string(), other: z.number() }),
z.object({ name: z.string() })
z.object({ name: z.string() }),
);
// false

isCompatibleType(
z.object({ name: z.string(), other: z.number() }),
z.object({ name: z.string() })
z.object({ name: z.string() }),
);
// true
```
Expand All @@ -47,7 +47,7 @@ isSameType(
}
// return other values to use the default comparison
},
}
},
);
// true
```
Expand Down Expand Up @@ -75,7 +75,7 @@ function isSameType(
b: ZodType,
options?: {
interceptor?: (a: ZodType, b: ZodType) => boolean | undefined;
}
},
): boolean;
```

Expand All @@ -87,7 +87,7 @@ function isCompatibleType(
b: ZodType,
options?: {
interceptor?: (a: ZodType, b: ZodType) => boolean | undefined;
}
},
): boolean;
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"engines": {
"node": ">=20"
},
"prettier": {},
"publishConfig": {
"access": "public",
"exports": {
Expand Down
53 changes: 28 additions & 25 deletions src/__tests__/is-compatible-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe("isCompatibleType", () => {
expect(isCompatibleType(z.string(), z.number())).toBe(false);
expect(isCompatibleType(z.string().optional(), z.string())).toBe(false);
expect(isCompatibleType(z.string().optional(), z.string().optional())).toBe(
true
true,
);
expect(isCompatibleType(z.string().nullable(), z.string().optional())).toBe(
false
false,
);
expect(isCompatibleType(z.string(), z.string().nullable())).toBe(true);
expect(isCompatibleType(z.string(), z.string().optional())).toBe(true);
Expand All @@ -32,8 +32,8 @@ describe("isCompatibleType", () => {
}),
z.object({
name: z.string(),
})
)
}),
),
).toBe(true);
expect(
isCompatibleType(
Expand All @@ -42,8 +42,8 @@ describe("isCompatibleType", () => {
}),
z.object({
name: z.number().nullable(),
})
)
}),
),
).toBe(true);
expect(
isCompatibleType(
Expand All @@ -54,8 +54,8 @@ describe("isCompatibleType", () => {
.partial(),
z.object({
name: z.string(),
})
)
}),
),
).toBe(false);
expect(
isCompatibleType(
Expand All @@ -66,8 +66,8 @@ describe("isCompatibleType", () => {
.object({
name: z.string(),
})
.partial()
)
.partial(),
),
).toBe(true);
expect(
isCompatibleType(
Expand All @@ -76,58 +76,61 @@ describe("isCompatibleType", () => {
}),
z.object({
name: z.string().optional(),
})
)
}),
),
).toBe(false);
expect(
isCompatibleType(
z.object({}),
z.object({
name: z.string(),
})
)
}),
),
).toBe(false);
});

test("should compare rest tuple", () => {
expect(
isCompatibleType(z.tuple([z.string(), z.string()]), z.tuple([z.string()]))
isCompatibleType(
z.tuple([z.string(), z.string()]),
z.tuple([z.string()]),
),
).toBe(true);
expect(
isCompatibleType(
z.tuple([z.string(), z.string()]),
z.tuple([z.string(), z.number()])
)
z.tuple([z.string(), z.number()]),
),
).toBe(false);
expect(
isCompatibleType(
z.tuple([z.string(), z.string()]).rest(z.number()),
z.tuple([z.string(), z.string()])
)
z.tuple([z.string(), z.string()]),
),
).toBe(true);
expect(
isCompatibleType(
z.tuple([z.string()]),
z.tuple([z.string()]).rest(z.number())
)
z.tuple([z.string()]).rest(z.number()),
),
).toBe(false);

expect(
isCompatibleType(z.tuple([]).rest(z.number()), z.array(z.number()))
isCompatibleType(z.tuple([]).rest(z.number()), z.array(z.number())),
).toBe(false);
});

test("should compare `or` type", () => {
expect(isCompatibleType(z.string(), z.string().or(z.number()))).toBe(true);
expect(
isCompatibleType(z.string(), z.number().or(z.string()).or(z.boolean()))
isCompatibleType(z.string(), z.number().or(z.string()).or(z.boolean())),
).toBe(true);
expect(isCompatibleType(z.string().or(z.number()), z.string())).toBe(false);
expect(
isCompatibleType(
z.string().or(z.number()),
z.number().or(z.string()).or(z.boolean())
)
z.number().or(z.string()).or(z.boolean()),
),
).toBe(true);
});
});
62 changes: 31 additions & 31 deletions src/__tests__/is-same-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ describe("isSameType", () => {
expect(isSameType(z.string().optional(), z.string())).toBe(false);
expect(isSameType(z.string().optional(), z.string().optional())).toBe(true);
expect(isSameType(z.string().nullable(), z.string().optional())).toBe(
false
false,
);
});

test("should return false when compare branded type", () => {
expect(isSameType(z.string().brand("test"), z.string())).toBe(false);
expect(isSameType(z.string().brand("test"), z.string().brand("test"))).toBe(
false
false,
);
// expect(() =>
// isSameType(z.string().brand("test"), z.string().brand("test"))
Expand All @@ -37,8 +37,8 @@ describe("isSameType", () => {
}),
z.object({
name: z.string(),
})
)
}),
),
).toBe(true);
expect(
isSameType(
Expand All @@ -47,8 +47,8 @@ describe("isSameType", () => {
}),
z.object({
name: z.number(),
})
)
}),
),
).toBe(false);
expect(
isSameType(
Expand All @@ -59,8 +59,8 @@ describe("isSameType", () => {
.partial(),
z.object({
name: z.string(),
})
)
}),
),
).toBe(false);
expect(
isSameType(
Expand All @@ -73,8 +73,8 @@ describe("isSameType", () => {
.object({
name: z.string(),
})
.partial()
)
.partial(),
),
).toBe(true);
expect(
isSameType(
Expand All @@ -83,75 +83,75 @@ describe("isSameType", () => {
}),
z.object({
name: z.string().optional(),
})
)
}),
),
).toBe(false);
expect(
isSameType(
z.object({}),
z.object({
name: z.string(),
})
)
}),
),
).toBe(false);
});

test("should compare rest tuple", () => {
expect(
isSameType(
z.tuple([z.string(), z.string()]),
z.tuple([z.string(), z.string()])
)
z.tuple([z.string(), z.string()]),
),
).toBe(true);
expect(
isSameType(
z.tuple([z.string(), z.string()]),
z.tuple([z.string(), z.number()])
)
z.tuple([z.string(), z.number()]),
),
).toBe(false);
expect(
isSameType(
z.tuple([z.string(), z.string()]).rest(z.number()),
z.tuple([z.string(), z.string()])
)
z.tuple([z.string(), z.string()]),
),
).toBe(false);
expect(
isSameType(
z.tuple([z.string(), z.string()]).rest(z.number()),
z.tuple([z.string(), z.string()]).rest(z.number())
)
z.tuple([z.string(), z.string()]).rest(z.number()),
),
).toBe(true);

expect(isSameType(z.tuple([]).rest(z.number()), z.array(z.number()))).toBe(
false
false,
);
});

test("should compare `and` type", () => {
expect(
isSameType(z.string().and(z.number()), z.string().and(z.number()))
isSameType(z.string().and(z.number()), z.string().and(z.number())),
).toBe(true);
expect(
isSameType(z.number().and(z.string()), z.number().and(z.string()))
isSameType(z.number().and(z.string()), z.number().and(z.string())),
).toBe(true);

// order matters
expect(
isSameType(z.number().and(z.string()), z.string().and(z.number()))
isSameType(z.number().and(z.string()), z.string().and(z.number())),
).toBe(false);
});

test("should compare `or` type", () => {
expect(
isSameType(z.string().or(z.number()), z.string().or(z.number()))
isSameType(z.string().or(z.number()), z.string().or(z.number())),
).toBe(true);
expect(
isSameType(z.number().or(z.string()), z.number().or(z.string()))
isSameType(z.number().or(z.string()), z.number().or(z.string())),
).toBe(true);

// order matters
expect(
isSameType(z.number().or(z.string()), z.string().or(z.number()))
isSameType(z.number().or(z.string()), z.string().or(z.number())),
).toBe(false);
});

Expand All @@ -166,8 +166,8 @@ describe("isSameType", () => {
z.object({ foo: z.string().readonly() }),
z.object({
foo: z.string().readonly(),
})
)
}),
),
).toBe(true);
expect(isSameType(z.string().readonly(), z.string())).toBe(false);
});
Expand Down
Loading

0 comments on commit b5e5765

Please sign in to comment.