gpt4 book ai didi

javascript - 有没有办法将此 Promises 调用重构为 "best coding practices"版本?

转载 作者:行者123 更新时间:2023-12-03 00:19:26 25 4
gpt4 key购买 nike

我的代码运行良好,我只是想继续通过良好的编码实践来解决此搜索功能。我觉得我重复相同的代码太多次了。我有两种不同类型的对象“程序”和“租赁”,如果用户在搜索菜单中选择其中之一,我只想在我的数据库中搜索指定的对象,但是如果他选择两者或都不选择,我想执行这两个搜索功能。为了实现这一目标,我有一个条件 if 来检查这些搜索选项,如果只选择一个,我将在相应的架构上执行相应的 find(),

if((queryObj.selectedSearchObjectType.indexOf("rentals") > -1))
{
let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
}

但是,您可能会注意到,我重复相同的代码 4 次只是为了实现此目的,我想制定良好的编码实践,因此我认为这看起来丑陋且效率低下。

if(queryObj.title) //If we have a title seacrh
{
queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array

if(titleSplited.length > 0)
{
// Check if selectedSearchObjectType is defined to execute queryes selectively
if(queryObj.selectedSearchObjectType)
{
// Check if selectedSearchObjectType contains 'rentals' in order to add the result to finalResults Array
if((queryObj.selectedSearchObjectType.indexOf("rentals") > -1))
{
let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
}

// Check if selectedSearchObjectType contains 'programs' in order to add the result to finalResults Array
if((queryObj.selectedSearchObjectType.indexOf("programs") > -1))
{
let foundProgramsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.programs = searchResults.programs.concat(foundProgramsByTitle);
}
}else{
//If no selectedSearchObjectType was selected then execute both
let foundProgramsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
searchResults.programs = searchResults.programs.concat(foundProgramsByTitle);
}

// Filter results code ...
// .........................
// End of Function
}

关于如何将其重构为更好的版本有什么想法吗?

最佳答案

可以是这样的:

if(!queryObj.title) return; // just exit from the function or return representation of an empty result


queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array

if(titleSplited.length <= 0) return; // same as above

// define the holder for search operations
const searchPromises = {};

if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf("rentals") > -1)
{
searchPromises.rentals = Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
}

if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf("programs") > -1)
{
searchPromises.programs = Promise.all(titleSplited.map((value) =>
getProgramsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
}

if (searchPromises.rentals) {
searchResults.rentals = searchResults.rentals.concat(await searchPromises.rentals);
}

if (searchPromises.programs) {
searchResults.programs = searchResults.programs.concat(await searchPromises.programs);
}

// Filter results code ...
// .........................
// End of Function

稍后我们可以定义搜索类型并提取搜索功能:

const RENTALS = 'rentals';
const PROGRAMS = 'programs';

const searchFns = {
[RENTALS]: (value) => getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } ),
[PROGRAMS]: (value) => getProgramsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
};

所以你可以将你的函数更改为:

if(!queryObj.title) return; // just exit from the function or return representation of an empty result

queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array

if(titleSplited.length <= 0) return; // same as above

// define the holder for search operations
const searchPromises = {};
const searchTypes = [RENTALS, PROGRAMS];

searchTypes.forEach((type) => {
if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf(type) > -1)
{
searchPromises[type] = Promise.all(titleSplited.map(searchFns[type]));
}
});

// iterate over created promises
for (let type in searchPromises) {
searchResults[type] = searchResults[type].concat(await searchPromises[type])
}

// Filter results code ...
// .........................
// End of Function

关于javascript - 有没有办法将此 Promises 调用重构为 "best coding practices"版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54386647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com