| Conditions | 8 |
| Total Lines | 73 |
| Code Lines | 60 |
| 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 type { PropType } from 'vue'; |
||
| 141 | |||
| 142 | getSnippetsTree(selections: Selection[]): void { |
||
| 143 | const mappedObj = {}; |
||
| 144 | |||
| 145 | const generate = ( |
||
| 146 | currentIndex: number, |
||
| 147 | argument: { keyWords: string[], name: string }, |
||
| 148 | result: { [key: string]: TreeItem }, |
||
| 149 | ) => { |
||
| 150 | const { keyWords, name } = argument; |
||
| 151 | const currentKey = keyWords[currentIndex]; |
||
| 152 | |||
| 153 | // next key is child of current key |
||
| 154 | const nextKey = keyWords[currentIndex + 1]; |
||
| 155 | |||
| 156 | result[currentKey] = result[currentKey] || { |
||
| 157 | id: currentKey, |
||
| 158 | name, |
||
| 159 | parentId: null, |
||
| 160 | children: {}, |
||
| 161 | }; |
||
| 162 | |||
| 163 | if (!nextKey) { |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | // Put next key into children of current key |
||
| 168 | result[currentKey].children[nextKey] = result[currentKey]?.children[nextKey] || { |
||
| 169 | id: `${result[currentKey].id}.${nextKey}`, |
||
| 170 | name, |
||
| 171 | parentId: result[currentKey].id, |
||
| 172 | children: {}, |
||
| 173 | }; |
||
| 174 | |||
| 175 | generate(currentIndex + 1, { keyWords, name }, result[currentKey].children); |
||
| 176 | }; |
||
| 177 | |||
| 178 | const convertTreeToArray = (nodes: TreeItem[], output: TreeItem[] = []) => { |
||
| 179 | const getName = ({ parentId = null, id, children, name }: TreeItem): string => { |
||
| 180 | const [eventName] = parentId ? id.split('.').reverse() : [id]; |
||
| 181 | |||
| 182 | // Replace '_' or '-' to blank space. |
||
| 183 | return !Object.values(children).length ? name : eventName.replace(/_|-/g, ' '); |
||
| 184 | }; |
||
| 185 | |||
| 186 | nodes.forEach(node => { |
||
| 187 | const children = node.children ? Object.values(node.children) : []; |
||
| 188 | output.push({ |
||
| 189 | id: node.id, |
||
| 190 | name: getName(node), |
||
| 191 | childCount: children.length, |
||
| 192 | parentId: node.parentId, |
||
| 193 | children: {}, |
||
| 194 | }); |
||
| 195 | |||
| 196 | if (children.length > 0) { |
||
| 197 | output = convertTreeToArray(children, output); |
||
| 198 | } |
||
| 199 | }); |
||
| 200 | |||
| 201 | return output; |
||
| 202 | }; |
||
| 203 | |||
| 204 | selections.forEach(snippet => { |
||
| 205 | const keyWords = snippet.id.split('/'); |
||
| 206 | if (keyWords.length === 0) { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | generate(0, { keyWords, ...snippet }, mappedObj); |
||
| 211 | }); |
||
| 212 | |||
| 213 | this.searchResults = convertTreeToArray(Object.values(mappedObj)); |
||
| 214 | }, |
||
| 225 |