Refactor this: [code]
- User
Refactor this:
createActionLabel(action, data) { const board = action.board || data.board; const label = board ? action.label.replace(/из таблицы 'comments_.*/g, '') : action.label; const link = (() => { if (!board) return `search&text=${ label }`; const isReport = action.type === 'report_remove'; if (isReport) return `search&text=Board:${ board }`; else return `board&text=${ board }`; })(); return ` <span class="action-container" title="Действие"> ${ this.icon('glyphicon-list') } <a href="/moder/modlog?action=${ link }" class="action-text">${ label }${ board ? `${ data.num === data.parent ? 'треда' : '' } с доски /${ board }/` : '' }</a> </span>`; } - Assistant
The
createActionLabelfunction you've provided can be refactored for better readability and maintainability. Here’s one way to do it:createActionLabel(action, data) { const board = action.board || data.board; const label = this.getLabel(action, board); const link = this.createLink(action, board); return this.renderActionContainer(link, label, board, data); } getLabel(action, board) { return board ? action.label.replace(/из таблицы 'comments_.*/g, '') : action.label; } createLink(action, board) { if (!board) { return `search&text=${this.encodeLabel(action.label)}`; } const isReport = action.type === 'report_remove'; return isReport ? `search&text=Board:${board}` : `board&text=${board}`; } encodeLabel(label) { return encodeURIComponent(label); } renderActionContainer(link, label, board, data) { const boardInfo = board ? `${data.num === data.parent ? 'треда' : ''} с доски /${board}/` : ''; return ` <span class="action-container" title="Действие"> ${this.icon('glyphicon-list')} <a href="/moder/modlog?action=${link}" class="action-text">${label}${boardInfo}</a> </span>`; } - User
Fix this:
const label = replaceMap.reduce((formattedLabel, [regex, replacement]) => formattedLabel.replace(regex, replacement), []); - Assistant
The issue with the provided code snippet is that you are initializing the accumulator of the
reducemethod with an empty array ([]). However, the accumulator should be initialized with the original label that you want to format.Here’s how you can fix it:
- Ensure that you replace the empty array with the initial label value.
- The
replaceMapshould contain pairs of regular expressions and their corresponding replacements.
Here’s the corrected code:
const label = replaceMap.reduce((formattedLabel, [regex, replacement]) => formattedLabel.replace(regex, replacement), originalLabel);Explanation:
- Replace
originalLabelwith the actual string that you want to format. This could be a variable holding the label you want to adjust.
- User
Refactor this:
const replaceMap = [ [ /из таблицы 'comments_.*/, '' ], [ /в таблицу банов/, 'бана' ], ] const board = action.board || data.board; const label = replaceMap.reduce((formattedLabel, [regex, replacement]) => formattedLabel.replace(regex, replacement), action.label); const link = (() => { const banTypes = [ 'ban_add', 'banrequest_add', 'banrequest_disallow', 'banrequest_allow' ]; const isReport = action.type === 'report_remove', isBan = banTypes.includes(action.type); if (!board) return `search&text=${ action.label }`; if (isReport) return `search&text=Board:${ board }`; else if (isBan) return `search&text=//!${ board }`; return `board&text=${ board }`; })(); return ` <span class="action-container" title="Действие"> ${ this.icon('glyphicon-list') } <a href="/moder/modlog?action=${ link }" class="action-text">${ label } ${ board ? ` ${ action.type === 'board_post_remove' ? data.num === data.parent ? 'треда' : 'поста' : '' } на доске /${ board }/` : '' }</a> </span>`; - Assistant