Skip to content

Commit b32fa68

Browse files
committed
add contains.js
1 parent e82bb78 commit b32fa68

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
function contains() {}
1+
function contains(object, property) {
2+
if (typeof object !== "object" || object === null || Array.isArray(object)) {
3+
return false;
4+
}
5+
6+
return Object.hasOwn(object, property);
7+
}
28

39
module.exports = contains;
10+
11+

‎Sprint-2/implement/contains.test.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,26 @@ as the object doesn't contains a key of 'c'
2121
// When passed to contains
2222
// Then it should return false
2323
test.todo("contains on empty object returns false");
24-
24+
test("contains on empty object returns false", () => {
25+
expect(contains({}, "a")).toBe(false);
26+
});
2527
// Given an object with properties
2628
// When passed to contains with an existing property name
2729
// Then it should return true
28-
30+
test("contains returns true when property exists", () => {
31+
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
32+
});
2933
// Given an object with properties
3034
// When passed to contains with a non-existent property name
3135
// Then it should return false
36+
test("contains returns false when property does not exist", () => {
37+
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
38+
});
39+
3240

3341
// Given invalid parameters like an array
3442
// When passed to contains
3543
// Then it should return false or throw an error
44+
test("contains returns false when passed an array", () => {
45+
expect(contains([], "a")).toBe(false);
46+
});

0 commit comments

Comments
 (0)