This is a playground
to test code. It runs a full Node.js
environment and already has all of npm
’s 1,000,000+ packages pre-installed, including match-toy
with all npm
packages installed. Try it out:
const { match } = require('match-toy');
const factorial = match
.case('0', () => 1)
.case('n', ({ n }) => n * factorial(n - 1))
.end();
console.log('0', factorial(0));
console.log('4', factorial(4));
console.log('10', factorial(10));
const howMany = match
.case('[]', () => 'empty')
.case('[_]', () => 'one')
.case('[_, _]', () => 'two')
.case('[_, _, ...]', () => 'more than two')
.else(() => 'it is not a list')
.end();
console.log(howMany([]));
console.log(howMany([1]));
console.log(howMany([1, 2]));
console.log(howMany([1, 2, 3]));
console.log(howMany('?'));
This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.