New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

if-else-throw

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

if-else-throw

If X, return Y, else throw Z.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4K
decreased by-9.08%
Maintainers
1
Weekly downloads
 
Created
Source

if-else-throw

If X, return Y, else throw Z.

X can be a boolean or a function.

Installation

Requires Node.js 6.0.0 or above.

npm i if-else-throw

API

The module exports a single function.

Parameters

  1. test (any): The “if” condition, evaluated for truthiness. If a function is provided, it is called with val as its only argument and its return value is used.
  2. Optional: val (any): The “then” value to return if test is truthy. If omitted, test is returned.
  3. Optional: error (Error or string): The Error to throw if test is falsely. If a string is provided, it is wrapped in a new Error. If omitted, defaults to new Error().

Return Value

If test is truthy, returns val (or returns test if val is omitted). If test is falsey, there is no return value because an error is thrown.

Examples

const ifElseThrow = require('if-else-throw')

const value = []
ifElseThrow(
  Array.isArray(value), // The boolean condition (in this case, true)
  value, // The value returned if true
  new TypeError('Not an array') // The error thrown if false
) // []

ifElseThrow(
  x => Array.isArray(x), // x will be the second argument ('this is a string')
  'this is a string', // Given to the function and returned if the function returns true
  new TypeError('Not an array') // The error thrown if false
) // Uncaught TypeError: Not an array

ifElseThrow(
  'this is truthy',
  'Must be truthy'
) // 'this is truthy'

ifElseThrow(
  null,
  'Must be truthy'
) // Uncaught Error: Must be truthy

Keywords

FAQs

Package last updated on 11 Apr 2018

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