Skip to content

Commit

Permalink
feat: add flag moveAllToComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Apr 6, 2024
1 parent d81adbd commit ab30af8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
23 changes: 18 additions & 5 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ user will only interact with this class. here we generate different kind of repo
<dl>
<dt><a href="#Rules">Rules</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="#DisableOptimizationFor">DisableOptimizationFor</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="#Options">Options</a> : <code>Object</code></dt>
<dd></dd>
</dl>
Expand Down Expand Up @@ -82,21 +84,21 @@ This function is used to get the optimized document after seeing the report.

## findAllComponents(optimizableComponentGroup) ⇒
**Kind**: global function
**Returns**: A list of all elements in optimization report.
**Returns**: A list of optimization report elements.

| Param | Description |
| --- | --- |
| optimizableComponentGroup | list of all AsyncAPI Specification-valid components. |
| optimizableComponentGroup | all AsyncAPI Specification-valid components. |

<a name="findDuplicateComponents"></a>

## findDuplicateComponents(optimizableComponentGroup) ⇒
**Kind**: global function
**Returns**: A list of duplicated elements in optimization report.
**Returns**: A list of optimization report elements.

| Param | Description |
| --- | --- |
| optimizableComponentGroup | list of all AsyncAPI Specification-valid components that you want to analyze for duplicates. |
| optimizableComponentGroup | all AsyncAPI Specification-valid components that you want to analyze for duplicates. |

<a name="hasParent"></a>

Expand Down Expand Up @@ -146,7 +148,16 @@ Converts JSON or YAML string object.
| [removeComponents] | <code>Boolean</code> | whether to remove un-used components from `components` section or not. Defaults to `true`. |
| [moveAllToComponents] | <code>Boolean</code> | whether to move all AsyncAPI Specification-valid components to the `components` section or not. Defaults to `true`. |
| [moveDuplicatesToComponents] | <code>Boolean</code> | whether to move duplicated components to the `components` section or not. Defaults to `false`. |
| [schemas] | <code>Boolean</code> | whether to add calculated `schemas` to the optimized AsyncAPI Document or not. Defaults to `true`. |

<a name="DisableOptimizationFor"></a>

## DisableOptimizationFor : <code>Object</code>
**Kind**: global typedef
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| [schema] | <code>Boolean</code> | whether object `schema` should be excluded from the process of optimization (`true` instructs **not** to add calculated `schemas` to the optimized AsyncAPI Document.) |

<a name="Options"></a>

Expand All @@ -158,3 +169,5 @@ Converts JSON or YAML string object.
| --- | --- | --- |
| [rules] | [<code>Rules</code>](#Rules) | the list of rules that specifies which type of optimizations should be applied. |
| [output] | <code>String</code> | specifies which type of output user wants, `'JSON'` or `'YAML'`. Defaults to `'YAML'`; |
| [disableOptimizationFor] | [<code>DisableOptimizationFor</code>](#DisableOptimizationFor) | the list of objects that should be excluded from the process of optimization. |

4 changes: 1 addition & 3 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ optimizer.getReport().then((report) => {
moveDuplicatesToComponents: false,
},
disableOptimizationFor: {
schema: true,
schema: false,
},
})
//store optimizedDocument as to output.yaml
require('fs').writeFileSync('./examples/output.yaml', optimizedDocument)
})

// , { rules: { schemas: false } }
12 changes: 8 additions & 4 deletions src/Optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,20 @@ export class Optimizer {
* @typedef {Object} Rules
* @property {Boolean=} reuseComponents - whether to reuse components from `components` section or not. Defaults to `true`.
* @property {Boolean=} removeComponents - whether to remove un-used components from `components` section or not. Defaults to `true`.
* @property {Boolean=} moveAllToComponents - whether to move all AsyncAPI Specification-valid components to the `components` section or not.
* @property {Boolean=} moveDuplicatesToComponents - whether to move duplicated components to the `components` section or not. Defaults to `true`.
* @property {Boolean=} moveAllToComponents - whether to move all AsyncAPI Specification-valid components to the `components` section or not. Defaults to `true`.
* @property {Boolean=} moveDuplicatesToComponents - whether to move duplicated components to the `components` section or not. Defaults to `false`.
*/
/**
* @typedef {Object} DisableOptimizationFor
* @property {Boolean=} schema - whether object `schema` should be excluded from the process of optimization (`true` instructs **not** to add calculated `schemas` to the optimized AsyncAPI Document.)
*/

/**
* @typedef {Object} Options
* @property {Rules=} rules - the list of rules that specifies which type of optimizations should be applied.
* @property {String=} output - specifies which type of output user wants, `'JSON'` or `'YAML'`. Defaults to `'YAML'`;
* @property {DisableOptimizationFor=} disableOptimizationFor - the list of objects that should be excluded from the process of optimization.
*/
/**
/**
* This function is used to get the optimized document after seeing the report.
*
* @param {Options=} Options - the options are a way to customize the final output.
Expand Down
2 changes: 1 addition & 1 deletion src/Reporters/moveAllToComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const findAllComponents = (
if (component.component['x-origin']) {
componentName = String(component.component['x-origin']).split('/').reverse()[0]
} else {
componentName = String(component.path).split('.')[1]
componentName = String(component.path).split('.').reverse()[0]
}
const target = `components.${optimizableComponentGroup.type}.${componentName}`
resultElements.push({
Expand Down
4 changes: 2 additions & 2 deletions src/Reporters/moveDuplicatesToComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Debug from 'debug'
const debug = Debug('reporter:moveDuplicatesToComponents')
/**
*
* @param optimizableComponentGroup components that you want to analyze for duplicates.
* @param optimizableComponentGroup all AsyncAPI Specification-valid components that you want to analyze for duplicates.
* @returns A list of optimization report elements.
*/
const findDuplicateComponents = (
Expand All @@ -28,7 +28,7 @@ const findDuplicateComponents = (
if (component.component['x-origin']) {
componentName = String(component.component['x-origin']).split('/').reverse()[0]
} else {
componentName = String(component.path).split('.')[1]
componentName = String(component.path).split('.').reverse()[0]
}
const target = `components.${optimizableComponentGroup.type}.${componentName}`
resultElements.push({
Expand Down

0 comments on commit ab30af8

Please sign in to comment.