Skip to content

Commit e01ab01

Browse files
committed
fix[isObjectOf]: no discover prototype properties
Fixes #92
1 parent da59127 commit e01ab01

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

is/object_of.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ export function isObjectOf<
4848
Array.isArray(x)
4949
) return false;
5050
// Check each values
51-
for (const k in predObj) {
52-
if (!predObj[k]((x as T)[k])) return false;
53-
}
54-
return true;
51+
return Object.keys(predObj).every((k) => predObj[k]((x as T)[k]));
5552
},
5653
"isObjectOf",
5754
predObj,

is/object_of_test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,46 @@ Deno.test("isObjectOf<T>", async (t) => {
9898
>(true);
9999
}
100100
});
101+
102+
await t.step("if 'predObj' has prototype properties", async (t) => {
103+
const prototypeObj = {
104+
a: is.Number,
105+
b: is.Boolean,
106+
};
107+
// deno-lint-ignore ban-types
108+
const predObj2 = Object.assign(Object.create(prototypeObj) as {}, {
109+
c: is.String,
110+
});
111+
112+
await t.step("returns true on T object that omits prototype", () => {
113+
assertEquals(isObjectOf(predObj2)({ c: "a" }), true);
114+
assertEquals(
115+
isObjectOf(predObj2)({ c: "a", d: "ignored" }),
116+
true,
117+
);
118+
assertEquals(
119+
isObjectOf(predObj2)(Object.assign(() => void 0, { c: "a" })),
120+
true,
121+
);
122+
});
123+
124+
await t.step("returns false on non T object that omits prototype", () => {
125+
assertEquals(isObjectOf(predObj2)("a"), false, "Value is not an object");
126+
assertEquals(
127+
isObjectOf(predObj2)({ a: 0, b: true, c: 1 }),
128+
false,
129+
"Object have a different type property",
130+
);
131+
assertEquals(
132+
isObjectOf(predObj2)({ a: 0, b: true }),
133+
false,
134+
"Object does not have one property",
135+
);
136+
assertEquals(
137+
isObjectOf({ 0: is.String })(["a"]),
138+
false,
139+
"Value is not an object",
140+
);
141+
});
142+
});
101143
});

0 commit comments

Comments
 (0)