contains-key
v0.2.2
Published
Confirms objects have a key or a set of keys as direct or inherited properties
Downloads
8
Maintainers
Readme
contains-key
Confirms objects have a key or a set of keys as direct or inherited properties.
Install
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'); // true
containsKey(inherited, 'foo'); // true
containsKey(source, 'bar'); // false;
containsKey(inherited, 'bar'); // false
containsKey(source, 'baz'); // false
containsKey(inherited, 'baz'); // false
containsKey(source, ['foo', 'bar', 'baz']); // false
containsKey(inherited, ['foo', 'bar', 'baz']); // false
containsKey(source, 'foo', 'string'); // true
containsKey(inherited, 'foo', 'string'); // true
containsKey(source, 'foo', 'number'); // false
containsKey(inherited, 'foo', 'number'); // false
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'); // true
containsOwnKey(inherited, 'foo'); // false
containsOwnKey(source, 'bar'); // false;
containsOwnKey(inherited, 'bar'); // false
containsOwnKey(source, 'baz'); // false;
containsOwnKey(inherited, 'baz'); // false;
containsOwnKey(source, ['foo', 'bar', 'baz']); // false
containsOwnKey(inherited, ['foo', 'bar', 'baz']); // false
containsOwnKey(source, 'foo', 'string'); // true
containsOwnKey(inherited, 'foo', 'string'); // false
containsOwnKey(source, 'foo', 'number'); // false
containsOwnKey(inherited, 'foo', 'number'); // false
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']); // true
containsAnyKey(inherited, ['foo', 'bar']); // true
containsAnyKey(source, ['bar', 'baz']); // false
containsAnyKey(inherited, ['bar', 'baz']); // false
containsAnyKey(source, ['foo', 'bar', 'baz'], 'string'); // true
containsAnyKey(inherited, ['foo', 'bar', 'baz'], 'string'); // true
containsAnyKey(source, ['foo', 'bar', 'baz'], 'number'); // false
containsAnyKey(inherited, ['foo', 'bar', 'baz'], 'string'); // false
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']); // true
containsAnyOwnKey(inherited, ['foo', 'bar']); // false
containsAnyOwnKey(source, ['bar', 'baz']); // false
containsAnyOwnKey(inherited, ['bar', 'baz']); // false
containsAnyOwnKey(source, ['foo', 'bar', 'baz'], 'string'); // true
containsAnyOwnKey(inherited, ['foo', 'bar', 'baz'], 'string'); // false
containsAnyOwnKey(source, ['foo', 'bar', 'baz'], 'number'); // false
containsAnyOwnKey(inherited, ['foo', 'bar', 'baz'], 'string'); // false