我如何重构这个似乎重复了很多相同事情的函数调用



我做了一个功能来搜索博客文章。但是,当添加到containsQuery数组时,我必须确保标题优先于摘录,摘录优先于内容。我制作了以下代码,它似乎运行良好,但似乎有很多多余的代码。我该如何改进?

const findArticles = (posts: any[], query: string) => {
const containsQuery: any[] = []
let words
let i: number
if (query.includes(' ')) {
words = query.toLowerCase().split(' ')
}
const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[] = [],) => {
if (multipleWords === false) {
for (i = 0; i < posts.length; i++) {
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
}
}
} else {
for (i = 0; i < posts.length; i++) {
if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
containsQuery.push(posts[i])
}
}
}

}
if (words) {
findArticle(true,  posts, query, 'title', words,)
} else {
findArticle(false, posts, query, 'title')
}
if (words) {
findArticle(true,  posts, query, 'excerpt', words,)
} else {
findArticle(false, posts, query, 'excerpt')
}
if (words) {
findArticle(true,  posts, query, 'content', words,)
} else {
findArticle(false, posts, query, 'content')
}
const oneOfKind = Array.from(new Set(containsQuery.map(article => article.id))).map(id => {
return containsQuery.find(a => a.id === id)
})

return oneOfKind
}

为了避免重复并节省时间,我尝试将帖子复制到我自己的constcopyOfPosts=posts中,然后如下所示进行变异。但由于某种原因,这最终破坏了代码。上面的代码似乎是我让它正常工作的唯一方法。欢迎提出任何建议。

if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
copyOfPosts.splice(i, 1)
}

此处:

const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[]) => {
const containsQuery: any[] = []
if (multipleWords === false) {
for (let i = 0; i < posts.length; i++) {
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
}
}
} else {
for (let i = 0; i < posts.length; i++) {
if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
containsQuery.push(posts[i])
}
}
}
return containsQuery
}
const findArticles = (posts: any[], query: string) => {
let words = query.includes(' ') ? query.toLocaleLowerCase().split(' ') : [] 
const containsQuery = findArticle(true, posts, query, 'title', words)
containsQuery.push(...findArticle(true, posts, query, 'excerpt', words))
containsQuery.push(...findArticle(true, posts, query, 'content', words))
const oneOfKind = Array.from(new Set(containsQuery.map(article => article.id))).map(id => {
return containsQuery.find(a => a.id === id)
})

return oneOfKind
}

我会用一种对更好的方式重构findArticle

const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[]) => {
const containsQuery: any[] = []
if (multipleWords === false) {
for (let i = 0; i < posts.length; i++) {
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
}
}
return containsQuery
} 

for (let i = 0; i < posts.length; i++) {
if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
containsQuery.push(posts[i])
}
}
return containsQuery
}

这是因为我不喜欢在这种情况下使用过多的else,但人们可能会抱怨使用两个return

最新更新