diff --git a/src/services/MenuBuilder.ts b/src/services/MenuBuilder.ts index 0021d361..22caa2c9 100644 --- a/src/services/MenuBuilder.ts +++ b/src/services/MenuBuilder.ts @@ -12,6 +12,7 @@ import { SECURITY_DEFINITIONS_COMPONENT_NAME, setSecuritySchemePrefix, JsonPointer, + alphabeticallyByProp, } from '../utils'; import { MarkdownRenderer } from './MarkdownRenderer'; import { GroupModel, OperationModel } from './models'; @@ -130,9 +131,11 @@ export class MenuBuilder { /** * Returns array of OperationsGroup items for the tags of the group or for all tags + * @param parser * @param tagsMap tags info returned from `getTagsWithOperations` * @param parent parent item * @param group group which this tag belongs to. if not provided gets all tags + * @param options normalized options */ static getTagsItems( parser: OpenAPIParser, @@ -183,14 +186,21 @@ export class MenuBuilder { res.push(item); } + + if (options.sortTagsAlphabetically) { + res.sort(alphabeticallyByProp('name')); + } + return res; } /** * 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,6 +219,11 @@ export class MenuBuilder { operation.depth = depth; res.push(operation); } + + if (options.sortOperationsAlphabetically) { + res.sort(alphabeticallyByProp('name')); + } + return res; } diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 66a0cf61..55bcba44 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -18,6 +18,8 @@ export interface RedocRawOptions { requiredPropsFirst?: boolean | string; sortPropsAlphabetically?: boolean | string; sortEnumValuesAlphabetically?: boolean | string; + sortOperationsAlphabetically?: boolean | string; + sortTagsAlphabetically?: boolean | string; noAutoAuth?: boolean | string; nativeScrollbars?: boolean | string; pathInMiddlePanel?: boolean | string; @@ -204,6 +206,8 @@ export class RedocNormalizedOptions { requiredPropsFirst: boolean; sortPropsAlphabetically: boolean; sortEnumValuesAlphabetically: boolean; + sortOperationsAlphabetically: boolean; + sortTagsAlphabetically: boolean; noAutoAuth: boolean; nativeScrollbars: boolean; pathInMiddlePanel: boolean; @@ -264,6 +268,8 @@ export class RedocNormalizedOptions { this.requiredPropsFirst = argValueToBoolean(raw.requiredPropsFirst); this.sortPropsAlphabetically = argValueToBoolean(raw.sortPropsAlphabetically); this.sortEnumValuesAlphabetically = argValueToBoolean(raw.sortEnumValuesAlphabetically); + this.sortOperationsAlphabetically = argValueToBoolean(raw.sortOperationsAlphabetically); + this.sortTagsAlphabetically = argValueToBoolean(raw.sortTagsAlphabetically); this.noAutoAuth = argValueToBoolean(raw.noAutoAuth); this.nativeScrollbars = argValueToBoolean(raw.nativeScrollbars); this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel); diff --git a/src/utils/index.ts b/src/utils/index.ts index dd19252f..c25ed970 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -8,3 +8,4 @@ export * from './dom'; export * from './decorators'; export * from './debug'; export * from './memoize'; +export * from './sort'; diff --git a/src/utils/sort.ts b/src/utils/sort.ts new file mode 100644 index 00000000..4c3536ad --- /dev/null +++ b/src/utils/sort.ts @@ -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(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]); + } + }; +}