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 13c0681
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 143 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 } }
10 changes: 7 additions & 3 deletions src/Optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,18 @@ 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.
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
10 changes: 7 additions & 3 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 Expand Up @@ -60,7 +60,11 @@ const findDuplicateComponents = (
}

export const moveDuplicatesToComponents: Reporter = (optimizableComponentsGroup) => {
return createReport(findDuplicateComponents, optimizableComponentsGroup, 'moveDuplicatesToComponents')
return createReport(
findDuplicateComponents,
optimizableComponentsGroup,
'moveDuplicatesToComponents'
)
}

function getOutsideComponents(
Expand Down
60 changes: 30 additions & 30 deletions test/Reporters/Reporters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,96 @@ import { OptimizableComponentGroup } from '../../src/index.d'

const moveAllToComponentsExpectedResult: any[] = [
{
action: 'move',
path: 'channels.withDuplicatedMessage1.messages.duped1',
target: 'components.messages.withDuplicatedMessage1',
action: 'move',
target: 'components.messages.duped1',
},
{
action: 'move',
path: 'channels.withDuplicatedMessage2.messages.duped2',
target: 'components.messages.withDuplicatedMessage2',
action: 'move',
target: 'components.messages.duped2',
},
{
action: 'move',
path: 'channels.withFullFormMessage.messages.canBeReused',
target: 'components.messages.withFullFormMessage',
action: 'move',
target: 'components.messages.canBeReused',
},
{
action: 'move',
path: 'channels.withDuplicatedMessage1',
action: 'move',
target: 'components.channels.withDuplicatedMessage1FromXOrigin',
},
{
action: 'move',
path: 'channels.withDuplicatedMessage2',
action: 'move',
target: 'components.channels.withDuplicatedMessage2',
},
{
action: 'move',
path: 'channels.withFullFormMessage',
action: 'move',
target: 'components.channels.withFullFormMessage',
},
{
action: 'move',
path: 'channels.UserSignedUp1',
action: 'move',
target: 'components.channels.UserSignedUp1',
},
{
action: 'move',
path: 'channels.UserSignedUp2',
action: 'move',
target: 'components.channels.UserSignedUp2',
},
{
action: 'move',
path: 'channels.deleteAccount',
action: 'move',
target: 'components.channels.deleteAccount',
},
{
action: 'move',
path: 'channels.withDuplicatedMessage1.messages.duped1.payload',
target: 'components.schemas.withDuplicatedMessage1',
action: 'move',
target: 'components.schemas.payload',
},
{
action: 'move',
path: 'channels.withDuplicatedMessage2.messages.duped2.payload',
target: 'components.schemas.withDuplicatedMessage2',
action: 'move',
target: 'components.schemas.payload',
},
{
action: 'move',
path: 'channels.UserSignedUp1.messages.myMessage.payload',
target: 'components.schemas.UserSignedUp1',
action: 'move',
target: 'components.schemas.payload',
},
{
action: 'move',
path: 'channels.UserSignedUp1.messages.myMessage.payload.properties.displayName',
target: 'components.schemas.UserSignedUp1',
action: 'move',
target: 'components.schemas.displayName',
},
{
action: 'move',
path: 'channels.UserSignedUp1.messages.myMessage.payload.properties.email',
target: 'components.schemas.UserSignedUp1',
action: 'move',
target: 'components.schemas.email',
},
{
action: 'move',
path: 'channels.deleteAccount.messages.deleteUser.payload',
target: 'components.schemas.deleteAccount',
action: 'move',
target: 'components.schemas.payload',
},
{
action: 'move',
path: 'operations.user/deleteAccount.subscribe',
target: 'components.operations.user/deleteAccount',
action: 'move',
target: 'components.operations.subscribe',
},
]
const moveDuplicatesToComponentsExpectedResult: any[] = [
{
path: 'channels.withDuplicatedMessage1.messages.duped1',
action: 'move',
target: 'components.messages.withDuplicatedMessage1',
target: 'components.messages.duped1',
},
{
path: 'channels.withDuplicatedMessage2.messages.duped2',
action: 'reuse',
target: 'components.messages.withDuplicatedMessage1',
target: 'components.messages.duped1',
},
{
path: 'channels.UserSignedUp1',
Expand All @@ -115,12 +115,12 @@ const moveDuplicatesToComponentsExpectedResult: any[] = [
{
path: 'channels.withDuplicatedMessage1.messages.duped1.payload',
action: 'move',
target: 'components.schemas.withDuplicatedMessage1',
target: 'components.schemas.payload',
},
{
path: 'channels.withDuplicatedMessage2.messages.duped2.payload',
action: 'reuse',
target: 'components.schemas.withDuplicatedMessage1',
target: 'components.schemas.payload',
},
]
const RemoveComponentsExpectedResult = [
Expand Down
Loading

0 comments on commit 13c0681

Please sign in to comment.