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

get-key-or-default

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-key-or-default

Get object key or default value

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 04 Jul 2016

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