refactor/style: reduce duplication

fix/workaround/#26
SukkaW 6 years ago
parent 66fc42c2de
commit 196a9adc54

@ -376,6 +376,16 @@
let topLevelComments = [], let topLevelComments = [],
childComments = []; childComments = [];
let commentJSON = (comment) => {
return {
comment,
author: comment.author.name,
// 如果不设置 admin 会返回 undefined所以需要嘴一个判断
isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
children: getChildren(+comment.id)
}
}
let getChildren = (id) => { let getChildren = (id) => {
// 如果没有子评论,就不需要解析子评论了 // 如果没有子评论,就不需要解析子评论了
if (childComments.length === 0) { if (childComments.length === 0) {
@ -385,13 +395,7 @@
var list = []; var list = [];
for (let comment of childComments) { for (let comment of childComments) {
if (comment.parent === id) { if (comment.parent === id) {
list.unshift({ list.unshift(commentJSON(comment));
comment,
author: comment.author.name,
// 如果不设置 admin 会返回 undefined所以需要嘴一个判断
isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
children: getChildren(+comment.id)
});
} }
} }
@ -408,12 +412,7 @@
}); });
let commentLists = topLevelComments.map((comment) => { let commentLists = topLevelComments.map((comment) => {
return { return commentJSON(comment);
comment,
author: comment.author.name,
isPrimary: (disqusjs.config.admin ? (comment.author.username === disqusjs.config.admin) : false),
children: getChildren(+comment.id)
};
}); });
return commentLists; return commentLists;

Loading…
Cancel
Save