1.0.0 • Published 8 years ago

get-key-or-default v1.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
8 years ago

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)

TypeData TypeNameDescription
parameterobjectsourceThe object containing the key value.
parameterstring/numberkeyThe key that contains the value.
parameter*defaultValueThe result if the value is not found, or not valid.
parameterfunctionisValidCallbackEvaluates if a value provided is valid.
returns*n/aThe source objects key value, otherwise the default value

isValidCallback(value)

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

TypeData TypeNameDescription
parameter!undefinedvalueThe value that is being validated.
returnsbooleann/aTrue if the value is valid, otherwise false.