2021-07-22 23:04:56 +00:00
|
|
|
import { GrpcRequest, isGrpcRequest } from '../models/grpc-request';
|
|
|
|
import { isRequest, Request } from '../models/request';
|
|
|
|
import { isRequestGroup, RequestGroup } from '../models/request-group';
|
2020-12-01 00:37:57 +00:00
|
|
|
import {
|
|
|
|
HTTP_METHODS,
|
|
|
|
SORT_CREATED_ASC,
|
|
|
|
SORT_CREATED_DESC,
|
|
|
|
SORT_HTTP_METHOD,
|
2021-08-05 17:38:58 +00:00
|
|
|
SORT_MODIFIED_ASC,
|
|
|
|
SORT_MODIFIED_DESC,
|
2020-12-01 00:37:57 +00:00
|
|
|
SORT_NAME_ASC,
|
|
|
|
SORT_NAME_DESC,
|
|
|
|
SORT_TYPE_ASC,
|
|
|
|
SORT_TYPE_DESC,
|
|
|
|
} from './constants';
|
|
|
|
|
|
|
|
type SortableModel = Request | RequestGroup | GrpcRequest;
|
2021-08-05 17:38:58 +00:00
|
|
|
type SortFunction<SortableType> = (a: SortableType, b: SortableType) => number;
|
2020-12-01 00:37:57 +00:00
|
|
|
|
2021-08-26 05:43:56 +00:00
|
|
|
export const ascendingNameSort: SortFunction<{name: string}> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
const descendingNameSort: SortFunction<{name: string}> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
return b.name.localeCompare(a.name);
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
const createdFirstSort: SortFunction<{created: number}> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
if (a.created === b.created) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.created < b.created ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
const createdLastSort: SortFunction<{created: number}> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
if (a.created === b.created) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.created > b.created ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
const ascendingModifiedSort: SortFunction<{lastModifiedTimestamp: number}> = (a, b) => {
|
|
|
|
if (a.lastModifiedTimestamp === b.lastModifiedTimestamp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.lastModifiedTimestamp < b.lastModifiedTimestamp ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
const descendingModifiedSort: SortFunction<{lastModifiedTimestamp: number}> = (a, b) => {
|
|
|
|
if (a.lastModifiedTimestamp === b.lastModifiedTimestamp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.lastModifiedTimestamp > b.lastModifiedTimestamp ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
const httpMethodSort: SortFunction<Pick<SortableModel, 'type' | 'metaSortKey' | '_id'>> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
// Sort Requests and GrpcRequests to top, in that order
|
|
|
|
if (a.type !== b.type) {
|
|
|
|
if (isRequest(a) || isRequest(b)) {
|
|
|
|
return isRequest(a) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isGrpcRequest(a) || isGrpcRequest(b)) {
|
|
|
|
return isGrpcRequest(a) ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort Requests by HTTP method
|
|
|
|
if (isRequest(a)) {
|
|
|
|
const aIndex = HTTP_METHODS.indexOf(a.method);
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2020-12-01 00:37:57 +00:00
|
|
|
const bIndex = HTTP_METHODS.indexOf(b.method);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-12-01 00:37:57 +00:00
|
|
|
if (aIndex !== bIndex) {
|
|
|
|
return aIndex < bIndex ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by ascending method name if comparing two custom methods
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2020-12-01 00:37:57 +00:00
|
|
|
if (aIndex === -1 && a.method.localeCompare(b.method) !== 0) {
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2020-12-01 00:37:57 +00:00
|
|
|
return a.method.localeCompare(b.method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by metaSortKey if comparing two Requests with the same method,
|
|
|
|
// two GrpcRequests, or two RequestGroups
|
|
|
|
return metaSortKeySort(a, b);
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
const ascendingTypeSort: SortFunction<Pick<SortableModel, 'type' | 'metaSortKey' | '_id'>> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
if (a.type !== b.type && (isRequestGroup(a) || isRequestGroup(b))) {
|
|
|
|
return isRequestGroup(b) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return metaSortKeySort(a, b);
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
const descendingTypeSort: SortFunction<Pick<SortableModel, 'type' | 'metaSortKey' | '_id'>> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
if (a.type !== b.type && (isRequestGroup(a) || isRequestGroup(b))) {
|
|
|
|
return isRequestGroup(a) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return metaSortKeySort(a, b);
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
export const metaSortKeySort: SortFunction<Pick<SortableModel, '_id' | 'metaSortKey'>> = (a, b) => {
|
2020-12-01 00:37:57 +00:00
|
|
|
if (a.metaSortKey === b.metaSortKey) {
|
|
|
|
return a._id > b._id ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.metaSortKey < b.metaSortKey ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
export const ascendingNumberSort: SortFunction<number> = (a, b) => {
|
2021-03-10 04:33:38 +00:00
|
|
|
return a < b ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
export const descendingNumberSort: SortFunction<number> = (a, b) => {
|
2021-03-10 04:33:38 +00:00
|
|
|
return ascendingNumberSort(b, a);
|
|
|
|
};
|
|
|
|
|
2021-08-05 17:38:58 +00:00
|
|
|
export const sortMethodMap = {
|
2020-12-01 00:37:57 +00:00
|
|
|
[SORT_NAME_ASC]: ascendingNameSort,
|
|
|
|
[SORT_NAME_DESC]: descendingNameSort,
|
|
|
|
[SORT_CREATED_ASC]: createdFirstSort,
|
|
|
|
[SORT_CREATED_DESC]: createdLastSort,
|
2021-08-05 17:38:58 +00:00
|
|
|
[SORT_MODIFIED_ASC]: ascendingModifiedSort,
|
|
|
|
[SORT_MODIFIED_DESC]: descendingModifiedSort,
|
2020-12-01 00:37:57 +00:00
|
|
|
[SORT_HTTP_METHOD]: httpMethodSort,
|
|
|
|
[SORT_TYPE_DESC]: descendingTypeSort,
|
|
|
|
[SORT_TYPE_ASC]: ascendingTypeSort,
|
|
|
|
};
|