| Conditions | 7 |
| Paths | 12 |
| Total Lines | 63 |
| 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 { List, Map, fromJS } from 'immutable'; |
||
| 3 | export const treeToFlatList = ( |
||
| 4 | data, |
||
| 5 | rootIdentifier = 'root', |
||
| 6 | childIdentifier = 'children' |
||
| 7 | ) => { |
||
| 8 | |||
| 9 | if (!data) { |
||
| 10 | throw new Error('Expected data to be defined'); |
||
| 11 | } |
||
| 12 | |||
| 13 | const result = []; |
||
| 14 | |||
| 15 | let stack = List(); |
||
| 16 | |||
| 17 | const cfg = { flatIndex: 0 }; |
||
| 18 | |||
| 19 | if (!Map.isMap(data)) { |
||
| 20 | data = fromJS(data); |
||
| 21 | } |
||
| 22 | |||
| 23 | if (data.get(rootIdentifier)) { |
||
| 24 | data = data.get(rootIdentifier); |
||
| 25 | |||
| 26 | stack = stack.push( |
||
| 27 | toItem(List(), childIdentifier, cfg)(data) |
||
| 28 | ); |
||
| 29 | } |
||
| 30 | else { |
||
| 31 | stack = data.get(childIdentifier).map( |
||
| 32 | toItem(List([-1]), List([0]), childIdentifier) |
||
| 33 | ); |
||
| 34 | } |
||
| 35 | |||
| 36 | while (stack.count()) { |
||
| 37 | |||
| 38 | const item = stack.first(); |
||
| 39 | |||
| 40 | stack = stack.shift(); |
||
| 41 | const children = item.get(childIdentifier); |
||
| 42 | // console.log(item.get('id'), (children || List()).map(i => i.get('id')).toJS(), stack.map(i => i.get('id')).toJS()); |
||
| 43 | |||
| 44 | if (List.isList(children) && !item.get('_hideChildren')) { |
||
| 45 | stack = children.map( |
||
| 46 | toItem( |
||
| 47 | item.get('_path').push(item.get('_id')), |
||
| 48 | childIdentifier, |
||
| 49 | cfg, |
||
| 50 | item, |
||
| 51 | children |
||
| 52 | ) |
||
| 53 | ).concat(stack); |
||
| 54 | } |
||
| 55 | |||
| 56 | // removing erroneous data since grid uses internal values |
||
| 57 | result.push( |
||
| 58 | item.delete(childIdentifier) |
||
| 59 | .delete('parentId') |
||
| 60 | .delete('id') |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | return List(result); |
||
| 65 | }; |
||
| 66 | |||
| 112 |