Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

z Native pattern matching for Javascript.

Build Status Coverage Status Dependency Status NPM version

Usage

  • install via npm: npm install z
  • require z in your code, and name the match function as you want!
const matches = require('z')
  • you can match by comparing values
const person = { name: 'Johna' }
matches(person)(
  (x = { name: 'John' }) => console.log('John you are not welcome!'),
  (x)                    => console.log(`Hey ${x.name}, you are welcome!`)
)
  • you can match by comparing types or instances
matches(1)(
  (x = 2)      => console.log(`number 2 is the best!!!`),
  (x = Number) => console.log(`number ${x} is not that good`),
  (x = Date) => console.log(`blaa.. strings are awful!`)
)
  • on arrays, you can split head and tail, so you can do awesome recurring functions! , without manipulating arrays or changing state. Yes true functional programming in JavaScript!
const reverse = (list) => matches(list)(
  (head, tail = []) => [head], // stopping condition
  (head, tail)      => myReverse(tail).concat(head)
)


reverse([1, 2, 3, 4])) // returns [4, 3, 2, 1]

Available Patterns

  • Matches by value: (x = 1) =>, (x = null) =>, (x = 'true') =>
  • Matches by object and array: (x = {a: 1}) =>, (x = [1, 2]) =>
  • Matches by type: (x = String) =>, (x = Boolean) =>
  • Matches by instance: (x = Date) =>, (x = Person) =>
  • Matches spiting array into head and tail (head, tail) =>, it can be multiple heads, eg.: (x, y, z, xs), (a, b, c, d, e, f, g, tail), etc...

This is amazing! Why nobody did this before on JavaScript?

He did, years ago! But his first and greater Javascript pattern match library create matches with strings, and this is ugly! (sorry). Maybe he thought this is ugly too and he did a new pattern matching library without strings, but need to install some macro stuff.. and well, I don't think that's nice either.

Why z was created? Because IT'S B E A U T I F U L: No need to install nothing but the actual z library, the matches are real native javascript code, the patterns are written naturally, pure ❤️.

The project is new and growing!

Suggestions, bug reports and pull requests are welcomed!

About

native pattern matching for javascript

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages