-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
23 lines (23 loc) · 819 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"use strict";
/**
* Checks for the existence of specified keys in object
*
* @param {Object} object - object to check
* @param {array|string} keys - delimited string of keys or array of keys
* @return {Object} - key boolean map i.e. { [key]: boolean }
*/
exports.default = module.exports = function keycheck(object, keys) {
if (object.toString() !== "[object Object]") {
throw new TypeError("Invalid JSON object!");
}
if (!(Array.isArray(keys) || typeof keys === "string")) {
throw new TypeError("Keys must be a string or array of strings!");
}
if (typeof keys === "string") {
keys = keys.match(/\w+((-|_)\w+)?/g);
}
return keys.reduce((keyBoolMap, currentKey) => {
keyBoolMap[currentKey] = Object.prototype.hasOwnProperty.call(object, currentKey)
return keyBoolMap;
}, {})
};