11cb0ef41Sopenharmony_ci'use strict' 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst npmFetch = require('npm-registry-fetch') 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_cimodule.exports = search 61cb0ef41Sopenharmony_cifunction search (query, opts) { 71cb0ef41Sopenharmony_ci return search.stream(query, opts).collect() 81cb0ef41Sopenharmony_ci} 91cb0ef41Sopenharmony_cisearch.stream = searchStream 101cb0ef41Sopenharmony_cifunction searchStream (query, opts = {}) { 111cb0ef41Sopenharmony_ci opts = { 121cb0ef41Sopenharmony_ci detailed: false, 131cb0ef41Sopenharmony_ci limit: 20, 141cb0ef41Sopenharmony_ci from: 0, 151cb0ef41Sopenharmony_ci quality: 0.65, 161cb0ef41Sopenharmony_ci popularity: 0.98, 171cb0ef41Sopenharmony_ci maintenance: 0.5, 181cb0ef41Sopenharmony_ci ...opts.opts, // this is to support the cli's --searchopts parameter 191cb0ef41Sopenharmony_ci ...opts, 201cb0ef41Sopenharmony_ci } 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci switch (opts.sortBy) { 231cb0ef41Sopenharmony_ci case 'optimal': { 241cb0ef41Sopenharmony_ci opts.quality = 0.65 251cb0ef41Sopenharmony_ci opts.popularity = 0.98 261cb0ef41Sopenharmony_ci opts.maintenance = 0.5 271cb0ef41Sopenharmony_ci break 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci case 'quality': { 301cb0ef41Sopenharmony_ci opts.quality = 1 311cb0ef41Sopenharmony_ci opts.popularity = 0 321cb0ef41Sopenharmony_ci opts.maintenance = 0 331cb0ef41Sopenharmony_ci break 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci case 'popularity': { 361cb0ef41Sopenharmony_ci opts.quality = 0 371cb0ef41Sopenharmony_ci opts.popularity = 1 381cb0ef41Sopenharmony_ci opts.maintenance = 0 391cb0ef41Sopenharmony_ci break 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci case 'maintenance': { 421cb0ef41Sopenharmony_ci opts.quality = 0 431cb0ef41Sopenharmony_ci opts.popularity = 0 441cb0ef41Sopenharmony_ci opts.maintenance = 1 451cb0ef41Sopenharmony_ci break 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci return npmFetch.json.stream('/-/v1/search', 'objects.*', 491cb0ef41Sopenharmony_ci { 501cb0ef41Sopenharmony_ci ...opts, 511cb0ef41Sopenharmony_ci query: { 521cb0ef41Sopenharmony_ci text: Array.isArray(query) ? query.join(' ') : query, 531cb0ef41Sopenharmony_ci size: opts.limit, 541cb0ef41Sopenharmony_ci from: opts.from, 551cb0ef41Sopenharmony_ci quality: opts.quality, 561cb0ef41Sopenharmony_ci popularity: opts.popularity, 571cb0ef41Sopenharmony_ci maintenance: opts.maintenance, 581cb0ef41Sopenharmony_ci }, 591cb0ef41Sopenharmony_ci mapJSON: (obj) => { 601cb0ef41Sopenharmony_ci if (obj.package.date) { 611cb0ef41Sopenharmony_ci obj.package.date = new Date(obj.package.date) 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci if (opts.detailed) { 641cb0ef41Sopenharmony_ci return obj 651cb0ef41Sopenharmony_ci } else { 661cb0ef41Sopenharmony_ci return obj.package 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci }, 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci ) 711cb0ef41Sopenharmony_ci} 72