object-has-keys

0.2.3 • Public • Published

object-has-keys Build Status

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:

    1. What the issue is
    2. Steps to reproduce
    3. 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

Readme

Keywords

Package Sidebar

Install

npm i object-has-keys

Weekly Downloads

0

Version

0.2.3

License

MIT

Unpacked Size

6.17 kB

Total Files

4

Last publish

Collaborators

  • nisbaj