mirror of
https://github.com/gurnec/removeddit.git
synced 2026-03-11 08:54:27 +00:00
Use Map and Set where possible and appropriate
This commit is contained in:
parent
47b4d913bd
commit
702668ad2e
2 changed files with 21 additions and 23 deletions
|
|
@ -7,18 +7,18 @@ import {
|
|||
} from '../../utils'
|
||||
|
||||
const arrayToLookup = (commentList, removed, deleted) => {
|
||||
const lookup = {}
|
||||
const lookup = new Map()
|
||||
|
||||
commentList.forEach(comment => {
|
||||
comment.replies = []
|
||||
|
||||
if (removed.includes(comment.id)) {
|
||||
if (removed.has(comment.id)) {
|
||||
comment.removed = true
|
||||
} else if (deleted.includes(comment.id)) {
|
||||
} else if (deleted.has(comment.id)) {
|
||||
comment.deleted = true
|
||||
}
|
||||
|
||||
lookup[comment.id] = comment
|
||||
lookup.set(comment.id, comment)
|
||||
})
|
||||
|
||||
return lookup
|
||||
|
|
@ -28,25 +28,23 @@ const unflatten = (comments, root, removed, deleted) => {
|
|||
const lookup = arrayToLookup(comments, removed, deleted)
|
||||
const commentTree = []
|
||||
|
||||
Object.keys(lookup).forEach(commentID => {
|
||||
const comment = lookup[commentID]
|
||||
lookup.forEach(comment => {
|
||||
const parentID = comment.parent_id
|
||||
let parentComment
|
||||
|
||||
if (parentID === root) {
|
||||
commentTree.push(comment)
|
||||
} else if ((parentComment = lookup.get(parentID)) !== undefined) {
|
||||
parentComment.replies.push(comment)
|
||||
} else {
|
||||
if (lookup[parentID] === undefined) {
|
||||
console.error('MISSING PARENT ID:', parentID, 'for comment', comment)
|
||||
return
|
||||
}
|
||||
|
||||
lookup[parentID].replies.push(comment)
|
||||
console.error('MISSING PARENT ID:', parentID, 'for comment', comment)
|
||||
}
|
||||
})
|
||||
|
||||
if (lookup[root] !== undefined) {
|
||||
lookup[root].replies = commentTree
|
||||
return [lookup[root]]
|
||||
let rootComment
|
||||
if ((rootComment = lookup.get(root)) !== undefined) {
|
||||
rootComment.replies = commentTree
|
||||
return rootComment
|
||||
}
|
||||
|
||||
return commentTree
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ class Thread extends React.Component {
|
|||
state = {
|
||||
post: {},
|
||||
pushshiftComments: [],
|
||||
removed: [],
|
||||
deleted: [],
|
||||
removed: new Set(),
|
||||
deleted: new Set(),
|
||||
loadingComments: true
|
||||
}
|
||||
|
||||
|
|
@ -73,8 +73,8 @@ class Thread extends React.Component {
|
|||
return getRedditComments(ids)
|
||||
.then(redditComments => {
|
||||
console.log(`Reddit: ${redditComments.length} comments`)
|
||||
const removed = []
|
||||
const deleted = []
|
||||
const removed = new Set()
|
||||
const deleted = new Set()
|
||||
|
||||
redditComments.forEach(comment => {
|
||||
const pushshiftComment = pushshiftCommentLookup.get(comment.id)
|
||||
|
|
@ -90,9 +90,9 @@ class Thread extends React.Component {
|
|||
|
||||
// Check what is removed / deleted according to reddit
|
||||
if (isRemoved(comment.body)) {
|
||||
removed.push(comment.id)
|
||||
removed.add(comment.id)
|
||||
} else if (isDeleted(comment.body)) {
|
||||
deleted.push(comment.id)
|
||||
deleted.add(comment.id)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -124,8 +124,8 @@ class Thread extends React.Component {
|
|||
<React.Fragment>
|
||||
<CommentInfo
|
||||
total={this.state.pushshiftComments.length}
|
||||
removed={this.state.removed.length}
|
||||
deleted={this.state.deleted.length}
|
||||
removed={this.state.removed.size}
|
||||
deleted={this.state.deleted.size}
|
||||
/>
|
||||
<SortBy />
|
||||
{isSingleComment &&
|
||||
|
|
|
|||
Loading…
Reference in a new issue