# fi

> Javascript library for writing, chaining and using conditional statements in a functional way

Latest version **1.0.16** (published 2022-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install fi
pnpm add fi
yarn add fi
bun add fi
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.16 |
| Published | 2022-03-11 |
| First published | 2013-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Arnor Heidar Sigurdsson |
| Maintainers | arnorhs |
| Keywords | ternary, operator, if, statement, condition, conditional, conditionals, else, switch, case, callback |

## Links

- npm: https://www.npmjs.com/package/fi
- Repository: https://github.com/arnorhs/arnorhs-packages
- Homepage: https://github.com/arnorhs/arnorhs-packages/tree/master/pkg/fi
- Issues: https://github.com/arnorhs/arnorhs-packages/issues
- npm.io page: https://npm.io/package/fi

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.0.16 (latest) — 2022-03-11
- 1.0.15 — 2022-03-11
- 1.0.13 — 2021-12-30
- 1.0.11 — 2021-12-04
- 1.0.9 — 2021-11-24
- 1.0.7 — 2021-11-21
- 1.0.6 — 2021-11-21
- 1.0.5 — 2021-11-20
- 0.0.5 — 2013-11-29
- 0.0.4 — 2013-05-19
- 0.0.3 — 2013-05-15
- 0.0.2 — 2013-05-15
- 0.0.1 — 2013-05-15

## README

# fi - Functional conditionals

With fi you can ignore the language constructs and write all your conditional logic in a functional way.

## usage with Typescript

```typescript
import { fi } from 'fi'

const myVar: string = fi(false, 'The dragon')
  .elseif(
    () => 1 == 2,
    () => 'Vampire',
  )
  .else('Wargulf')
  .ret()
```

## usage with javascript / CommonJS

```javascript
const { fi } = require('fi')

const myVar = fi(false, 'The dragon')
  .elseif(
    () => 1 == 2,
    () => 'Vampire',
  )
  .else('Wargulf')
  .ret()
```

### Installation

The library is distributed as an [npm module](https://npmjs.org/package/fi):

```sh
npm install fi
```

or

```sh
yarn add fi
```

### Examples

Basic if statement

```typescript
const getIfTrue = fi(true, 'flower puppy')

// getIfTrue() returns "flower puppy"
```

We can make it more interesting and add an elseif & else statements:

```typescript
const getIfTrue = fi(false, 'flower puppy')
  .elseif(() => false, 'something else')
  .else('space pedals')

// getIfTrue() returns 'space pedals'
```

Even more interesting using an if-else statement as well:

With a half completed if statment, we can also start chaining more stuff to it later

```typescript
const myif = fi(false, 'flower puppy').elseif(false, 'human skin')

// do some other stuff, and add to the chain:
const myVar = myif.else('crapware')

// myVar is "crapware"
```

Any value passed into any fi conditional or return value can be a function

Functions that don't meet a conditional are never executed, and conditionals that dont' meet a
condition (like an else if conditional in an if statement evaluated as true) will also never
get executed.

### Ternary

The ternary variant is not chainable and does not return a function

```typescript
import { ternary } from 'fi

const myVar = ternary(() => 1 > 4), () => 15, () => 42)

// myVar is 42
```

Switch statement using an object:

```javascript
var sw = require('fi').sw

var myVar = sw('Rainbow', {
  red: 'Redish',
  green: 'Greenish',
  blue: 'Blueish',
  default: 'Some color',
})

// myVar is "Some color"
```

Switch statement using an array (so you can use functions as your keys):

```javascript
var myVar = sw("Rainbow", [
    () => "red", "Redish",
    () => "green", () => "Random green",
    () => "blue", "Blueish",
    () => "Some color" // our default
})

// myVar is "Some color"
```

### But.. WHY?!

1. I don't know
2. It could potentially lead to some interesting use cases

### Author:

Arnor Heidar Sigurdsson
[@arnorhs on Twitter](http://twitter.com/arnorhs/)

### License

MIT

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