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;
});
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);
console.log(warning);
});
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;
});
Works async as well
const obj = {};
checkKey({
'obj': obj,
'objectKey': 'foo'
}, function (err) {
if (err) throw err;
});
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