if-else-throw
Advanced tools
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 | ||
``` |
3670
57
3
+ Addederrate@^1.0.0
+ Addedofn@^1.0.0
+ Addedpfn@^1.0.0
+ Added@lamansky/flatten@1.0.0(transitive)
+ Addedarray-pad@0.0.1(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedcase-insensitive@1.0.0(transitive)
+ Addedclass-chain@1.0.0(transitive)
+ Addedcopy-own@1.2.0(transitive)
+ Addedenglish-list@1.0.0(transitive)
+ Addederrate@1.3.0(transitive)
+ Addedffn@2.1.0(transitive)
+ Addedhas-duplicates@1.0.0(transitive)
+ Addedis-class-of@1.0.1(transitive)
+ Addedis-global-object@1.0.0(transitive)
+ Addedis-instance-of@1.0.2(transitive)
+ Addedis-nil@1.0.1(transitive)
+ Addedis-obj@1.0.1(transitive)
+ Addedis-object@1.0.2(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedlodash.set@4.3.2(transitive)
+ Addedofn@1.0.0(transitive)
+ Addedpfn@1.1.0(transitive)
+ Addedplainify@1.0.0(transitive)
+ Addedqfn@1.0.1(transitive)
+ Addedrtrim-array@1.1.0(transitive)
+ Addedsbo@1.1.3(transitive)
+ Addedsorp@1.0.0(transitive)
+ Addedtrim-call@1.1.0(transitive)
+ Addedwfn@1.0.0(transitive)
- Removedpossible-function@^1.0.1
- Removedpossible-function@1.0.1(transitive)