0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

feat: allow order packages via on web #1163

add new param on web sort_packages. options asc or desc
This commit is contained in:
Juan Picado @jotadeveloper 2019-02-07 21:18:45 +01:00
parent 10370c6eeb
commit 49c6f0353e
No known key found for this signature in database
GPG key ID: 18AC54485952D158
4 changed files with 59 additions and 10 deletions

View file

@ -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:

View file

@ -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));
});
});

View file

@ -412,13 +412,11 @@ export function fileExists(path: string): boolean {
}
}
export function sortByName(packages: Array<any>): string[] {
export function sortByName(packages: Array<any>, 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;
});
}

View file

@ -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');