Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

check-object-key

Package Overview
Dependencies
Maintainers
3
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

check-object-key

Checking a thing

  • 0.1.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

Build Status Dependencies Coverage Status

Check object key

Standardized way to check an objects key, setting defaults, timing out etc.

Installation

npm i check-object-key

Usage

Load up library like so:

'use strict';

const	checkKey	= require('check-object-key');

Basic usage, check if if key exists

const	obj	= {'foo': 'bar'};

checkKey({
	'obj':	obj,
	'objectKey':	'foo'
}, function (err) {
	if (err) throw err;

	// If no error, key exists
});

Basic usage, set default value if key does not exist

const	obj	= {};

checkKey({
	'obj':	obj,
	'objectKey':	'foo',
	'default':	'blut'
}, function (err, warning) {
	if (err) throw err;

	console.log(obj.foo); // "blut"
	console.log(warning); // obj["foo"] is not set, setting default: "blut"
});

Basic usage, check key against list ov validValues

const	obj	= {'foo': 'nein'};

checkKey({
	'obj':	obj,
	'objectKey':	'foo',
	'validValues':	['ja', 'yes', 'da']
}, function (err) {
	if (err) throw err; // This should throw an error, since "nein" is not in the validValues array
});

Works async as well

const	obj	= {};

checkKey({
	'obj':	obj,
	'objectKey':	'foo'
}, function (err) {
	if (err) throw err;

	// No error is thrown, since obj.foo is set in the async operation below
});

someAsyncThingie(function (err, someValue) {
	if (err) throw err;

	obj.foo	= someValue;
});

Full documentation on parameters

options.obj:	object	- Object to have its keys checked
options.objectKey:	string	- object key name
options.default:	any	- The default value if it does not exist
options.defaultLabel:	string	- What to print in the log as the default value (will default to "default" if it is a string)
options.retries:	integer	- used internally. Set to 10+ to have the method immediately set the default value or fail if the key does not exist

Keywords

FAQs

Package last updated on 15 Feb 2019

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