New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

spool

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spool

Thread arguments through pure functions.

latest
Source
npmnpm
Version
3.0.0
Version published
Maintainers
1
Created
Source

spool

Spool of thread

npm install spool

spool takes functions like this:

function add(a, b) {
  return a + b
}

function sub(a, b) {
  return a - b
}

add(sub(add(1, 9), 2), 5) // 13

and lets you call them like this:

import spool from 'spool'

const math = spool({
  add,
  sub,
})

math(1).add(9).sub(2).add(5).v // 13

That way, you can write pure functions, but call them in an OOP style... for some reason.

spool threads each return value as the first parameter to the next function. You can also use spoor to thread each return value as the last parameter to the next function.

You can even wrap mori. This is totally something you should do, and it results in a 100% better api:

import mori from 'mori'
import {spoor} from 'spool'

const m = spoor(mori)

m({foo: 1, bar: 2}).toClj().keys().toJs().v // [ 'foo', 'bar' ]

Here is another example:

import spool from 'spool'

client().artists(1).albums(2).tracks(3).get().v
// =>
// { path: ["/api/v1", "artists", 1, "albums", 2, "tracks", 3],
//   method: "GET"
// }

const client = spool({
  artists,
  albums,
  tracks,
  get,
  post,
  put,
}, {
  path: ['/api/v1'],
})

function albums(r, id) {
  return path(r, ['albums', id])
}

function artists(r, id) {
  return path(r, ['artists', id])
}

function tracks(r, id) {
  return path(r, ['tracks', id])
}

function get(r, id) {
  return method(r, 'GET')
}

function post(r, id) {
  return method(r, 'POST')
}

function put(r, id) {
  return method(r, 'PUT')
}

function method(r, method) {
  return merge(r, {method})
}

function path(r, p) {
  return nest(r, {path: p})
}

function headers(r, headers) {
  return merge(r, {headers})
}

function nest(...rs) {
  return {
    ...merge(...rs),
    path: [].concat(...rs.map(r => r.path).filter(p => p)),
  }
}

function merge(...rs) {
  return rs.reduce((o, r) => {
    for (const k in r) o[k] = _merge(o[k], r[k])
    return o
  }, {})
}

function _merge(a, b) {
  if (!a) return b
  if (!b) return a

  return typeof a === 'object' && typeof b === 'object' ? merge(a, b) : b
}

Keywords

thread

FAQs

Package last updated on 06 Sep 2015

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