Skip to content

Commit

Permalink
Merge pull request #75 from Milly/function-object
Browse files Browse the repository at this point in the history
`isObjectOf` allows function object
  • Loading branch information
lambdalisue authored Apr 14, 2024
2 parents b89d255 + 51091e2 commit 38607b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export function isOptionalOf<T>(
}
return Object.defineProperties(
setPredicateFactoryMetadata(
(x: unknown): x is Predicate<T | undefined> => x === undefined || pred(x),
(x: unknown): x is T | undefined => x === undefined || pred(x),
{ name: "isOptionalOf", args: [pred] },
),
{ optional: { value: true as const } },
Expand Down Expand Up @@ -1157,7 +1157,11 @@ export function isObjectOf<
}
return setPredicateFactoryMetadata(
(x: unknown): x is ObjectOf<T> => {
if (x == null || typeof x !== "object" || Array.isArray(x)) return false;
if (
x == null ||
typeof x !== "object" && typeof x !== "function" ||
Array.isArray(x)
) return false;
// Check each values
for (const k in predObj) {
if (!predObj[k]((x as T)[k])) return false;
Expand Down
7 changes: 7 additions & 0 deletions is_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,13 @@ Deno.test("isObjectOf<T>", async (t) => {
true,
"Object have an unknown property",
);
assertEquals(
isObjectOf(predObj)(
Object.assign(() => void 0, { a: 0, b: "a", c: true }),
),
true,
"Function object",
);
});
await t.step("returns false on non T object", () => {
const predObj = {
Expand Down

0 comments on commit 38607b0

Please sign in to comment.