Skip to content

Commit d3e14ce

Browse files
committed
strengthen array validation and update contains logic to reject arrays.
1 parent 106cf86 commit d3e14ce

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Sprint-2/implement/contains.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
function contains(obj, key) {
2+
if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
3+
return false;
4+
}
5+
26
return Object.hasOwn(obj, key);
37
}
48

Sprint-2/implement/contains.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ test("contains on object with non-existent property returns false", () => {
5050
// Given invalid parameters like an array
5151
// When passed to contains
5252
// Then it should return false or throw an error
53-
test("contains on invalid parameters returns false", () => {
54-
expect(contains([], "a")).toBe(false);
55-
});
53+
54+
test("contains on invalid parameters returns false", () => {
55+
// We use an index that EXISTS in the array (0)
56+
// If the function returns false, we know it's because it rejected the ARRAY type
57+
expect(contains(["test"], 0)).toBe(false);
58+
expect(contains(null, "a")).toBe(false);
59+
});

0 commit comments

Comments
 (0)