Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/lib/content/commands/npm-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ If only a single string field for a single version is output, then it will not b
If the field is an object, it will be output as a JavaScript object literal.

If the `--json` flag is given, the outputted fields will be JSON.
The output is always an array, even if only a single version matches.

If the version range matches multiple versions then each printed value will be prefixed with the version it applies to.

Expand Down
8 changes: 1 addition & 7 deletions lib/commands/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,12 @@ class View extends BaseCommand {
})

if (json) {
// TODO(BREAKING_CHANGE): all unwrapping should be removed.
// Users should know based on their arguments if they can expect an array or an object.
// And this unwrapping can break that assumption.
// e.g. `npm view abbrev@^2` should always return an array, but currently since there is only one version matching `^2` this will return a single object instead.
// Users can expect an array .
const first = Object.keys(res[0] || {})
const jsonRes = first.length === 1 ? res.map(m => m[first[0]]) : res
if (jsonRes.length === 0) {
return
}
if (jsonRes.length === 1) {
return jsonRes[0]
}
return jsonRes
}

Expand Down
Loading
Loading