Merge pull request #1032 from 30-seconds/add-hasKey

Add a new snippet, hasKey
This commit is contained in:
Angelos Chalaris
2019-10-15 15:53:12 +03:00
committed by GitHub
2 changed files with 66 additions and 0 deletions

33
snippets/hasKey.md Normal file
View File

@ -0,0 +1,33 @@
---
title: hasKey
tags: object,recursion,intermediate
---
Returns `true` if the target value exists in a JSON object, `false` otherwise.
Check if the key contains `.`, use `String.prototype.split('.')[0]` to get the first part and store as `_key`.
Use `typeof` to check if the contents of `obj[key]` are an `object` and, if so, call `hasKey` with that object and the remainder of the `key`.
Otherwise, use `Object.keys(obj)` in combination with `Array.prototype.includes()` to check if the given `key` exists.
```js
const hasKey = (obj, key) => {
if (key.includes('.')) {
let _key = key.split('.')[0];
if (typeof obj[_key] === 'object')
return hasKey(obj[_key], key.slice(key.indexOf('.') + 1))
}
return Object.keys(obj).includes(key);
}
```
```js
let obj = {
a: 1, b: { c: 4 }, 'd.e': 5
}
hasKey(obj, 'a'); // true
hasKey(obj, 'b'); // true
hasKey(obj, 'b.c'); // true
hasKey(obj, 'd.e'); // true
hasKey(obj, 'd'); // false
hasKey(obj, 'f'); // false
```

33
test/hasKey.test.js Normal file
View File

@ -0,0 +1,33 @@
const {hasKey} = require('./_30s.js');
const data = {
a: 1, b: { c: 4 }, 'd.e': 5
};
test('hasKey is a Function', () => {
expect(hasKey).toBeInstanceOf(Function);
});
test('hasKey returns true for simple property', () => {
expect(hasKey(data, 'a')).toBe(true);
});
test('hasKey returns true for object property', () => {
expect(hasKey(data, 'b')).toBe(true);
});
test('hasKey returns true for nested property', () => {
expect(hasKey(data, 'b.c')).toBe(true);
});
test('hasKey returns true for property with dots', () => {
expect(hasKey(data, 'd.e')).toBe(true);
});
test('hasKey returns false for non-existent property', () => {
expect(hasKey(data, 'f')).toBe(true);
});
test('hasKey returns false for virtual nested property', () => {
expect(hasKey(data, 'd')).toBe(false);
});