ternary-function

use the result if it's truthy of falsy.

Usage no npm install needed!

<script type="module">
  import ternaryFunction from 'https://cdn.skypack.dev/ternary-function';
</script>

README

Ternary Function

use the result if it's truthy of falsy

npm npm bundle size

Usage

import ternary from 'ternary-function';
                                  
var result = ternary(10+5, t => t*2, f => f+10) // will return "30", because 10+5 (15) is "truthy"
                                  
var result = ternary(
    localStorage.getItem('abc'), // if it returns a truthy value...
    t => JSON.parse(t), // the returned value will be JSON parsed,...
    null // but it it's falsy, it will return null.
)

/**
 * Ternary function will automatically return undefined if no falsy value is set
 */
var result = ternary(10-10, t => t*2); // will return "undefined", becauset 10-10 (0) is "falsy"
var result = ternary(
    localStorage.getItem('xyz'), // if it returns a truthy value...
    t => JSON.parse(t) // the returned value will be JSON parsed,...
) // but it it's falsy, it will return undefined.

Installation

  1. Install it using npm or yarn
    • npm install --save ternary-function
    • yarn add ternary-function
  2. Import it
    • import ternary from 'ternary-function'
  3. Use it 😄