Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve custom filters readability with optional chaining #1275

Merged
58 changes: 30 additions & 28 deletions apps/nunjucks-filters/src/customFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getPayloadExamples(msg) {
}

const payload = msg.payload();
if (payload && payload.examples()) {
if (payload?.examples()) {
return payload.examples().map(example => ({ example }));
}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ function getHeadersExamples(msg) {
}

const headers = msg.headers();
if (headers && headers.examples()) {
if (headers?.examples()) {
return headers.examples().map(example => ({ example }));
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ filter.oneLine = oneLine;

/**
* Generate JSDoc from message properties of the header and the payload
*
*
* @example
* docline(
* Schema {
Expand All @@ -134,52 +134,54 @@ filter.oneLine = oneLine;
* my-app-header,
* options.message.headers
* )
*
*
* Returned value will be -> * @param {integer} options.message.headers.my-app-header
*
*
* @field {object} - Property object
* @fieldName {string} - Name of documented property
* @scopePropName {string} - Name of param for JSDocs
* @returns {string} JSDoc compatible entry
*/
function docline(field, fieldName, scopePropName) {
/* eslint-disable sonarjs/cognitive-complexity */
const getType = (f) => f.type() ? f.type() : 'string';
const getDescription = (f) => f.description() ? ` - ${f.description().replace(/\r?\n|\r/g, '')}` : '';
const getDefault = (f, type) => (f.default() && type === 'string') ? `'${f.default()}'` : f.default();
const getPName = (pName) => pName ? `${pName}.` : ``;
ManikantaMandala marked this conversation as resolved.
Show resolved Hide resolved

const buildLine = (f, fName, pName) => {
const type = f.type() ? f.type() : 'string';
const description = f.description() ? ` - ${f.description().replace(/\r?\n|\r/g, '')}` : '';
let def = f.default();
const type = getType(f);
const description = getDescription(f);
let def = getDefault(f, type);

if (def && type === 'string') def = `'${def}'`;
let line = ` * @param {${type}} ${getPName(pName)}${fName}`;
line += def !== undefined ? `=${def}]` : ``;

let line;
if (def !== undefined) {
line = ` * @param {${type}} [${pName ? `${pName}.` : ''}${fName}=${def}]`;
} else {
line = ` * @param {${type}} ${pName ? `${pName}.` : ''}${fName}`;
}
return type === 'object' ? `${line}`:`${line}${description}`;
};

if (type === 'object') {
let lines = `${line}\n`;
let first = true;
for (const propName in f.properties()) {
lines = `${lines}${first ? '' : '\n'}${buildLine(f.properties()[propName], propName, `${pName ? `${pName}.` : ''}${fName}`)}`;
first = false;
}
return lines;
const buildObjectLines = (f, fName, pName) => {
let lines = `${buildLine(f, fName, pName)}\n`;
let first = true;
for (const propName in f.properties()) {
lines += `${first ? '' : '\n'}${buildLine(f.properties()[propName], propName, `${getPName(pName)}${fName}`)}`;
first = false;
}

return `${line}${description}`;
return lines;
};

return buildLine(field, fieldName, scopePropName);
return getType(field) === 'object'
? buildObjectLines(field, fieldName, scopePropName)
: buildLine(field, fieldName, scopePropName);
}

filter.docline = docline;

/**
* Helper function to replace server variables in the url with actual values
* @url {string} - url string
* @serverserverVariables {Object} - Variables model map
* @returns {string}
* @returns {string}
*/
function replaceServerVariablesWithValues(url, serverVariables) {
const getVariablesNamesFromUrl = (inputUrl) => {
Expand All @@ -198,7 +200,7 @@ function replaceServerVariablesWithValues(url, serverVariables) {
const getVariableValue = (object, variable) => {
const keyValue = object[variable]._json;

if (keyValue) return keyValue.default || (keyValue.enum && keyValue.enum[0]);
if (keyValue) return keyValue.default || (keyValue.enum?.[0]);
};

const urlVariables = getVariablesNamesFromUrl(url);
Expand Down
Loading