New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

contains-key

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contains-key

Confirms objects have a key or a set of keys as direct or inherited properties

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by100%
Maintainers
0
Weekly downloads
 
Created
Source

contains-key

Version Types License

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'); // 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

Keywords

FAQs

Package last updated on 29 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc