0.1.11 • Published 6 years ago

check-object-key v0.1.11

Weekly downloads
15
License
ISC
Repository
github
Last release
6 years ago

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
0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago