ceiocs
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

ceiocs

MIT License npm version Build Status Maintainability Test Coverage

About

ceiocs is library that allows developers to use conditional 'expressions' instead of conditional 'statements'.

ceiocs means Conditional Expression Instead Of Conditional Statements.

Usage

Introduction

when you use ceiocs, you should import as following.

import { branch, match } from "ceiocs";

you can use boolean as condition and anything as value.

import { branch, match } from "ceiocs";

// like if-statement
branch
  .if(false, "a")
  .elseif(false, "b")
  .else("c"); // => "c"

// like switch-statement
match
  .case(123)
  .when(100, "a")
  .when(123, "b")
  .otherwise("c") // => "b"

when you want to delay evaluation, you can use function. it is allowed to use both of function and other at same time.

when you use function, each function is not evaluated until it is required. on following code, () => 3 is not evaluated.

import { branch, match } from "ceiocs";

branch
  .if(() => false, 1)
  .elseif(true, () => 2)
  .else(() => 3); // => 2

match
  .case(() => 123)
  .when(100, () => "a")
  .when(() => 123, () => "b")
  .otherwise(() => "c") // => "b"

Async

when you want to async condition or value, you must use async mode.

all you need is only pass async property wihtin method chain. if you use async value or function without async property, you can not compile.

you do not have to pass async at first. only when you need.

import { branch, match } from "ceocis"

branch
  .async.if(async () => true, 100)
  .elseif(true, 50)
  .else(Promise.resolve(20)) // => Promise.resolve(100)

branch
  .if(false, 100)
  .async.elseif(true, async () => 50)
  .else(Promise.resolve(20)) // => Promise.resolve(50)

match
  .async
  .case(async () => 123)
  .when(100, Promise.resolve("a"))
  .when(Promise.resolve(123), async () => "b")
  .otherwise(async () => "c") // => Promise.resolve("b")

match
  .case(123)
  .when(100, "a")
  .async
  .when(Promise.resolve(123), async () => "b")
  .otherwise(async () => "c") // => Promise.resolve("b")

License

MIT

Package Sidebar

Install

npm i ceiocs

Weekly Downloads

8

Version

0.0.9

License

MIT

Unpacked Size

67.1 kB

Total Files

37

Last publish

Collaborators

  • topo