Giter Site home page Giter Site logo

zepatrik / switchcase Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anywhichway/switchcase

0.0 2.0 0.0 51 KB

Declarative and functional switch supporting literals, functional tests, and regular expressions

License: MIT License

JavaScript 84.20% HTML 15.80%

switchcase's Introduction

Codacy Badge

switchcase

Declarative and functional switch supporting literals, functional tests, and regular expressions.

This module can be used to simplify code using if then else and switch statements. It can also be used as a foundation for super flexible routers.

Installation

npm install switchcase

For browsers, use the files in the browser directory.

API

Lets start with an example:

let sw = switchcase({
	1: "Case one, a literal",
	[(value) => value===2]: "Case two, a function",
	[/3/]: "Case three, a regular expression",
	[4]: () => "Case four, just demonstrating a functional value",
	default: "Defaulted"
});

console.log(sw(1));
console.log(sw(2));
console.log(sw(3));
console.log(sw(4)()); // note the function invocation
console.log(sw(5));

will print

Case one, a literal

Case two, a function

Case three, a regular expression

Case four, just demonstrating a functional value

Defaulted

In short, calling switchcase with an object will return a functional switch. By using ES2015 object initializer syntax, the properties of the object can look like they are functions or regular expressions.

The returned function can also be enhanced through the use of chained calls:

  1. case(test,value), which adds a test and value.

  2. default(value), which sets the default result.

The same example above using this approach:

let sw = switchcase()
	.default("Defaulted") // note, you can put the default anywhere!
	.case(1,"Case one, a literal")
	.case(value => value===2, "Case two, a function")
	.case(/3/,"Case three, a regular expression")
	.case(4,() => "Case four, just demonstrating a functional value");

console.log(sw(1));
console.log(sw(2));
console.log(sw(3));
console.log(sw(4)()); // note the function invocation
console.log(sw(5));

You can also optionaly use sw.match(value) in place of direct invocation, sw(value), if you think it assists with clarity.

Finally, a second argument to switchcase(<object>,<boolean>) will treat all property values in the case object that can be converted to integers as integers and only match integers to those properties. Otherwise, a soft compare is done and "1" will equal 1.

Internals

The main power of switchcase comes from the use of the ES2015 object initializer syntax, []. This allows the use of the JavaScript interpreter to validate functions and regular expressions before they get turned into string property names. The switchcase function then loops through the properties in the object and converts them back to functions or regular expressions.

Rational and Background

There are a number of articles on the use of decalarative or functional approaches to the replacement of switch. In our opinion, here are three of the best:

  1. July, 2014 Replacing switch statements with object literals ... plus commentary on Reddit.

  2. Jan, 2017 Rethinking JavaScript: Eliminate the switch statement for better code

  3. Nov, 2017 Alternative to JavaScript’s switch statement with a functional twist

We simply wanted a switch capability that could support literals, functional tests, and regular expressions so that we could build super flexible routers.

Release History - Reverse Chronological Order

2018-04-13 v1.0.0 Code style improvements. Fixed node.js unit test config.

2017-11-27 v0.0.3 Added unit tests

2017-11-27 v0.0.2 Added documentation

2017-11-27 v0.0.1 First public release

switchcase's People

Contributors

anywhichway avatar atticoos avatar

Watchers

 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.