Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

barefoot

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

barefoot

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

  • 0.0.24
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
2
Weekly downloads
 
Created
Source

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

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

avoid

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

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

parallel

Execute asynchronous functions which take same inputs

parallel = (funcs) ->
  (params, done) -> 
    
    i = 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 error, results

    funcs.forEach (func) ->
      func params, tempDone

getRequestParams

getRequestParams = (req) -> 
  params = {}
  for field in ["body", "query", "params"]
    if req[field]?
      params = _.extend params, req[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 template, data 
    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 template, data

memoryCache

memoize = (method, seconds) ->
  cache = {}

  (params, done) ->
    hash = JSON.stringify(params)
    if cache[hash]? and cache[hash].expiration > new Date()
      done null, cache[hash].result
    else
      method params, (err, res) ->
        if not err?
          cache[hash] =
            result : res
            expiration : (new Date()).setSeconds((new Date()).getSeconds() + seconds)

        done err, res

Export public methods

module.exports =
  toDictionary : toDictionary
  has          : has
  amap         : amap
  chain        : chain
  avoid        : avoid
  parallel     : parallel
  webService   : webService
  webPage      : webPage
  memoize      : memoize

FAQs

Package last updated on 19 Nov 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc