Skip to content

fix[isObjectOf]: no discover prototype properties #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions is/object_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ export function isObjectOf<
Array.isArray(x)
) return false;
// Check each values
for (const k in predObj) {
if (!predObj[k]((x as T)[k])) return false;
}
return true;
return Object.keys(predObj).every((k) => predObj[k]((x as T)[k]));
},
"isObjectOf",
predObj,
Expand Down
44 changes: 43 additions & 1 deletion is/object_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Deno.test("isObjectOf<T>", async (t) => {
const predObj = {
getFullYear: is.Function,
};
assertEquals(isObjectOf(predObj)(date), true, "Value is not an object");
assertEquals(isObjectOf(predObj)(date), true);
});

await t.step("predicated type is correct", () => {
Expand Down Expand Up @@ -98,4 +98,46 @@ Deno.test("isObjectOf<T>", async (t) => {
>(true);
}
});

await t.step("if 'predObj' has prototype properties", async (t) => {
const prototypeObj = {
a: is.Number,
b: is.Boolean,
};
// deno-lint-ignore ban-types
const predObj2 = Object.assign(Object.create(prototypeObj) as {}, {
c: is.String,
});

await t.step("returns true on T object that omits prototype", () => {
assertEquals(isObjectOf(predObj2)({ c: "a" }), true);
assertEquals(
isObjectOf(predObj2)({ c: "a", d: "ignored" }),
true,
);
assertEquals(
isObjectOf(predObj2)(Object.assign(() => void 0, { c: "a" })),
true,
);
});

await t.step("returns false on non T object that omits prototype", () => {
assertEquals(isObjectOf(predObj2)("a"), false, "Value is not an object");
assertEquals(
isObjectOf(predObj2)({ a: 0, b: true, c: 1 }),
false,
"Object have a different type property",
);
assertEquals(
isObjectOf(predObj2)({ a: 0, b: true }),
false,
"Object does not have one property",
);
assertEquals(
isObjectOf({ 0: is.String })(["a"]),
false,
"Value is not an object",
);
});
});
});