Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

yamafaktory/pattern-guard

Repository files navigation

Pattern-guard

Pattern-guard is a small module that brings you the Haskell guards syntax in JavaScript.

Usage

b = 1337 | b > c = 999 | c > a = 42 | c > b = 11 ` console.log(result) // 42">const guards = require('pattern-guard')

const [a, b, c] = [1, 3, 7]

const result = guards({ a, b, c })`
| a > b = 1337
| b > c = 999
| c > a = 42
| c > b = 11
`

console.log(result) // 42

Please note that, like in Haskell, the first truthy guard will be returned.

Guards can also be inlined:

c = 'nope' | c > a = 'maybe'`">guards({ a, b, c })`a < b = 'yep' | b > c = 'nope' | c > a = 'maybe'`

And they support all comparison operators < <= == === !== != >= > and the logical operators too && || !.

In Haskell, the otherwise keyword is used as a catch-all. To avoid using this keyword as variable name, this logic is not implemented in the module, you can simply emulate the same behaviour by using true:

c = 'nope' | c < a = 'nope' | true = 'yep' ` console.log(result) // 'yep'">const [a, b, c] = [1, 2, 3]

const result = guards({ a, b, c })`
| a > b = 'nope'
| b > c = 'nope'
| c < a = 'nope'
| true = 'yep'
`

console.log(result) // 'yep'

Linting

The code quality is checked by the JavaScript Standard Style.

License

Released under the MIT license by Davy Duperron.

About

JavaScript pattern guards

Topics

Resources

Readme

License

MIT license

Stars

Watchers

Forks

Packages

Contributors