0.0.20 • Published 7 years ago

switz v0.0.20

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Switz - Yet another switch-like control structure.

NPM Version Build Status Coverage Status

It's just reinventing the wheel.

Switch-like control structure written in TypeScript.

Installation

$ npm install --save switz

Usage

Syntax

switz

switz(subject, statement)

subject - compared to case conditions. statement - function that determines cases and other behaviors.

statement

statement will be called with an argument context. context has case, default and matcher methods.

context

  • context.case(condition: any, handler: (match: any) => any): Set case with condition and handler function.
  • context.default(handler: () => any): Set default handler function.
  • context.matcher(matcher: (subject: T, condition: any) => any): Set matcher function used to compare subject and each case's condition. If this returns truthy value, tha case matches.

Example

switch - JavaScript

// switz
const switz = require("switz");

switz(expr, s => {
  s.matcher(switz.IncludedMatcher);

  s.case(["Oranges"], () => {
    console.log('Oranges are $0.59 a pound.');
  });

  s.case(["Apples"], () => {
    console.log('Apples are $0.32 a pound.');
  });

  s.case(["Bananas"], () => {
    console.log('Bananas are $0.48 a pound.');
  });

  s.case(["Cherries"], () => {
    console.log('Cherries are $3.00 a pound.');
  });

  s.case(["Mangoes", "Papayas"], () => {
    console.log('Mangoes and papayas are $2.79 a pound.');
  });

  s.default(() => {
    console.log('Sorry, we are out of ' + expr + '.');
  });
});

// Native switch
switch (expr) {
  case 'Oranges':
    console.log('Oranges are $0.59 a pound.');
    break;
  case 'Apples':
    console.log('Apples are $0.32 a pound.');
    break;
  case 'Bananas':
    console.log('Bananas are $0.48 a pound.');
    break;
  case 'Cherries':
    console.log('Cherries are $3.00 a pound.');
    break;
  case 'Mangoes':
  case 'Papayas':
    console.log('Mangoes and papayas are $2.79 a pound.');
    break;
  default:
    console.log('Sorry, we are out of ' + expr + '.');
}

Returns matched case's handler output.

const value1 = switz("foo", s => {
  s.case("foo", () => "foo!");
  s.case("bar", () => "bar!");
});

console.log(value1); // foo!

Can match subject and conditions with preset matcher. Also, can use matcher output as handler input.

import switz, {RegexpMatcher} from 'switz';

const message = switz("fooooooooooooooooooooooooooooooo", s => {
  s.matcher(RegexpMatcher);
  s.case(/fo{10,}/, match => `yes, ${match[0].length} "o"s.`);
  s.default(() => "no");
});

console.log(message); // yes, 32 "o"s.

Can be used with type parameter in TypeScript.

const v = switz<string>("foo", s => {
  s.case("foo", () => "bar");
  s.case("bar", () => "baz");
  s.case("fuz", () => 123); // Type unmatch ERROR!
});

Method-chainable.

switz("foo", s => s
  .case("foo", () => "bar")
  .case("bar", () => "baz")
);

The function switz is a wrapper of Switch class instance. You can use Switch and Case class directly if you would like to.

const {Switch, Case} = require("switz");
// Or use `import`
import {Switch, Case} from "switz";

const subject = Math.floor(Math.random() * 100);

const mySwitch = new Switch(subject);

mySwitch.setMatcher((s, c) => {
  const min = c[0];
  const max = c[1];

  return min <= s && max >= s;
});

mySwitch.addCase(new Case([0, 50], () => "Between 0-50"));
mySwitch.addCase(new Case([50, 100], () => "Between 50-100"));

console.log(mySwitch.evaluate());

See also: test codes

Develop

First.

$ git clone
$ cd switz
$ yarn

Test.

$ yarn test

Lint.

$ yarn run lint
0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago