Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nishanbajracharya/object-has-keys
A javascript utility to check if given object has a set of keys.
https://github.com/nishanbajracharya/object-has-keys
javascript
Last synced: about 12 hours ago
JSON representation
A javascript utility to check if given object has a set of keys.
- Host: GitHub
- URL: https://github.com/nishanbajracharya/object-has-keys
- Owner: nishanbajracharya
- License: mit
- Created: 2018-05-13T13:59:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-24T14:41:57.000Z (over 1 year ago)
- Last Synced: 2024-10-13T06:54:22.711Z (about 1 month ago)
- Topics: javascript
- Language: JavaScript
- Size: 75.2 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# object-has-keys [![Build Status](https://travis-ci.org/nishanbajracharya/object-has-keys.svg?branch=master)](https://travis-ci.org/nishanbajracharya/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.```sh
$ npm install --save object-has-keys
```## Usage
Import the package into your package using `require` or `import` statements
```js
const { hasKeys, hasAnyKey } = require('object-has-keys'); // CommonJS
```
```js
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.
```js
const myObject = {
key1: 'value',
key2: 'value',
key3: 'value'
};hasKeys(myObject, 'key1'); // true
hasKeys(myObject, ['key1', 'key2']); // truehasKeys(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.
```js
const myObject = {
key1: 'value',
key2: 'value',
key3: 'value'
};hasAnyKey(myObject, 'key1'); // true
hasAnyKey(myObject, ['key1', 'key5']); // truehasAnyKey(myObject, 'key5'); // false
hasAnyKey(myObject, ['key6']); // false
```## Contributing
To contribute, follow one of the two options:- **Open an Issue**
Open an issue detailing:
1. What the issue is
2. Steps to reproduce
3. Possible solutionsNote: 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