From 49c6f0353e7ec2ed58e0af4b9102b6080d5b6eac Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Thu, 7 Feb 2019 21:18:45 +0100 Subject: [PATCH] feat: allow order packages via on web #1163 add new param on web sort_packages. options asc or desc --- conf/default.yaml | 5 ++-- src/api/web/endpoint/package.js | 10 +++++++- src/lib/utils.js | 10 +++----- test/unit/api/utils.spec.js | 44 ++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/conf/default.yaml b/conf/default.yaml index 4b721a700..4f6efca28 100644 --- a/conf/default.yaml +++ b/conf/default.yaml @@ -12,9 +12,10 @@ storage: ./storage plugins: ./plugins web: - # WebUI is enabled as default, if you want disable it, just uncomment this line - #enable: false title: Verdaccio + # gravatar: true + # by default packages are ordercer ascendant (asc|desc) + # sort_packages: asc auth: htpasswd: diff --git a/src/api/web/endpoint/package.js b/src/api/web/endpoint/package.js index 8c5cbc501..21e4f028e 100644 --- a/src/api/web/endpoint/package.js +++ b/src/api/web/endpoint/package.js @@ -12,6 +12,10 @@ import type { Router } from 'express'; import type { IAuth, $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageHandler, $SidebarPackage } from '../../../../types'; import type { Config } from '@verdaccio/types'; +const getOrder = order => { + return order === 'asc'; +}; + function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth, config: Config) { const can = allow(auth); @@ -53,7 +57,11 @@ function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth, return permissions; } - next(sortByName(await processPermissionsPackages(packages))); + const { web } = config; + // $FlowFixMe + const order: boolean = web.sort_packages ? getOrder(web.sort_packages) : true; + + next(sortByName(await processPermissionsPackages(packages), order)); }); }); diff --git a/src/lib/utils.js b/src/lib/utils.js index 34af6f5ed..d92c4b532 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -412,13 +412,11 @@ export function fileExists(path: string): boolean { } } -export function sortByName(packages: Array): string[] { +export function sortByName(packages: Array, orderAscending: boolean | void = true): string[] { return packages.sort(function(a, b) { - if (a.name < b.name) { - return -1; - } else { - return 1; - } + const comparatorNames = a.name.toLowerCase() < b.name.toLowerCase(); + + return orderAscending ? (comparatorNames ? -1 : 1) : comparatorNames ? 1 : -1; }); } diff --git a/test/unit/api/utils.spec.js b/test/unit/api/utils.spec.js index 3d884389b..f8cd9bcd2 100644 --- a/test/unit/api/utils.spec.js +++ b/test/unit/api/utils.spec.js @@ -12,7 +12,8 @@ import { getVersion, normalizeDistTags, getWebProtocol, - getVersionFromTarball + getVersionFromTarball, + sortByName } from '../../../src/lib/utils'; import { DIST_TAGS } from '../../../src/lib/constants'; import Logger, { setup } from '../../../src/lib/logger'; @@ -46,6 +47,47 @@ describe('Utilities', () => { const cloneMetadata = (pkg = metadata) => Object.assign({}, pkg); describe('API utilities', () => { + describe('Sort packages', () => { + const packages = [ + { + name: 'ghc' + }, + { + name: 'abc' + }, + { + name: 'zxy' + } + ]; + test('should order ascending', () => { + expect(sortByName(packages)).toEqual([ + { + name: 'abc' + }, + { + name: 'ghc' + }, + { + name: 'zxy' + } + ]); + }); + + test('should order descending', () => { + expect(sortByName(packages, false)).toEqual([ + { + name: 'zxy' + }, + { + name: 'ghc' + }, + { + name: 'abc' + } + ]); + }); + }); + describe('getWebProtocol', () => { test('should handle undefined header', () => { expect(getWebProtocol(undefined, 'http')).toBe('http');