object-has-keys
v0.2.3
Published
Utility to check if given object has a set of keys.
Downloads
2
Readme
object-has-keys
A JavaScript utility to check if given object has a set of keys.
Getting started
The first step is to add object-has-keys
into your project.
$ npm install --save object-has-keys
Usage
Import the package into your package using require
or import
statements
const { hasKeys, hasAnyKey } = require('object-has-keys'); // CommonJS
import { hasKeys, hasAnyKey } from 'object-has-keys'; // ES2015+
The hasKeys
function takes two arguments, the object
and the keys
. It returns true if all the keys queried exist.
const myObject = {
key1: 'value',
key2: 'value',
key3: 'value'
};
hasKeys(myObject, 'key1'); // true
hasKeys(myObject, ['key1', 'key2']); // true
hasKeys(myObject, 'key5'); // false
hasKeys(myObject, ['key5']); // false
hasKeys(myObject, ['key1', 'key5']); // false
The hasAnyKey
function takes two arguments, the object
and the keys
. It returns true if any key queried exists.
const myObject = {
key1: 'value',
key2: 'value',
key3: 'value'
};
hasAnyKey(myObject, 'key1'); // true
hasAnyKey(myObject, ['key1', 'key5']); // true
hasAnyKey(myObject, 'key5'); // false
hasAnyKey(myObject, ['key6']); // false
Contributing
To contribute, follow one of the two options:
Open an Issue
Open an issue detailing:
- What the issue is
- Steps to reproduce
- Possible solutions
Note: These details are recommended but are entirely optional.
Send a Pull Request
Fork this project and send a pull request to the
master
branch.
License
MIT