barefoot

barefoot is a utility-belt library for Node for asynchronous functions manipulation

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'

lateral = require 'lateral'

We declare an object to contain all the methods we want to export from this module

methods = {}

toDictionary

Transform an array of object into a dictionary based on the property passed as a second param

methods.toDictionary = (array, prop) ->
  dictionary = {}
  array.forEach (elt) -> 
    dictionary[elt[prop]= elt if elt? and elt[prop]?
  return dictionary

has

Provides a function which test if parameters object has certain properties

methods.has = (parameters) ->
  (params, done) ->
    ok = true
    ok = (ok and params? and params[par]?) for par in parameters
    done (if ok then null else new Error("Missing Parameters"))params

amap

Asynchronous map Use the awesome lateral module to do the job

methods.amap = (func, nbProcesses = 1) ->
  (array, done) ->
    results = []
    errors = null
    unit = lateral.create (complete, item) ->
      func item(err, res) ->
        if err?
          errors ?= []
          errors.push(err)
          results.push null
        else
          results.push res
        complete()
    nbProcesses
 
    unit.add(array).when () ->
      done errorsresults

chain

Chain aynschronous methods with signature (params, done) -> done(err, result) Stop if one of the method has an error in the callback

methods.chain = (funcs) -> 
  (params, done, err) ->
    if funcs.length == 0
      done errparams
    else
      funcs[0params(err, res) =>
        if err?
          done errres
        else
          methods.chain(funcs.slice(1funcs.length))(resdoneerr)
module.exports = methods