Skip to content

Commit 248a1e6

Browse files
committed
implement contains function and tests
1 parent d2c79da commit 248a1e6

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
function contains() {}
1+
function contains(obj, prop) {
2+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false;
3+
return Object.prototype.hasOwnProperty.call(obj, prop);
4+
}
25

36
module.exports = contains;

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ as the object doesn't contains a key of 'c'
2020
// Given an empty object
2121
// When passed to contains
2222
// Then it should return false
23-
test.todo("contains on empty object returns false");
23+
test("contains on empty object returns false", () => {
24+
expect(contains({}, "a")).toEqual(false);
25+
});
2426

25-
// Given an object with properties
26-
// When passed to contains with an existing property name
27-
// Then it should return true
27+
test("returns true when object has the property", () => {
28+
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
29+
});
2830

29-
// Given an object with properties
30-
// When passed to contains with a non-existent property name
31-
// Then it should return false
31+
test("returns false when object does not have the property", () => {
32+
expect(contains({ a: 1, b: 2 }, "c")).toEqual(false);
33+
});
3234

33-
// Given invalid parameters like an array
34-
// When passed to contains
35-
// Then it should return false or throw an error
35+
test("returns false when passed an array", () => {
36+
expect(contains([1, 2, 3], "0")).toEqual(false);
37+
});

0 commit comments

Comments
 (0)