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'
|
} from '../../utils'
|
||||||
|
|
||||||
const arrayToLookup = (commentList, removed, deleted) => {
|
const arrayToLookup = (commentList, removed, deleted) => {
|
||||||
const lookup = {}
|
const lookup = new Map()
|
||||||
|
|
||||||
commentList.forEach(comment => {
|
commentList.forEach(comment => {
|
||||||
comment.replies = []
|
comment.replies = []
|
||||||
|
|
||||||
if (removed.includes(comment.id)) {
|
if (removed.has(comment.id)) {
|
||||||
comment.removed = true
|
comment.removed = true
|
||||||
} else if (deleted.includes(comment.id)) {
|
} else if (deleted.has(comment.id)) {
|
||||||
comment.deleted = true
|
comment.deleted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
lookup[comment.id] = comment
|
lookup.set(comment.id, comment)
|
||||||
})
|
})
|
||||||
|
|
||||||
return lookup
|
return lookup
|
||||||
|
|
@ -28,25 +28,23 @@ const unflatten = (comments, root, removed, deleted) => {
|
||||||
const lookup = arrayToLookup(comments, removed, deleted)
|
const lookup = arrayToLookup(comments, removed, deleted)
|
||||||
const commentTree = []
|
const commentTree = []
|
||||||
|
|
||||||
Object.keys(lookup).forEach(commentID => {
|
lookup.forEach(comment => {
|
||||||
const comment = lookup[commentID]
|
|
||||||
const parentID = comment.parent_id
|
const parentID = comment.parent_id
|
||||||
|
let parentComment
|
||||||
|
|
||||||
if (parentID === root) {
|
if (parentID === root) {
|
||||||
commentTree.push(comment)
|
commentTree.push(comment)
|
||||||
|
} else if ((parentComment = lookup.get(parentID)) !== undefined) {
|
||||||
|
parentComment.replies.push(comment)
|
||||||
} else {
|
} else {
|
||||||
if (lookup[parentID] === undefined) {
|
console.error('MISSING PARENT ID:', parentID, 'for comment', comment)
|
||||||
console.error('MISSING PARENT ID:', parentID, 'for comment', comment)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
lookup[parentID].replies.push(comment)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (lookup[root] !== undefined) {
|
let rootComment
|
||||||
lookup[root].replies = commentTree
|
if ((rootComment = lookup.get(root)) !== undefined) {
|
||||||
return [lookup[root]]
|
rootComment.replies = commentTree
|
||||||
|
return rootComment
|
||||||
}
|
}
|
||||||
|
|
||||||
return commentTree
|
return commentTree
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@ class Thread extends React.Component {
|
||||||
state = {
|
state = {
|
||||||
post: {},
|
post: {},
|
||||||
pushshiftComments: [],
|
pushshiftComments: [],
|
||||||
removed: [],
|
removed: new Set(),
|
||||||
deleted: [],
|
deleted: new Set(),
|
||||||
loadingComments: true
|
loadingComments: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,8 +73,8 @@ class Thread extends React.Component {
|
||||||
return getRedditComments(ids)
|
return getRedditComments(ids)
|
||||||
.then(redditComments => {
|
.then(redditComments => {
|
||||||
console.log(`Reddit: ${redditComments.length} comments`)
|
console.log(`Reddit: ${redditComments.length} comments`)
|
||||||
const removed = []
|
const removed = new Set()
|
||||||
const deleted = []
|
const deleted = new Set()
|
||||||
|
|
||||||
redditComments.forEach(comment => {
|
redditComments.forEach(comment => {
|
||||||
const pushshiftComment = pushshiftCommentLookup.get(comment.id)
|
const pushshiftComment = pushshiftCommentLookup.get(comment.id)
|
||||||
|
|
@ -90,9 +90,9 @@ class Thread extends React.Component {
|
||||||
|
|
||||||
// Check what is removed / deleted according to reddit
|
// Check what is removed / deleted according to reddit
|
||||||
if (isRemoved(comment.body)) {
|
if (isRemoved(comment.body)) {
|
||||||
removed.push(comment.id)
|
removed.add(comment.id)
|
||||||
} else if (isDeleted(comment.body)) {
|
} else if (isDeleted(comment.body)) {
|
||||||
deleted.push(comment.id)
|
deleted.add(comment.id)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -124,8 +124,8 @@ class Thread extends React.Component {
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<CommentInfo
|
<CommentInfo
|
||||||
total={this.state.pushshiftComments.length}
|
total={this.state.pushshiftComments.length}
|
||||||
removed={this.state.removed.length}
|
removed={this.state.removed.size}
|
||||||
deleted={this.state.deleted.length}
|
deleted={this.state.deleted.size}
|
||||||
/>
|
/>
|
||||||
<SortBy />
|
<SortBy />
|
||||||
{isSingleComment &&
|
{isSingleComment &&
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue