contains-key
![License](https://img.shields.io/github/license/rafamel/utils.svg)
Confirms objects have a key or a set of keys as direct or inherited properties.
Install
npm install contains-key
Usage
As a notable difference with Object.hasOwnProperty
, contains-key
functions will return false
if a property exists but its value is undefined
. As another key difference, some contains-key
's functions will also return true
for inherited properties.
If a kind
is passed, it must be any of the following: 'null'
, 'boolean'
, 'number'
, 'string'
, 'object'
, 'array'
, 'symbol'
, 'function'
.
containsKey(item: any, key: string | string[], kind?: string): boolean
Returns true
if item
is an object where key
's values are not undefined
.
If a kind
is passed, it will only return true
if the specified properties have values of that type.
import { containsKey } from 'contains-key';
const source = { foo: 'foo', bar: undefined };
const inherited = Object.create(source, {});
containsKey(source, 'foo');
containsKey(inherited, 'foo');
containsKey(source, 'bar');
containsKey(inherited, 'bar');
containsKey(source, 'baz');
containsKey(inherited, 'baz');
containsKey(source, ['foo', 'bar', 'baz']);
containsKey(inherited, ['foo', 'bar', 'baz']);
containsKey(source, 'foo', 'string');
containsKey(inherited, 'foo', 'string');
containsKey(source, 'foo', 'number');
containsKey(inherited, 'foo', 'number');
containsOwnKey(item: any, key: string | string[], kind?: string): boolean
Similar to containsKey
, with the difference that it will only return true
for direct properties of an object.
import { containsOwnKey } from 'contains-key';
const source = { foo: 'foo', bar: undefined };
const inherited = Object.create(source, {});
containsOwnKey(source, 'foo');
containsOwnKey(inherited, 'foo');
containsOwnKey(source, 'bar');
containsOwnKey(inherited, 'bar');
containsOwnKey(source, 'baz');
containsOwnKey(inherited, 'baz');
containsOwnKey(source, ['foo', 'bar', 'baz']);
containsOwnKey(inherited, ['foo', 'bar', 'baz']);
containsOwnKey(source, 'foo', 'string');
containsOwnKey(inherited, 'foo', 'string');
containsOwnKey(source, 'foo', 'number');
containsOwnKey(inherited, 'foo', 'number');
containsAnyKey(item: any, keys: string[], kind?: string): boolean
Returns true
if item
is an object where any of the keys
' values are not undefined
.
If a kind
is passed, it will return true
if any of the keys
' values are of that specific type.
import { containsAnyKey } from 'contains-key';
const source = { foo: 'foo', bar: undefined };
const inherited = Object.create(source, {});
containsAnyKey(source, ['foo', 'bar']);
containsAnyKey(inherited, ['foo', 'bar']);
containsAnyKey(source, ['bar', 'baz']);
containsAnyKey(inherited, ['bar', 'baz']);
containsAnyKey(source, ['foo', 'bar', 'baz'], 'string');
containsAnyKey(inherited, ['foo', 'bar', 'baz'], 'string');
containsAnyKey(source, ['foo', 'bar', 'baz'], 'number');
containsAnyKey(inherited, ['foo', 'bar', 'baz'], 'string');
containsAnyOwnKey(item: any, keys: string[], kind?: string): boolean
Similar to containsAnyKey
, with the difference that it will only return true
for direct properties of an object.
import { containsAnyOwnKey } from 'contains-key';
const source = { foo: 'foo', bar: undefined };
const inherited = Object.create(source, {});
containsAnyOwnKey(source, ['foo', 'bar']);
containsAnyOwnKey(inherited, ['foo', 'bar']);
containsAnyOwnKey(source, ['bar', 'baz']);
containsAnyOwnKey(inherited, ['bar', 'baz']);
containsAnyOwnKey(source, ['foo', 'bar', 'baz'], 'string');
containsAnyOwnKey(inherited, ['foo', 'bar', 'baz'], 'string');
containsAnyOwnKey(source, ['foo', 'bar', 'baz'], 'number');
containsAnyOwnKey(inherited, ['foo', 'bar', 'baz'], 'string');