Permalink
Browse files

Close #1493 Return values from npm.commands.view saner

  • Loading branch information...
isaacs committed Oct 12, 2011
1 parent 0b802d1 commit 0ae422cbd64cad87f26093a9ff99078f3672eff3
Showing with 37 additions and 9 deletions.
  1. +11 −0 doc/api/view.md
  2. +26 −9 lib/view.js
View
@@ -80,3 +80,14 @@ If multiple fields are requested, than each of them are prefixed with
the field name.
Console output can be disabled by setting the 'silent' parameter to true.
+
+## RETURN VALUE
+
+The data returned will be an object in this formation:
+
+ { <version>:
+ { <field>: <value>
+ , ... }
+ , ... }
+
+corresponding to the list of fields selected.
View
@@ -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")

0 comments on commit 0ae422c

Please sign in to comment.