Created
May 14, 2018 04:42
-
-
Save nishanbajracharya/e7f8e52ca09c0859270bb488e3e33f35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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