Giter Club home page Giter Club logo

conductor's Introduction

Introduction

conductor

Conductor on npmjs Conductor download stats on npmjs Codeship Status for WaldoJeffers/conductor codecov

conductor is a modern utility library to help you control the execution flow using functional programming.

๐Ÿคต description

It provides a set of utility functions which can be used both with asynchronous and synchronous code, allowing you to control your execution flow very clearly and with a minimum of code. The library is designed in a functional programming spirit, to provide a coherent API and highly composable functions. Think of it as if Ramda & Async had a baby.

Read more on Why I'm building conductor.

๐Ÿ”ง installation

npm install conductor

โœจ examples

Here are a few examples of what you can do with conductor.

use asynchronous functions seamlessly

import { map } from 'conductor'

const fetchCharacter = id => fetch(`https://swapi.co/api/people/${id}`).then(res => res.json())
const character_ids = [1, 2, 3]
await map(fetchCharacter, character_ids) // [{id: 1, name: 'Luke'}, ...]

You can use map with an asynchronous mapper and directly use the await keyword. No need to use Promise.all like you need to with Array.prototype.map or lodash.map.

compose things

import { compose, get } from 'conductor'

const character_id = 1
const fetchCharacter = id => fetch(`https://swapi.co/api/people/${id}`).then(res => res.json())
const fetchPlanet = url => fetch(url).then(res => res.json())
const getHomeworldName = compose(get('name'), fetchPlanet, get('homeworld'), fetchCharacter)

await getHomeworldName(character_id) // Tatooine

You can compose functions seamlessly, without ever wondering if you need to use Promise.prototype.then because one function returns a Promise. Simply add await before compose if one your functions is asynchronous.

functional by design

import { compose, equals, get, join, map, filter } from 'conductor'

const jedis = [
  { name: 'Luke', side: 'light' },
  { name: 'Yoda', side: 'light' },
  { name: 'Darth Vader', side: 'dark' },
]
const isGood = filter(compose(equals('light'), get('side')))
const getName = map(get('name'))
const concat = join(', ')

compose(concat, getName, isGood)(jedis) // 'Luke, Yoda'

All functions in conductor are curried by default, which means they can be used in a partially applied form to define very modular and composable blocks in your code. In the example above, we have an array of jedis, and we want to retrieve a concatenated string of all the good guys' name. We first define an isGood function, which will filter out the bad guys. Then, we create a mapping functiongetNamewhich will retrieve each jedi's name. Finally, we create a concatenating function called concat. We can now easily compose them and pass thejedis array to the resulting function. Notice how we created small & modular point-free functions, and only passed the input data when we actually needed to.

๐Ÿ“– documentation

๐Ÿ—ฟ influences

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.