Skip to content

Instantly share code, notes, and snippets.

@nishanbajracharya
Created May 14, 2018 04:42
Show Gist options
  • Save nishanbajracharya/e7f8e52ca09c0859270bb488e3e33f35 to your computer and use it in GitHub Desktop.
Save nishanbajracharya/e7f8e52ca09c0859270bb488e3e33f35 to your computer and use it in GitHub Desktop.
/**
* Takes an object and checks if all given keys exist in the object.
*
* @param {Object} object
* @param {String|Array} keys
*
* @returns Boolean
*/
export const hasKeys = (object, keys) => {
if (!(object instanceof Array) && object instanceof Object) {
const keyArray = Array.isArray(keys) ? keys : [keys];
if (!keyArray.length) return true;
for (let key of keyArray) {
if (!object.hasOwnProperty(key)) return false;
}
return true;
}
throw new Error('Expected object for input: ' + JSON.stringify(object));
}
/**
* Takes an object and checks if any given keys exist in the object.
*
* @param {Object} object
* @param {String|Array} keys
*
* @returns Boolean
*/
export const hasAnyKey = (object, keys) => {
if (!(object instanceof Array) && object instanceof Object) {
const keyArray = Array.isArray(keys) ? keys : [keys];
if (!keyArray.length) return true;
for (let key of keyArray) {
if (object.hasOwnProperty(key)) return true;
}
return false;
}
throw new Error('Expected object for input: ' + JSON.stringify(object));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment