npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

contains-key

v0.2.2

Published

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

Downloads

14

Readme

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