Skip to content

Commit 2a3243b

Browse files
fixing the validation when array as a property value
1 parent db07953 commit 2a3243b

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

Sprint-2/implement/contains.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
function contains(object, key) {
2+
if (
3+
object === null ||
4+
object === undefined ||
5+
typeof object !== "object" ||
6+
Array.isArray(object)
7+
) {
8+
return false;
9+
}
10+
211
if (Object.keys(object).length === 0) {
312
return false;
413
}
514

6-
for (const checkItem in object) {
7-
let validation = object[checkItem];
8-
if (Array.isArray(validation)) {
15+
for (const item in object) {
16+
if (Array.isArray(object[item])) {
917
return false;
1018
}
1119
}
20+
1221
for (const item in object) {
1322
if (item === key) {
1423
return true;
1524
}
1625
}
26+
1727
return false;
1828
}
1929

Sprint-2/implement/contains.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ test("should return false if the input object do not have non existing key value
5454
test("should return false if the input object do not have non existing key value", () => {
5555
let mixObject = { a: 2, b: "hello", c: [] };
5656
expect(contains(mixObject, "c")).toBe(false);
57+
58+
59+
expect(contains(["A", "B"], "1" )).toBe(false);
60+
expect(contains(null, "1" )).toBe(false);
61+
expect(contains(undefined, "1" )).toBe(false);
62+
expect(contains("ABC", "1" )).toBe(false);
5763
});

0 commit comments

Comments
 (0)