Skip to content

Commit

Permalink
feat: sorting the attributes to default ordering after inject method
Browse files Browse the repository at this point in the history
  • Loading branch information
John Carmichael committed Feb 4, 2024
1 parent eb5eab7 commit e3c487a
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 41 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ url: <$ host $>


## Changelog
- 2023/12/28 4.11.0: feat: Order attributes after running inject, [gh issue](https://github.com/j-d-carmichael/boats/issues/82)
- 2023/12/28 4.10.2: fix: init files oa3 auto indexer was missing
- 2023/12/28 4.10.0: feat: init files now use the own lightweight files over the files used for testing purposes.
- 2023/12/28 4.9.4: fix: readme.md link
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "boats",
"version": "4.10.2",
"version": "4.11.0",
"description": "Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.",
"keywords": [
"asyncapi",
Expand Down Expand Up @@ -64,10 +64,10 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "11.1.0",
"@apidevtools/swagger-parser": "10.1.0",
"@asyncapi/parser": "3.0.2",
"@asyncapi/parser": "3.0.5",
"commander": "11.1.0",
"deepmerge": "4.3.1",
"dotenv": "16.3.1",
"dotenv": "16.4.1",
"fs-extra": "11.2.0",
"inquirer": "8.2.6",
"js-yaml": "4.1.0",
Expand Down
80 changes: 80 additions & 0 deletions src/SortAttributes.ts
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();
Loading

0 comments on commit e3c487a

Please sign in to comment.