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 - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

11

index.js
'use strict'
const PossibleFunction = require('possible-function')
const errate = require('errate')
const ofn = require('ofn')
const pfn = require('pfn')
module.exports = function (test, val, err = new Error()) {
if (!(err instanceof Error)) err = new Error(err)
if (PossibleFunction(test, test)(val)) return val; else throw err
}
const toss = error => { throw errate(error) }
module.exports = ofn([0, 2, 1], (test, val = test, error = new Error()) => pfn(test, test)(val) ? val : toss(error))
MIT License
Copyright ©2017 John Lamansky
Copyright ©2017–2018 John Lamansky

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

{
"name": "if-else-throw",
"version": "1.0.0",
"version": "2.0.0",
"description": "If X, return Y, else throw Z.",

@@ -19,10 +19,12 @@ "keywords": [

"engines": {
"node": ">=5.0.0"
"node": ">=6.0.0"
},
"dependencies": {
"possible-function": "^1.0.1"
"errate": "^1.0.0",
"ofn": "^1.0.0",
"pfn": "^1.0.0"
},
"devDependencies": {
"eslint-config-lamansky": "^1.0.0",
"mocha": "^3.2.0"
"mocha": "^5.0.5"
},

@@ -29,0 +31,0 @@ "scripts": {

@@ -9,14 +9,29 @@ # if-else-throw

Requires [Node.js](https://nodejs.org/) 6.0.0 or above.
```bash
npm install if-else-throw --save
npm i if-else-throw
```
## Usage
## 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')
let value
value = []
value = ifElseThrow(
const value = []
ifElseThrow(
Array.isArray(value), // The boolean condition (in this case, true)

@@ -27,8 +42,17 @@ value, // The value returned if true

let value = 'totally not an array'
value = ifElseThrow(
val => Array.isArray(val), // Instead of a boolean, can use a function
value,
new TypeError('Not an array')
) // Uncaught TypeError
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
```
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