有没有办法将此Promises调用重构为“最佳编码实践”版本?

时间:2019-01-27 09:16:27

标签: javascript node.js mongoose architecture

我的代码工作正常,我只想继续通过良好的编码习惯来解决此搜索功能。我觉得我重复相同的代码太多次了。如果用户在搜索菜单中选择了两者之一,则我有两种类型的对象“程序”和“出租”:我只想为指定的对象搜索“我的数据库”,但是如果他选择了两者或都不选择,则我想执行两个搜索功能。 为了实现这一点,我有条件确定是否检查那些搜索选项,如果只选择一个,我将在相应的模式上执行相应的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               
        }

关于如何将其重构为更好版本的任何想法?

1 个答案:

答案 0 :(得分:1)

可以是这样的:

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