# if-else-throw

> If X, return Y, else throw Z.

Latest version **2.0.0** (published 2018-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install if-else-throw
pnpm add if-else-throw
yarn add if-else-throw
bun add if-else-throw
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2018-04-11 |
| First published | 2017-11-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 3 |
| Unpacked size | 3.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | John Lamansky |
| Maintainers | lamansky |
| Keywords | error, throw, test, check, passthrough |

## Links

- npm: https://www.npmjs.com/package/if-else-throw
- Repository: https://github.com/lamansky/if-else-throw
- Issues: https://github.com/lamansky/if-else-throw/issues
- npm.io page: https://npm.io/package/if-else-throw

## Dependencies (3)

- [ofn](https://npm.io/package/ofn.md) ^1.0.0
- [pfn](https://npm.io/package/pfn.md) ^1.0.0
- [errate](https://npm.io/package/errate.md) ^1.0.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2018-04-11
- 1.0.0 — 2017-11-28

## README

# if-else-throw

If X, return Y, else throw Z.

X can be a boolean or a function.

## Installation

Requires [Node.js](https://nodejs.org/) 6.0.0 or above.

```bash
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

```javascript
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
```

---
_Source: https://npm.io/package/if-else-throw · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
