node package manager
Orgs are free. Discover, share, and reuse code in your team. Create a free org »



Run a reduce over a collection of files, and save the result in another file.

If there is not more recent mtime than the reduction file, the reduce won't run.

example - npm reverse deps

Iterate over all npm docs in your npm cache, and construct a table of reverse dependencies. ({$dependencie: {$dependant: count}} - count is the number of versions of $dependant than depend on $dependency)

var mtr = require('mtime-reduce')
var reduce = mtr({
  //selection of files you wish to reduce over. 
  glob: process.env.HOME +'/.npm/*/*package.json',
  //file to save the accumulation in 
  filename: '/tmp/reverse-deps.json',
  encoding: 'json', //DON'T FORGET THIS IF IT'S JSON! 
  //initial value passed to reduce function. 
  initial: {},
  //a reduce function - the same api as Array#reduce 
  reduce: function (acc, pkg) {
    for(var dep in pkg.dependencies || {}) {
      acc[dep] = acc[dep] || {}
      acc[dep][] = (acc[dep][] || 0) + 1
    return acc
//an async reduce function is returned. 
//pass it a callback! 
reduce (function (err, data) {
  if(err) throw err
  console.log(JSON.stringify(data, null, 2))