0.1.3 • Published 3 years ago

patty-melt v0.1.3

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

patty-melt

Pattern matching in JavaScript and Typescript made simple.

Examples

Assign values conditionally

import { pattyMelt } from 'patty-melt';

const today = (new Date()).getDay();

const dayOfTheWeek = pattyMelt(today, [
  [0, 'Sunday'],
  [1, 'Monday'],
  [2, 'Tuesday'],
  [3, 'Wednesday'],
  [4, 'Thursday'],
  [5, 'Friday'],
  [6, 'Saturday'],
]);

console.log(`Have a fantastic ${dayOfTheWeek}!`);

Simplify switch statements

import { pattyMelt } from 'patty-melt';
import getDayOfYear from 'date-fns/getDayOfYear';

const today = getDayOfYear(new Date());

// switch (today) {
//   case 1:
//     console.log('Happy New Year!');
//     break;
// 
//   case 45:
//     console.log('Happy Valentine\'s Day');
//     break;
//   
//   default:
//     console.log('Have a great day!');
// }

pattyMelt(today, [
  [1, () => console.log('Happy New Year!')],
  [45, () => console.log('Happy Valentine\'s Day')],
]) || console.log('Have a great day!');

Or, if you prefer a more familiar syntax:

import { defaultCase, pattyMelt } from 'patty-melt';
import getDayOfYear from 'date-fns/getDayOfYear';

const today = getDayOfYear(new Date());

pattyMelt(today, [
  [1, () => console.log('Happy New Year!')],
  [45, () => console.log('Happy Valentine\'s Day')],
  [defaultCase, () => console.log('Have a great day!')],
]);

Match values using regular expressions

import { pattyMelt } from 'patty-melt';

const today = (new Date()).toString();

pattyMelt(today, [
  [/^(Mon|Tue|Wed|Thu|Fri)/, () => console.log('Have a productive weekday!')],
  [/^(Sat|Sun)/, () => console.log('Enjoy your weekend!')],
]);

Match values using functions

import { pattyMelt } from 'patty-melt';

const today = (new Date()).getDay();

pattyMelt(today, [
  [day => ([0, 6].contains(day)), () => console.log('Have a productive weekday!')],
  [day => (day > 0 && day < 6), () => console.log('Enjoy your weekend!')],
]);

Refactor variable reassignment with a declarative syntax

Before:

const value = Math.ceil(Math.random() * 20)  - 10;

let message = '';

if (value > 0) {
    message = 'value is positive';
} else {
    message = 'value is zero or negative';
}

First refactor:

import { pattyMelt } from 'patty-melt';

const value = Math.ceil(Math.random() * 20)  - 10;

const message = pattyMelt(value > 0, [
    [true, 'value is positive'],
    [false, 'value is zero or negative']
]);

Second refactor (way more declarative!):

import { pattyMelt } from 'patty-melt';

const value = Math.ceil(Math.random() * 20)  - 10;

const message = pattyMelt(value, [
    [x => (x > 0), 'value is positive'],
    [x => (x <= 0), 'value is zero or negative']
]);
0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago