diff options
author | isaacs <i@izs.me> | 2011-10-12 05:12:35 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2011-10-12 05:12:35 +0400 |
commit | 0ae422cbd64cad87f26093a9ff99078f3672eff3 (patch) | |
tree | 39bff27b02b10de8dc47096b6b9e8fb850e3c396 /lib | |
parent | 0b802d1ec9f617e0159353ceaa5f229b4a548ef7 (diff) |
Close #1493 Return values from npm.commands.view saner
Diffstat (limited to 'lib')
-rw-r--r-- | lib/view.js | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/lib/view.js b/lib/view.js index 0a5e5ba39..ea39b3e32 100644 --- a/lib/view.js +++ b/lib/view.js @@ -77,10 +77,24 @@ function view (args, silent, cb) { }) }) results = results.reduce(reducer, {}) - if (error || silent) cb(error, results) - else printData(results, cb) + var retval = results + if (args.length === 1 && args[0] === "") { + retval = cleanBlanks(retval) + log.silly(retval, "cleanup") + } + if (error || silent) cb(error, retval) + else printData(results, data._id, cb.bind(null, error, retval)) + }) +} + +function cleanBlanks (obj) { + var clean = {} + Object.keys(obj).forEach(function (version) { + clean[version] = obj[version][""] }) + return clean } + function reducer (l, r) { if (r) Object.keys(r).forEach(function (v) { l[v] = l[v] || {} @@ -90,16 +104,18 @@ function reducer (l, r) { }) return l } + // return whatever was printed function showFields (data, version, fields) { var o = {} - ;[data,version].forEach(function (s) { + ;[data, version].forEach(function (s) { Object.keys(s).forEach(function (k) { o[k] = s[k] }) }) - return search(o, fields.split("."), version._id, fields) + return search(o, fields.split("."), version.version, fields) } + function search (data, fields, version, title) { var field , tail = fields @@ -111,11 +127,11 @@ function search (data, fields, version, title) { o[version][title] = data return o } - var index = field.match(/(.+)\[([0-9]+)\]$/) + var index = field.match(/(.+)\[([^\]]+)\]$/) if (index) { field = index[1] index = index[2] - if (Array.isArray(data[field]) && index < data[field].length) { + if (data.field && data.field.hasOwnProperty(index)) { return search(data[field][index], tail, version, title) } else { field = field + "[" + index + "]" @@ -154,7 +170,7 @@ function search (data, fields, version, title) { return o } -function printData (data, cb) { +function printData (data, name, cb) { var versions = Object.keys(data) , msg = "" , showVersions = versions.length > 1 @@ -170,8 +186,9 @@ function printData (data, cb) { d = util.inspect(cleanup(data[v][f]), false, 5, true) } if (f && showFields) f += " = " - if (d.indexOf("\n") !== -1) f += "\n" - msg += (showVersions ? v + " " : "") + (showFields ? f : "") + d + "\n" + if (d.indexOf("\n") !== -1) d = "\n" + d + msg += (showVersions ? name + "@" + v + " " : "") + + (showFields ? f : "") + d + "\n" }) }) output = output || require("./utils/output.js") |