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

callback-reduce

v1.1.0

Published

Callbacks made reducible

Downloads

38

Readme

callback-reduce

Build Status

Reducers is a great abstraction for working with data sequences eventual or not. In fact it's great because API for working with data is same map/reduce regardless of weather it's sync or async.

var reduce = require("reducers/reduce")
var filter = require("reducers/filter")
var map = require("reducers/map")

// Sync
reduce(filte(map(array, JSON.parse), isCached), accumulate)

// Async
reduce(filte(map(stream, JSON.parse), isCached), accumulate)

Another cool concept about reducers is that allows you to work with non sequnce values with the same API. It just treats atomic vaules as sequences of themself.

var reduce = require("reducers/reduce")
function sum(sequence) {
  return reduce(sequence, function(result, item) {
    return result + item
  }, 0)
}

reduce(sum(15), console.log, "=>")            // => 15
reduce(sum([ 15, 3, 7 ]), console.log, "=>")  // => 25

But there are bunch of async APIs around that are designed in terms of: do action provide a callback, where you'll handle either error or value.

var fs = require("fs")

function getPackageName(path, callback) {
  fs.readFile(path, function(error, buffer) {
    if (error) return callback(error)
    var json = JSON.parse(buffer.toString())
    callback(error, json.name)
  })
}

getPackageName("./package.json", console.log) // => "callback-reduce"

Which is ok but not very composable and there is no simple to transform data. But with reducers we like the fact of unified API that works on the data structures regardless of their nature or timing. This libarray lets you get reducible callbacks for callback styled functions!

var fs = require("fs")
var map = require("reducers/map")
var callback = require("callback-reduce")
var reduce = require("reducers/reduce")

var content = callback(fs.readFile, "./package.json")
var json = map(map(content, String), JSON.parse)
var name = map(json, function($) { return $.name })

reduce(name, console.log, "=>")     // => "callback-reduce"

And of course it's lazy and compasable with rest of the API that reducers provide. For more complicated example see:

var print = require("reducers/debug/print")

var fs = require("fs")
var path = require("path")

var callback = require("./callback")
var expand = require("reducers/expand")
var map = require("reducers/map")
var filter = require("reducers/filter")
var cache = require("reducers/cache")
var concat = require("reducers/concat")
var reduce = require("reducers/reduce")


function lstree(root) {
  // Get sequence of directory entries, also we cache it as we read
  // from it several times.
  var entries = cache(callback(fs.readdir, root))
  // Resolve entries to the current path.
  var paths = map(entries, path.join.bind(path, root))
  // Expand sequence of paths, to associated stats. Unfortunately node does not
  // keeps path info in the stats so we need to hack this up. Otherwise it would
  // have being just: var stats = expand(paths, callback.bind(fs, fs.stats))
  var stats = expand(paths, function(path) {
    return map(callback(fs.stat, path), function(stats) {
      stats.toString = path.toString.bind(path)
      return stats
    })
  })
  // Filter & map file paths.
  var files = map(filter(stats, function($) { return $.isFile() }), String)
  // Filter & map directory paths.
  var dirs = map(filter(stats, function($) { return $.isDirectory() }), String)

  // Return concatination of lstree-s for each dir, files and given path.
  return concat(expand(dirs, lstree), files, root)
}

Install

npm install callback-reduce