Skip to content

nekoromancer/match.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •