Skip to content

Commit

Permalink
HCK-8000: add comma commenting for last activated column statement (#143
Browse files Browse the repository at this point in the history
)
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent 10d9612 commit e1b9523
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
8 changes: 5 additions & 3 deletions forward_engineering/helpers/columnHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

const { getTypeByData } = require('./typeHelper');
const { commentDeactivatedStatement } = require('./generalHelper');
const { joinActivatedAndDeactivatedStatements } = require('./joinActivatedAndDeactivatedStatements');

module.exports = {
getColumnDefinition(properties, udtTypeMap = {}, isParentActivated = false) {
return Object.keys(properties)
const statements = Object.keys(properties)
.map(name => {
const data = properties[name];
const typeDefinition = getTypeDefinition(data, udtTypeMap, name);
Expand All @@ -17,8 +18,9 @@ module.exports = {
return commentDeactivatedStatement(columnStatement, data.isActivated, isParentActivated, false);
}
})
.filter(column => column)
.join(',\n');
.filter(column => column);

return joinActivatedAndDeactivatedStatements({ statements, indent: '\n' });
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @param {{
* index: number;
* numberOfStatements: number;
* lastIndexOfActivatedStatement: number;
* delimiter: string;
* }}
* @return {string}
* */
const getDelimiter = ({ index, numberOfStatements, lastIndexOfActivatedStatement, delimiter }) => {
const isLastStatement = index === numberOfStatements - 1;
const isLastActivatedStatement = index === lastIndexOfActivatedStatement;

if (isLastStatement) {
return '';
}

if (isLastActivatedStatement) {
return ' --' + delimiter;
}

return delimiter;
};

/**
* @param {{
* statements?: string[];
* delimiter?: string;
* indent?: string;
* }}
* @return {string}
* */
const joinActivatedAndDeactivatedStatements = ({ statements = [], delimiter = ',', indent = '\n' }) => {
const lastIndexOfActivatedStatement = statements.findLastIndex(statement => !statement.startsWith('--'));
const numberOfStatements = statements.length;

return statements
.map((statement, index) => {
const currentDelimiter = getDelimiter({
index,
numberOfStatements,
lastIndexOfActivatedStatement,
delimiter,
});

return statement + currentDelimiter;
})
.join(indent);
};

module.exports = {
joinActivatedAndDeactivatedStatements,
};

0 comments on commit e1b9523

Please sign in to comment.