get-key-or-default

1.0.0 • Public • Published

Get Key or Default

Gets the value of an objects key, and falls back to a default value.

Example

let getKeyOrDefault = require('get-key-or-default');
 
// An object without a key
getKeyOrDefault({color: 'blue'}, 'height', 32);
// 32
 
// An object with a key
getKeyOrDefault({color: 'blue', height: 12}, 'height', 32);
// 12
 
// No object
getKeyOrDefault(undefined, 'height', 32);
// 32
 
// Validation
getKeyOrDefault({color: 'blue', height: 'twelve'}, 'height', 32, Number.isInteger);
// 32
 

Installation

$ npm install get-key-or-default

API

var getKeyOrDefault = require('get-key-or-default');

getKeyOrDefault(source, key, defaultValue, isValidCallback)

Type Data Type Name Description
parameter object source The object containing the key value.
parameter string/number key The key that contains the value.
parameter * defaultValue The result if the value is not found, or not valid.
parameter function isValidCallback Evaluates if a value provided is valid.
returns * n/a The source objects key value, otherwise the default value

isValidCallback(value)

The validation callback is fired when a defined value is evaluated.

Type Data Type Name Description
parameter !undefined value The value that is being validated.
returns boolean n/a True if the value is valid, otherwise false.

Dependents (0)

Package Sidebar

Install

npm i get-key-or-default

Weekly Downloads

1

Version

1.0.0

License

ISC

Last publish

Collaborators

  • lm3