Giter Site home page Giter Site logo

switz's Introduction

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

switz's People

Stargazers

Hiroki Masuda avatar IWABUCHI Yu(u)ki (butchi) avatar anz avatar Orito Itsuki avatar Kamata, Ryo avatar Shinichi Takahashi avatar

Watchers

James Cloos avatar Yoriki Yamaguchi avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.