From e1b95239345661e8e5203d1d6db5048e51c1ea25 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:26:12 +0300 Subject: [PATCH] HCK-8000: add comma commenting for last activated column statement (#143) --- forward_engineering/helpers/columnHelper.js | 8 +-- .../joinActivatedAndDeactivatedStatements.js | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 forward_engineering/helpers/joinActivatedAndDeactivatedStatements.js diff --git a/forward_engineering/helpers/columnHelper.js b/forward_engineering/helpers/columnHelper.js index 8325857..165071e 100644 --- a/forward_engineering/helpers/columnHelper.js +++ b/forward_engineering/helpers/columnHelper.js @@ -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); @@ -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' }); }, }; diff --git a/forward_engineering/helpers/joinActivatedAndDeactivatedStatements.js b/forward_engineering/helpers/joinActivatedAndDeactivatedStatements.js new file mode 100644 index 0000000..440e120 --- /dev/null +++ b/forward_engineering/helpers/joinActivatedAndDeactivatedStatements.js @@ -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, +};