Skip to content

Commit f1c49f5

Browse files
authored
Merge pull request #93 from Milly/no-prototype
fix[isObjectOf]: no discover prototype properties
2 parents 7d8bcbd + 0ec8074 commit f1c49f5

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
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: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ Deno.test("isObjectOf<T>", async (t) => {
3131
assertEquals(
3232
isObjectOf(predObj)({ a: 0, b: "a", c: true, d: "ignored" }),
3333
true,
34+
"Undefined properties are ignored",
3435
);
3536
assertEquals(
3637
isObjectOf(predObj)(
3738
Object.assign(() => void 0, { a: 0, b: "a", c: true }),
3839
),
3940
true,
41+
"Function are treated as an object",
4042
);
4143
});
4244

@@ -64,7 +66,7 @@ Deno.test("isObjectOf<T>", async (t) => {
6466
const predObj = {
6567
getFullYear: is.Function,
6668
};
67-
assertEquals(isObjectOf(predObj)(date), true, "Value is not an object");
69+
assertEquals(isObjectOf(predObj)(date), true);
6870
});
6971

7072
await t.step("predicated type is correct", () => {
@@ -98,4 +100,56 @@ Deno.test("isObjectOf<T>", async (t) => {
98100
>(true);
99101
}
100102
});
103+
104+
await t.step(
105+
"does not affect prototype properties of 'predObj'",
106+
async (t) => {
107+
const prototypeObj = {
108+
a: is.Number,
109+
b: is.Boolean,
110+
};
111+
// deno-lint-ignore ban-types
112+
const predObj = Object.assign(Object.create(prototypeObj) as {}, {
113+
c: is.String,
114+
});
115+
116+
await t.step("returns true on T object", () => {
117+
assertEquals(isObjectOf(predObj)({ c: "a" }), true);
118+
assertEquals(
119+
isObjectOf(predObj)({ a: "ignored", b: "ignored", c: "a" }),
120+
true,
121+
"Predicates defined in the prototype are ignored",
122+
);
123+
assertEquals(
124+
isObjectOf(predObj)({ c: "a", d: "ignored" }),
125+
true,
126+
"Undefined properties are ignored",
127+
);
128+
assertEquals(
129+
isObjectOf(predObj)(Object.assign(() => void 0, { c: "a" })),
130+
true,
131+
"Function are treated as an object",
132+
);
133+
});
134+
135+
await t.step("returns false on non T object", () => {
136+
assertEquals(isObjectOf(predObj)("a"), false, "Value is not an object");
137+
assertEquals(
138+
isObjectOf(predObj)({ a: 0, b: true, c: 1 }),
139+
false,
140+
"Object have a different type property",
141+
);
142+
assertEquals(
143+
isObjectOf(predObj)({ a: 0, b: true }),
144+
false,
145+
"Object does not have one property",
146+
);
147+
assertEquals(
148+
isObjectOf({ 0: is.String })(["a"]),
149+
false,
150+
"Value is not an object",
151+
);
152+
});
153+
},
154+
);
101155
});

0 commit comments

Comments
 (0)