Home > Mobile >  npm view: How sort package's versions by date-time?
npm view: How sort package's versions by date-time?

Time:06-16

npm view command shows data about a package and prints it to stdout.

How can I sort package json output versions by datetime?

npm view @act/act-qa-task-thycotic time --json
{
  "1.3.0-pr.515714": "2022-06-15T14:29:51.680Z",
  "created": "2022-06-14T23:22:13.692Z",
  "1.3.0-pr.515829": "2022-06-15T17:06:02.323Z",
  "1.3.0-pr.515697": "2022-06-15T13:45:24.534Z",
  "1.3.0-pr.515333": "2022-06-14T23:26:32.198Z",
  "1.3.0-pr.515861": "2022-06-15T17:30:36.429Z",
  "1.3.0-pr.515365": "2022-06-14T23:59:47.488Z",
  "1.3.0-pr.515497": "2022-06-15T02:15:01.895Z",
  "1.3.0-pr.515326": "2022-06-14T23:22:13.692Z",
  "1.3.0-pr.515479": "2022-06-15T02:01:56.968Z",
  "1.3.0": "2022-06-14T23:30:57.373Z",
  "1.3.0-pr.515482": "2022-06-15T02:05:14.094Z",
  "modified": "2022-06-15T17:30:36.429Z"
}

Is using jq the best choice?

npm view @act/act-qa-task-thycotic time --json | jq '.[]'

"2022-06-15T14:29:51.680Z"
"2022-06-14T23:22:13.692Z"
"2022-06-15T17:06:02.323Z"
"2022-06-15T13:45:24.534Z"
"2022-06-14T23:26:32.198Z"
"2022-06-15T17:30:36.429Z"
"2022-06-14T23:59:47.488Z"
"2022-06-15T02:15:01.895Z"
"2022-06-14T23:22:13.692Z"
"2022-06-15T02:01:56.968Z"
"2022-06-14T23:30:57.373Z"
"2022-06-15T02:05:14.094Z"
"2022-06-15T17:30:36.429Z"

How to sort package versions by date-time?

CodePudding user response:

jq is at least a very good choice. If all the timestamps are formatted the same way, especially in the same timezone, you can sort_by their string representation:

jq -r 'to_entries | sort_by(.value)[].key'
created
1.3.0-pr.515326
1.3.0-pr.515333
1.3.0
1.3.0-pr.515365
1.3.0-pr.515479
1.3.0-pr.515482
1.3.0-pr.515497
1.3.0-pr.515697
1.3.0-pr.515714
1.3.0-pr.515829
1.3.0-pr.515861
modified

Demo

  • Related