Socket
Book a DemoInstallSign in
Socket

object-only-value

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

object-only-value

Assert that object has only one key and return its value

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

object-only-value

Build Status

Assert that object has only one key and return its value.

For when you want to do obj[Object.keys(obj)[0]], but know that if obj somehow ended up with zero or more keys than one, it'd end in a disaster.

Literally:

var assert = require('assert');

module.exports = function(obj) {
  var keys = Object.keys(obj);
  assert.equal(keys.length, 1, 'Object should have exactly one key');
  return obj[keys[0]];
};

Installation

npm install object-only-value

Usage

var onlyValue = require('object-only-value');
var obj = { foo: 42 };
var theValue = onlyValue(obj); // => 42

obj = {};
theValue = onlyValue(obj); // => throws

obj = { foo: 42, bar: 24 };
theValue = onlyValue(obj); // => throws

Keywords

object

FAQs

Package last updated on 20 Jan 2015

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