mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-06 05:10:20 +03:00
feat: move sort by prop to utils functions
This commit is contained in:
parent
a7b75f320f
commit
62b2c864dc
|
@ -12,6 +12,7 @@ import {
|
|||
SECURITY_DEFINITIONS_COMPONENT_NAME,
|
||||
setSecuritySchemePrefix,
|
||||
JsonPointer,
|
||||
alphabeticallyByProp,
|
||||
} from '../utils';
|
||||
import { MarkdownRenderer } from './MarkdownRenderer';
|
||||
import { GroupModel, OperationModel } from './models';
|
||||
|
@ -188,9 +189,11 @@ export class MenuBuilder {
|
|||
|
||||
/**
|
||||
* Returns array of Operation items for the tag
|
||||
* @param parser
|
||||
* @param parent parent OperationsGroup
|
||||
* @param tag tag info returned from `getTagsWithOperations`
|
||||
* @param depth items depth
|
||||
* @param options - normalized options
|
||||
*/
|
||||
static getOperationsItems(
|
||||
parser: OpenAPIParser,
|
||||
|
@ -209,9 +212,11 @@ export class MenuBuilder {
|
|||
operation.depth = depth;
|
||||
res.push(operation);
|
||||
}
|
||||
|
||||
if (options.sortOperationsAlphabetically) {
|
||||
res.sort((a, b) => a.name.localeCompare(b.name));
|
||||
res.sort(alphabeticallyByProp<OperationModel>('name'));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,3 +8,4 @@ export * from './dom';
|
|||
export * from './decorators';
|
||||
export * from './debug';
|
||||
export * from './memoize';
|
||||
export * from './sort';
|
||||
|
|
21
src/utils/sort.ts
Normal file
21
src/utils/sort.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Function that returns a comparator for sorting objects by some specific key alphabetically.
|
||||
*
|
||||
* @param {String} property key of the object to sort, if starts from `-` - reverse
|
||||
*/
|
||||
export function alphabeticallyByProp<T>(property: string): (a: T, b: T) => number {
|
||||
let sortOrder = 1;
|
||||
|
||||
if (property[0] === '-') {
|
||||
sortOrder = -1;
|
||||
property = property.substr(1);
|
||||
}
|
||||
|
||||
return (a: T, b: T) => {
|
||||
if (sortOrder == -1) {
|
||||
return b[property].localeCompare(a[property]);
|
||||
} else {
|
||||
return a[property].localeCompare(b[property]);
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user