Conditions | 8 |
Total Lines | 62 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import {choiceSection} from './helpers/vote'; |
||
47 | |||
48 | /** |
||
49 | * Builds the configuration form. |
||
50 | * |
||
51 | * @param {object} state - Current state of poll |
||
52 | * @param {object} state.author - User that submitted the poll |
||
53 | * @param {string} state.topic - Topic of poll |
||
54 | * @param {string[]} state.choices - Text of choices to display to users |
||
55 | * @param {object} state.votes - Map of cast votes keyed by choice index |
||
56 | * @param {object} state.choiceCreator - Map of cast votes keyed by choice index |
||
57 | * @param {boolean} state.anon - Is anonymous?(will save voter name or not) |
||
58 | * @param {boolean} state.optionable - Can other user add other option? |
||
59 | * @returns {chatV1.Schema$CardWithId} card |
||
60 | */ |
||
61 | export function buildVoteCard(state: PollState) { |
||
62 | const sections = []; |
||
63 | const stateString = JSON.stringify(state); |
||
64 | |||
65 | const votes: Array<Array<Voter>> = Object.values(state.votes!); |
||
66 | const totalVotes: number = votes.reduce((sum, vote) => sum + vote.length, 0); |
||
67 | for (let i = 0; i < state.choices.length; ++i) { |
||
68 | const creator = state.choiceCreator?.[i]; |
||
69 | const section = choiceSection(i, state, totalVotes, stateString, creator); |
||
70 | sections.push(section); |
||
71 | } |
||
72 | if (state.optionable) { |
||
73 | sections.push( |
||
74 | { |
||
75 | 'widgets': [ |
||
76 | { |
||
77 | 'buttonList': { |
||
78 | 'buttons': [ |
||
79 | { |
||
80 | 'text': 'Add Option', |
||
81 | 'onClick': { |
||
82 | 'action': { |
||
83 | 'function': 'add_option_form', |
||
84 | 'interaction': 'OPEN_DIALOG', |
||
85 | 'parameters': [], |
||
86 | }, |
||
87 | }, |
||
88 | }, |
||
89 | ], |
||
90 | }, |
||
91 | }, |
||
92 | ], |
||
93 | }); |
||
94 | } |
||
95 | const card: chatV1.Schema$CardWithId = { |
||
96 | 'cardId': 'unique-card-id', |
||
97 | 'card': { |
||
98 | sections, |
||
99 | }, |
||
100 | }; |
||
101 | const authorName = state.author?.displayName ?? ''; |
||
102 | if (state.topic.length > 40) { |
||
103 | const widgetHeader = sectionHeader(state.topic, authorName); |
||
104 | card.card!.sections = [widgetHeader, ...sections]; |
||
105 | } else { |
||
106 | card.card!.header = cardHeader(state.topic, authorName); |
||
107 | } |
||
108 | return card; |
||
109 | } |
||
110 |