Skip to content

umudik/block-expression

Repository files navigation

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:

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

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

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

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>

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published