Skip to content

Commit 28b8651

Browse files
MrLeebodrwpow
authored andcommitted
Alphabetically sort output from transforms
1 parent d32cadd commit 28b8651

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

src/transform/headers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function transformHeaderObjMap(
1212
): string {
1313
let output = "";
1414

15-
for (const k of Object.keys(headerMap)) {
16-
const v = headerMap[k];
15+
const sortedEntries = Object.entries(headerMap).sort(([a], [b]) => a.localeCompare(b, "en"));
16+
for (const [k, v] of sortedEntries) {
1717
if (!v.schema) continue;
1818

1919
if (v.description) output += comment(v.description);

src/transform/parameters.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ export function transformParametersArray(
1919

2020
// sort into map
2121
const mappedParams: Record<string, Record<string, ParameterObject>> = {};
22-
for (const paramObj of parameters as any[]) {
23-
if (paramObj.$ref && globalParameters) {
24-
const paramName = paramObj.$ref.split('["').pop().replace(PARAM_END_RE, ""); // take last segment
22+
for (const paramObj of parameters) {
23+
if ("$ref" in paramObj && paramObj.$ref && globalParameters) {
24+
// take last segment
25+
let paramName = paramObj.$ref.split('["').pop();
26+
paramName = String(paramName).replace(PARAM_END_RE, "");
27+
2528
if (globalParameters[paramName]) {
26-
const reference = globalParameters[paramName] as any;
29+
const reference = globalParameters[paramName];
30+
if (!reference.in) continue;
31+
2732
if (!mappedParams[reference.in]) mappedParams[reference.in] = {};
2833
switch (ctx.version) {
2934
case 3: {
@@ -36,7 +41,7 @@ export function transformParametersArray(
3641
case 2: {
3742
mappedParams[reference.in][reference.name || paramName] = {
3843
...reference,
39-
$ref: paramObj.$ref,
44+
...("$ref" in paramObj ? { $ref: paramObj.$ref } : null),
4045
};
4146
break;
4247
}
@@ -45,6 +50,7 @@ export function transformParametersArray(
4550
continue;
4651
}
4752

53+
if (!("in" in paramObj)) continue;
4854
if (!paramObj.in || !paramObj.name) continue;
4955
if (!mappedParams[paramObj.in]) mappedParams[paramObj.in] = {};
5056
mappedParams[paramObj.in][paramObj.name] = paramObj;

src/transform/paths.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export function transformPathsObj(paths: Record<string, PathItemObject>, options
3333

3434
let output = "";
3535

36-
for (const [url, pathItem] of Object.entries(paths)) {
36+
const sortedEntries = Object.entries(paths).sort(([a], [b]) => a.localeCompare(b, "en"));
37+
for (const [url, pathItem] of sortedEntries) {
3738
if (pathItem.description) output += comment(pathItem.description); // add comment
3839

3940
if (pathItem.$ref) {

src/transform/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { transformSchemaObj } from "./schema.js";
55
export function transformRequestBodies(requestBodies: Record<string, RequestBody>, ctx: GlobalContext) {
66
let output = "";
77

8-
for (const [name, requestBody] of Object.entries(requestBodies)) {
8+
const sortedEntries = Object.entries(requestBodies).sort(([a], [b]) => a.localeCompare(b, "en"));
9+
for (const [name, requestBody] of sortedEntries) {
910
if (requestBody && requestBody.description) output += ` ${comment(requestBody.description)}`;
1011
output += ` "${name}": {\n ${transformRequestBodyObj(requestBody, ctx)}\n }\n`;
1112
}

src/transform/responses.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export function transformResponsesObj(responsesObj: Record<string, any>, ctx: Gl
1515

1616
let output = "";
1717

18-
for (const httpStatusCode of Object.keys(responsesObj)) {
18+
const sortedEntries = Object.entries(responsesObj).sort(([a], [b]) => a.localeCompare(b, "en"));
19+
for (const [httpStatusCode, response] of sortedEntries) {
1920
const statusCode = Number(httpStatusCode) || `"${httpStatusCode}"`; // don’t surround w/ quotes if numeric status code
20-
const response = responsesObj[httpStatusCode];
2121
if (response.description) output += comment(response.description);
2222

2323
if (response.$ref) {

src/transform/schema.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ function hasDefaultValue(node: any): boolean {
2727
export function transformSchemaObjMap(obj: Record<string, any>, options: TransformSchemaObjOptions): string {
2828
let output = "";
2929

30-
for (const k of Object.keys(obj)) {
31-
const v = obj[k];
32-
30+
const sortedEntries = Object.entries(obj).sort(([a], [b]) => a.localeCompare(b, "en"));
31+
for (const [k, v] of sortedEntries) {
3332
// 1. Add comment in jsdoc notation
3433
const comment = prepareComment(v);
3534
if (comment) output += comment;

0 commit comments

Comments
 (0)