Barefoot
Barefoot is a utility-belt library for Node for asynchronous functions manipulation
To install it
npm install barefoot
To use it
bf = require 'barefoot'
Module dependencies
lateral = require 'lateral'_ = require 'underscore'
Let's get started
toDictionary
Transform an array of object into a dictionary based on the property passed as a second param
= dictionary = arrayforEach dictionaryeltprop= elt if elt? and eltprop? return dictionary
has
Provides a function which test if parameters object has certain properties
= ok = true ok = ok and params? and paramspar? for par in parameters done if ok then null else "Missing Parameters"params
amap
Asynchronous map Use the awesome lateral module to do the job
= results = errors = null unit = lateralcreate func item if err? errors ?= errorspusherr resultspush null else resultspush res complete nbProcesses unitaddarraywhen done errorsresults
chain
Chain aynschronous methods with signature (val, done) -> done(err, result) Stop if one of the method has an error in the callback
= if funcslength == 0 done nullval else funcs0val if err? done errres else chainfuncs1..resdone
avoid
Wrap a void returning function to make it callable in a chain
= funcparams done nullparams
parallel
Execute asynchronous functions which take same inputs
= i = 0 errors = results = = i++ errorspusherr if err? resultspush result if i == funcslength error = if errorslength > 0 then errors else null done errorresults funcsforEach func paramstempDone
getRequestParams
= params = for field in "body""query""params" if reqfield? params = _extend paramsreqfield params.user = requser if requser? params
webService
= method getRequestParamsreq if err? ressend 500 else if contentType == "application/json" ressend data else rescontentType contentType resend datatoString
webPage
= if not method? and template? data = getRequestParamsreq data.__ = template : template resrender templatedata else method getRequestParamsreq if err? ressend 500 else data = if not data? data.user = requser if requser? and not datauser? data.__ = template : template resrender templatedata
memoryCache
= cache = hash = JSONstringifyparams if cachehash? and cachehashexpiration > done nullcachehashresult else method params if not err? cachehash= result : res expiration : setSecondsgetSeconds + seconds done errres
Export public methods
module.exports = toDictionary : toDictionary has : has amap : amap chain : chain avoid : avoid parallel : parallel webService : webService webPage : webPage memoize : memoize