Pattern Matching with patcom
https://github.com/concept-not-found/patcom
by Peer Reynders
HTML
<html>
<head>
<title>Comparing Fibonacci</title>
<meta charset="UTF-8" />
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Note:</h1>
<p>Just a JavaScript script—not a web page.</p>
<p>Check the "Console" tab.</p>
</body>
</html>
JavaScript
import { match, when, otherwise, greaterThanEquals } from 'https://unpkg.com/patcom?module';
function grade(score) {
return match(score)(
when(greaterThanEquals(90), () => 'A'),
when(greaterThanEquals(80), () => 'B'),
when(greaterThanEquals(70), () => 'C'),
when(greaterThanEquals(60), () => 'D'),
otherwise((_) => 'F')
);
}
console.log([91, 80, 79, 60, 59].map(grade)); // ["A", "B", "C", "D", "F"]