项目作者: yamafaktory

项目描述 :
JavaScript pattern guards 💂
高级语言: JavaScript
项目地址: git://github.com/yamafaktory/pattern-guard.git
创建时间: 2017-02-01T23:26:54Z
项目社区:https://github.com/yamafaktory/pattern-guard

开源协议:MIT License

下载


Pattern-guard 💂 Build Status npm version Standard - JavaScript Style Guide

Greenkeeper badge

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

Usage

  1. const guards = require('pattern-guard')
  2. const [a, b, c] = [1, 3, 7]
  3. const result = guards({ a, b, c })`
  4. | a > b = 1337
  5. | b > c = 999
  6. | c > a = 42
  7. | c > b = 11
  8. `
  9. console.log(result) // 42

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

Guards can also be inlined:

  1. 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:

  1. const [a, b, c] = [1, 2, 3]
  2. const result = guards({ a, b, c })`
  3. | a > b = 'nope'
  4. | b > c = 'nope'
  5. | c < a = 'nope'
  6. | true = 'yep'
  7. `
  8. console.log(result) // 'yep'

Linting

The code quality is checked by the JavaScript Standard Style.

License

Released under the MIT license by Davy Duperron.