all files / mlpm/lib/commands/ search.js

100% Statements 27/27
100% Branches 6/6
100% Functions 6/6
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                                                 
'use strict'
 
var log = require('winston')
var _ = require('lodash')
var columnify = require('columnify')
var api = require('../api.js')
 
function transformMetadata(result) {
  var metadata = result.metadata
  result.metadata = {}
 
  _.each(metadata, function(obj) {
    var key = _.without(_.keys(obj), 'metadata-type')[0]
      , newKey = key.replace('{http://mlpm.org/ns}', '')
      , type = obj[ 'metadata-type' ]
      , value = obj[ key ]
 
    if (! result.metadata[ newKey ] ) {
      result.metadata[ newKey ] = { 'metadata-type': type, values: [] }
    }
 
    result.metadata[ newKey ].values.push(value)
  })
}
 
function getValue(result, key) {
  return _.last( result.metadata[ key ].values )
}
 
function selectMetadata(result) {
  transformMetadata(result)
    return {
      name:        getValue(result, 'name'),
      description: getValue(result, 'description'),
      // author:      getValue(result, 'author'),
      version:     getValue(result, 'version'),
      modified:    getValue(result, 'modified').split('T')[0]
    }
}
 
function search(args) {
  api.search(args.query, function(err, data) {
    if (err) return log.error(err)
 
    var out = _.compose(log.info, columnify)
 
    out( _.map(data.results, selectMetadata) )
 
    // note if there are more results
    if ( data.total > data['page-length'] ) {
      log.info('(page 1 of ' + ((data.total / data['page-length'])|0) + ')')
    }
  })
}
 
search.usage = 'mlpm search [query terms]'
 
module.exports.command = search