Giter Site home page Giter Site logo

cloudxtreme / lodash-fp-composition Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coderofsalvation/lodash-fp-composition

0.0 0.0 0.0 14 KB

Just a bunch of practical functional mixins for lodash/fp to make code more readable/maintainable

JavaScript 98.10% Shell 1.90%

lodash-fp-composition's Introduction

keep the darkside away: practical functional mixins for lodash/fp to make code more readable, maintainable & composable

Warning: Experimental

Philosophy

  1. functional programming in javascript has 2 categories: the good stuff..and there's the other stuff :)
  2. _.flow and _.compose is good stuff
  3. composition of promises and functions should be hasslefree
  4. nested ifs/elses and early function returns, invite powers of dark side (see anti-if-campaign)

So..what does code looks like when using this library?


engine.getOrCreateUser   = _.flow( 
                               _.either( engine.getUser, engine.createUser ), 
                               _.maybe( _.log("user ok") )
                           )

engine.init              = _.flow(
                               _.when( getOrCreateUser,     _.lensOver( 'user', getOrCreateUser ) ), 
                               _.when( engine.isInited,     _.lensOver('inited', => true) ), 
                               _.when( engine.isNotInited,  _.error("something went wrong") ),
                           )

engine.init( _.clone(engine) ) 

see full example here

Functions

_.either(a, b)

this will execute function b only when function a returns false/null/undefined

example: _.either(getUserByEmail,createUserEmail)("[email protected]")

_.maybe(fn)

this will execute function fn only when there's input. this comes in handy when its unsure whether the previous function was succesful in a chain/flow/composed function.(){}

example: .flow( getOrCreateUser, maybe(.log("user ok")) )

_.when(f, g)

hipster if statement, only execute function g when function f does not return null/false/undefined

example: _.when( _.isString, console.log )("foo")

_.flow( mixed_array_of_promises_and_functions )

improved version of _.flow, which also supports automatic resolving of promises

example: _.flow( new Promise(.....), alert )("[email protected]")

_.lensOver(path, fn)

lens over allows i/o for a nested property

example: var updateBar = _.flow( -> 123, _.log ) _.lensOver( "foo.bar", updateBar )({foo:{bar:0}}) // sets 'foo.bar' to 123 (and prints in console)

_.template_es6(es6_template)

simple es6 templates for in the browser

example: _.template_es6('${foo}', {foo:"bar"}) // outputs 'bar'

_.prefix(prefix, fn)

simple way to prefix a function which outputs a string

example: _.error = _.prefix("error: ", _.log)

_.postfix(postfix, fn)

simple way to postfix a function which outputs a string

example: _.flow( _.get('.length'), _.prefix("items", _.log) )([1, 2, 3])

_.log(str)

simple log function (which forwards input to output)

example: _.flow( doFoo, _log, doBar )({input:"foo"})

_.error(str)

simple error function (which forwards input to output)

example: _.when( !hasFoo, _.prefix("something went wrong:", _error ) )({input:"foo"})

_.trigger(fn)

trigger simply executes a function OR promise, but forwards original input as output. this comes in handy when you don't want to break a flow/chain

example: _.flow( doSomethingWithInput, _.trigger( alert ), doSomethingElseWithInput )({foo:"bar"})

_.mapAsync(arr, done, cb)

calls cb(data, next) for each element in arr, and continues loop based on next()-calls (last element propagates done()). Perfect to iterate over an array synchronously, while performing async operations inbetween the elements.

example: _.mapAsync([1, 2, 3], alert, (data, next) => next() )

lodash-fp-composition's People

Contributors

coderofsalvation 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.