Code Monkey home page Code Monkey logo

more-sugar.js's Introduction

more-sugar.js

Mais uma biblioteca de açúcar sintático em JavaScript

Pattern matching

import {match} from 'more-sugar'

// Ao invés de
function taxRate(totalIncome) {
    if(totalIncome<300){
        return 0
    }
    if(totalIncome<600){
        return 0.03
    }
    if(totalIncome<940){
        return 0.05
    }
    if(totalIncome<1480){
        return 0.065
    }
    return 0.073
}

// Use
function taxRate(totalIncome) {
    return (match
    
      .lt(300,  () => 0)
      .lt(600,  () => 0.03)
      .lt(940,  () => 0.05)
      .lt(1480, () => 0.065)
      
      // Em outro caso
      ._(() => 0.073)
      
    )
}

Formas de usar os operadores match:

(match

    $operator($value_checker, $value)
    $operator($value_checker, $fn_value)
    $operator($fn_checker,    $value)
    $operator($fn_checker,    $fn_value)
    
    // $value_checker Pode ser um valor ou expressão regular
    // $fn_checker    É um lambda que retorna `true` ou `false`
    // $fn_value      É um lambda que retorna o produto da operação
    // $operator      Um dos:
    // - eq()            # igual a
    // - ne()            # diferente de
    // - ge()            # maior ou igual a
    // - gt()            # maior que
    // - le()            # menor ou igual a
    // - lt()            # menor que
    // - like()          # ...
    // - notlike()       # ...
    // - match(?)        # combina com expressão regular
    // - notmatch()      # não combina com expressão regular
    // - contains()      # ...
    // - notcontains()   # ...
    // - is(?)           # ...
    // - isnot(?)        # ...

)

Pipes

import {pipes} from 'more-sugar'

// Ao invés de
function render() {
    function step1(result) {
        // ...
        step2(result)
    }

    function step2(result) {
        // ...
        step3(result)
    }

    function step3(result) {
        // ...
        finish()
    }

    lib.getData(step1)
}

// Use
function render() {
    let chain = pipes()
        ['|>']((result) => {
            // step1 ...
            ok(result)
        })
        ['|>']((result) => {
            // step2 ...
            ok(result)
        })
        ['|>']((result) => {
            // step3 ...
            ok(result)
        })

    lib.getData(chain)
}

more-sugar.js's People

Contributors

erlimar avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.