# block-expression

> block-expression is designed to mimic Rust-style if block expressions in JavaScript, offering a cleaner and more functional approach to conditional logic. This package provides a set of functions structured to handle conditional logic with chaining else i

Latest version **0.0.8** (published 2024-05-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install block-expression
pnpm add block-expression
yarn add block-expression
bun add block-expression
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2024-05-22 |
| First published | 2023-12-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | https://github.com/umudik |
| Maintainers | umudik |

## Links

- npm: https://www.npmjs.com/package/block-expression
- Repository: https://github.com/umudik/block-expression
- Homepage: https://github.com/umudik/block-expression#readme
- Issues: https://github.com/umudik/block-expression/issues
- npm.io page: https://npm.io/package/block-expression

## Recent versions

- 0.0.8 (latest) — 2024-05-22
- 0.0.7 — 2023-12-12
- 0.0.6 — 2023-12-03
- 0.0.5 — 2023-12-03
- 0.0.4 — 2023-12-03
- 0.0.2 — 2023-12-03

## README

# block-expression

The "block-expression" library provides a Rust-like if block structure for managing conditional logic in a simple and readable way. It is designed for use in JavaScript and TypeScript projects.

## Installation

To include it in your project, use the following command:

```bash
npm install block-expression
```

## Usage

The block.if function takes a structure consisting of an array of conditions and result blocks. The conditions are evaluated sequentially, and the result block of the first satisfied condition is executed.

### Simple Examples

### if

```javascript
import { block } from "block-expression";

const x = 10;
const result = block.if(
  [
    [x > 15, () => "large"], // else-if
    [x > 5, () => "medium"], // else-if
    [x > 0, () => "small"], // else-if
  ],
  () => "zero" // else
);

console.log(result); // Outputs: "medium" as string
```

#### Usage with Type Parameter

```typescript
import { block } from "block-expression";

const numberArray = [1, 234];
const result = block.if<boolean>( // It will be set automatically but you can still add it.
  [
    [Array.isArray(numberArray), () => true], // else-if
  ],
  () => false // else
);

console.log(result); // Outputs: true as boolean
```

### trycatch

```typescript
import { block } from "block-expression";

const result = block.trycatch(
  async function tryblock() {
    return "hello world";
  },
  async function catchblock() {
    return 2634;
  }
);

console.log(result); // Outputs: "hello world" as <string | number>
```

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