-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sorting the attributes to default ordering after inject method
- Loading branch information
John Carmichael
committed
Feb 4, 2024
1 parent
eb5eab7
commit e3c487a
Showing
10 changed files
with
487 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import cloneObject from '@/cloneObject'; | ||
|
||
const defaultOpenAPIOrder = [ | ||
'tags', | ||
'summary', | ||
'description', | ||
'operationId', | ||
'security' | ||
]; | ||
|
||
const defaultAsyncAPIChannelOrder = [ | ||
'description', | ||
'publish', | ||
'subscribe' | ||
]; | ||
|
||
class SortAttributes { | ||
forOpenAPI (bundled: any, pathAttrOrderMap = defaultOpenAPIOrder) { | ||
const obj: any = cloneObject(bundled); | ||
for (const path in obj.paths) { | ||
for (const verb in obj.paths[path]) { | ||
obj.paths[path][verb] = this.orderObjectAttributes( | ||
this.customSort( | ||
Object.keys(obj.paths[path][verb]), | ||
pathAttrOrderMap | ||
), | ||
obj.paths[path][verb] | ||
); | ||
} | ||
} | ||
return obj; | ||
} | ||
|
||
forAsyncAPI (bundled: any, channelAttrOrderMap = defaultAsyncAPIChannelOrder) { | ||
const obj: any = cloneObject(bundled); | ||
for (const channel in obj.channels) { | ||
obj.channels[channel] = this.orderObjectAttributes( | ||
this.customSort( | ||
Object.keys(obj.channels[channel]), | ||
channelAttrOrderMap | ||
), | ||
obj.channels[channel] | ||
); | ||
} | ||
return obj; | ||
} | ||
|
||
orderObjectAttributes (inputArray: string[], inputObject: any) { | ||
if (!inputObject || typeof inputObject !== 'object') { | ||
return {}; | ||
} | ||
const orderedObject: any = {}; | ||
inputArray.forEach(attribute => { | ||
orderedObject[attribute] = inputObject[attribute]; | ||
}); | ||
return orderedObject; | ||
} | ||
|
||
customSort (input: string[], map: string[]): string[] { | ||
const sortedFinal = input.slice().sort(); | ||
const inputSet = new Set(input); | ||
const result = []; | ||
|
||
// Add elements in the "map" order given then add the remaining | ||
for (const item of map) { | ||
if (inputSet.has(item)) { | ||
result.push(item); | ||
} | ||
} | ||
for (const item of sortedFinal) { | ||
if (!result.includes(item) && inputSet.has(item)) { | ||
result.push(item); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
export default new SortAttributes(); |
Oops, something went wrong.