barefoot

0.0.24 • Public • Published

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

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

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

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 (val, done) -> done(err, result) Stop if one of the method has an error in the callback

chain = (funcs) ->
  (val, done) ->
    if funcs.length == 0
      done nullval
    else
      funcs[0val(err, res) =>
        if err?
          done errres
        else
          chain(funcs[1..])(resdone)

avoid

Wrap a void returning function to make it callable in a chain

avoid = (func) ->
  (params, done) ->
    func(params)
    done nullparams 

parallel

Execute asynchronous functions which take same inputs

parallel = (funcs) ->
  (params, done) -> 
    
    = 0
    errors = []
    results = []
    tempDone = (err, result) ->
      i++
      errors.push(err) if err?
      results.push result
      if i == funcs.length
        error = if errors.length > 0  then errors else null
        done errorresults
 
    funcs.forEach (func) ->
      func paramstempDone

getRequestParams

getRequestParams = (req) -> 
  params = {}
  for field in ["body""query""params"]
    if req[field]?
      params = _.extend paramsreq[field]
  params.user = req.user if req.user?
  params

webService

webService = (method, contentType = "application/json") ->
  (req, res) ->
    method getRequestParams(req)(err, data) ->
      if err? 
        res.send 500
      else
        if contentType == "application/json"
          res.send data
        else
          res.contentType contentType
          res.end data.toString()

webPage

webPage = (template, method) ->
  (req, res) ->
    if not method? and template?
      data = getRequestParams(req)
      data.__ = 
        template : template
      res.render templatedata 
    else
      method getRequestParams(req)(err, data) ->
        if err?
          res.send 500
        else
          data = {} if not data?
          data.user = req.user if req.user? and not data.user?
          data.__ = 
            template : template
          res.render templatedata

memoryCache

memoize = (method, seconds) ->
  cache = {}
 
  (params, done) ->
    hash = JSON.stringify(params)
    if cache[hash]? and cache[hash].expiration > new Date()
      done nullcache[hash].result
    else
      method params(err, res) ->
        if not err?
          cache[hash=
            result : res
            expiration : (new Date()).setSeconds((new Date()).getSeconds() + 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

Readme

Keywords

none

Package Sidebar

Install

npm i barefoot

Weekly Downloads

2

Version

0.0.24

License

BSD

Last publish

Collaborators

  • matttam
  • noisysocks