(comments)
| 12 | |
| 13 | // If this gets slow introduce caching. |
| 14 | function nest (comments) { |
| 15 | if (comments.length === 0) { |
| 16 | return comments |
| 17 | } |
| 18 | return comments.reduce((nested, comment) => { |
| 19 | comment.replies = comments.filter( |
| 20 | reply => reply.replyToId === comment.id |
| 21 | ) |
| 22 | nest(comment.replies) |
| 23 | if (comment.replyToId == null) { |
| 24 | nested.push(comment) |
| 25 | } |
| 26 | return nested |
| 27 | }, []) |
| 28 | } |
| 29 | } |
| 30 | } |