yuanchuan/match: Erlang-like pattern matching in JavaScript.

Erlang-like pattern matching in JavaScript (use at your own risk).

const { match } = require('@yuanchuan/match');

const sum = match(
  ([]) => 0,
  ([H, ...T]) => H + sum(T)
);

// 10
sum([1, 2, 3, 4]);

Installation

npm install --save @yuanchuan/match

Patterns

Unlike the proposal-pattern-matching, patterns inside the function arguments are quite limited supported. However, it can be extended using when().

Match by number of parameters

const greeting = match(
  (User) => greeting(Str, "Hi

Read the entire article at the source link...

We're happy to share this resource that we found. The content displayed on this page is property of it's original author and/or their organization.

Leave a Reply