pyonkoro.match

1.0.1 • Public • Published

match.js

Match.js is a Javascript library like the 'switch' statement but it works as expression

Install

npm i -s pyonkoro.match
// or
yarn add pyonkoro.match

Usage

type Nullable = null | undefined;  
type MatchResult<R> = R | Nullable;

match<T, R> (
	cases: [(data: T) => boolean, (data: T) => R][], 
	defaultValue: R | Nullable, 
	data: T): MatchResult<R>,
)

Example

const defaultValue = 0;
const evenSquare = x => match([
    // if number x is even and is not zero, function should return square value of x
	[x => x !== 0 && x % 2 === 0, x => Math.pow(x, 2)],
	// if number x is odd, function should return just x
	[x => x % 2 !== 0, x => x],
	// if there is no matched case, function should return default value 0
], defaultValue, x);
evenSquare(2)
// => 4

evenSquare(3)
// => 3

evenSquare(0)
// => 0

Package Sidebar

Install

npm i pyonkoro.match

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

4.22 kB

Total Files

8

Last publish

Collaborators

  • nekoromancer