npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

future-io

v1.1.0

Published

IO Monad for JS

Downloads

3

Readme

future-io

NPM version Build status Dependencies

A fantasy-land compliant monadic IO library for Node.js.

Building a simple cli that tells you if a number is even can look something like this:

#!/usr/bin/node

const io = require('future-io')
const ioProcess = require('future-io/node/process')

const even = ioProcess.argv
  .map(argv => (parseInt(argv[2]) % 2) === 0)
  .chain(even => ioProcess.stdout.write('Is even: ' + even))

io.unsafePerform(even)

API

IO-returning functions

To get you started fast this library mimics the interface of the native node modules. It just returns io's instead of taking callbacks!

Getting complete coverage of all io related functionality in node is still a work in progress. If you're missing something, please feel free to open an issue or pull request.

At the moment the following modules are exported. For some recipes demonstrating their use check out the examples directory.

future-io/node/console

future-io/node/fs

future-io/node/module

future-io/node/process

future-io/node/child_process

performing IO actions

unsafePerform :: IO e a -> ()

This will execute the io. If the io represents an error value, this function will throw. If this is not what you want, use IO.prototype.catch.

IO methods

IO implements the fantasy-land Functor, Apply and Monad specifications.

IO.of :: a -> IO e a

IO.error :: e -> IO e a

IO.prototype.map :: IO e a ~> (a -> b) -> IO e b

IO.prototype.ap :: IO e (a -> b) ~> IO e a -> IO e b

IO.prototype.chain :: IO e a ~> (a -> IO e b) -> IO e b

IO.prototype.catch :: IO e a ~> (e -> IO f a) -> IO f a

Wrapping custom IO functions

Often you'll find the need to define your own io returning functions, or wrap functions provided by a library. Luckily, this is very simple:

  const io = require('future-io')

  // Wrapping a function performing some side effects in an IO.
  // The `customOperation` function should return a promise, task or plan value.
  const customIO = io.wrapMethod(
    'customOperation',
    customOperation
  )

Testing

Testing code performing a lot of IO is usually pretty painful. Not so when using future-io!

Simply use fakePerform instead of unsafePerform to execute your IO actions in tests. Now you can step through your io functions step by step, checking the arguments being passed in and choosing values to return.

fakePerform() return an object with three methods:

  • take(actionName): Proceed until the next action call. Assert it has type actionName. Return a promise containg the arguments the action is being called with as an array
  • put(returnValue): Call after a take resolves to send a return value. Also call this when not returning a value, to ensure execution of the IO continues.
  • error(ioError): Like put, but returns an error value.

When the io execution finishes, fakePerform triggers a last end action. This action is passed an ioError, if one exists, in the first-argument position.

Example using ava and async/await

import test from 'ava'
import io from 'future-io'
import ioProcess from 'future-io/node/process'

test('logging the current working directory', async t => {
  const io = ioProcess.cwd().chain(ioProcess.stdout.write)
  const { put, take } = fakePerform(io)
  const cwd = '/home/foo'

  await take('process.cwd')
  put(cwd)

  const [ loggedCwd ] = await take('process.stdout.write')
  t.is(loggedCwd, cwd)
  put()

  const [ ioError ] = await take('end')
  t.falsy(ioError)
})

Example using mocha and co

import co from 'co'
import io from 'future-io'
import assert from 'assert'

it('logs the current working directory', co.wrap(function* () {
  const io = ioProcess.cwd().chain(ioProcess.stdout.write)
  const { put, take } = fakePerform(io)
  const cwd = '/home/foo'

  yield take('process.cwd')
  yield put(cwd)

  const [ loggedCwd ] = yield take('process.stdout.write')
  assert.equal(loggedCwd, cwd)
  put()

  const [ ioError ] = yield take('end')
  assert.ifError(ioError)
}))