0.0.24 • Published 10 years ago

barefoot v0.0.24

Weekly downloads
2
License
-
Repository
github
Last release
10 years ago

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'

Let's get started

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 errors, results

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 err, params
    else
      funcs[0] params, (err, res) =>
        if err?
          done err, res
        else
          methods.chain(funcs.slice(1, funcs.length))(res, done, err)

Export public methods

module.exports = methods
0.0.24

10 years ago

0.0.23

10 years ago

0.0.22

10 years ago

0.0.21

11 years ago

0.0.20

11 years ago

0.0.19

11 years ago

0.0.18

11 years ago

0.0.17

11 years ago

0.0.16

11 years ago

0.0.15

11 years ago

0.0.14

11 years ago

0.0.13

11 years ago

0.0.12

11 years ago

0.0.11

11 years ago

0.0.10

11 years ago

0.0.9

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago