commands.view shows awkward results object #1493

Closed
coolaj86 opened this Issue Oct 4, 2011 · 2 comments

2 participants

@coolaj86
npm.commands.view(["futures@> 2.0.0"], true, function (err, map, array) {
    console.log(err);
    console.log(map);
    console.log(array);
});

I would expect output that matches that of some of the other commands in a format such as this:

{
    "2.0.3": {
        name: "futures"
      , main: "index"
    }
  , "2.0.4": {
        ...
    }
}

and

[
    {
        version: "2.0.3"
      , name: "futures"
      , main: "index"
    }
  , {
        ...
    }
]

but instead the format is of this nature:

{
    'futures@2.0.2': {
        '': {
             name: 'futures'
           , version: '2.0.2'
           , main: 'index.js'
        }
    }
  , 'futures@2.0.3': {
             ...
    }
}

notice the empty strings as keys and the extraneous package name prefix

@coolaj86

here's a hotfix for it for those interested

(function () {
  var npm = require('npm')
    ;

  // https://github.com/isaacs/npm/issues/1493
  // this fixes an incongruent part of the API
  function hotFix1493(map) {
    var fixedMap = map
      , fixedArray = [];

    Object.keys(map).forEach(function (version) {
      var pkg = map[version][''];
      fixedMap[pkg.version] = pkg;
      delete map[version][''];
      delete map[version];
    });

    Object.keys(fixedMap).sort().forEach(function (version) {
      fixedArray.push(fixedMap[version]);
    });

    return fixedArray;
  }

  npm.load({}, function () {

    npm.commands.view(["futures@>= 2.3.0"], true, function (err, map, array) {
      array = hotFix1493(map);
      console.log(err);
      console.log(map);
      console.log(array);
    });
  });
}());
@isaacs
npm member

The issue is that view also takes additional arguments naming fields to show. Try it with npm.view(["futures@2.x", "dist.tarball", "dependencies"]), so it's saying "you didn't give me any fields to limit, so here's the object".

I guess the better API would be to return something like:

{ <version>: { <field1>: <value1>, ... }
, ...
}

So, if you don't specify any fields, it'll just give you the object, but if you do specify fields, then it'll filter and you'll only get the fields you specify.

@isaacs isaacs closed this in 0ae422c Oct 12, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment