Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yamafaktory/pattern-guard
JavaScript pattern guards 💂
https://github.com/yamafaktory/pattern-guard
guards haskell pattern-guard
Last synced: 5 days ago
JSON representation
JavaScript pattern guards 💂
- Host: GitHub
- URL: https://github.com/yamafaktory/pattern-guard
- Owner: yamafaktory
- License: mit
- Created: 2017-02-01T23:26:54.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-05-05T14:18:55.000Z (over 4 years ago)
- Last Synced: 2024-10-01T15:44:09.857Z (about 1 month ago)
- Topics: guards, haskell, pattern-guard
- Language: JavaScript
- Size: 138 KB
- Stars: 68
- Watchers: 5
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pattern-guard 💂 [![Build Status](https://travis-ci.org/yamafaktory/pattern-guard.svg?branch=master)](https://travis-ci.org/yamafaktory/pattern-guard) [![npm version](https://img.shields.io/npm/v/pattern-guard.svg?style=flat)](https://www.npmjs.com/package/pattern-guard) [![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![Greenkeeper badge](https://badges.greenkeeper.io/yamafaktory/pattern-guard.svg)](https://greenkeeper.io/)
Pattern-guard is a small module that brings you the [Haskell guards syntax](https://wiki.haskell.org/Pattern_guard) in JavaScript.
## Usage
```js
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:
```js
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`:
``` js
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](http://standardjs.com/).
## License
Released under the [MIT license](https://opensource.org/licenses/MIT) by Davy Duperron.