Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const lodtask = ({ingredientsArray}) => {
  2.     const ingredientsArgs = ingredientsArray.join(",");
  3.  
  4.     const requestApi = (ingredients, page) => {
  5.         return fetch(http://www.recipepuppy.com/api/?i=${ingredients}&p=${page})
  6.         .then((res) => res.json())
  7.         .catch(err => {
  8.             console.log("Yet another broken page: ", err);
  9.             return Promise.resolve([]);
  10.         })
  11.         .then((res) => res.results);
  12.     };
  13.  
  14.     const compareRecipes = (a, b) => {
  15.         const getComlexity = (recipe) => recipe.ingredients.split(",").length;
  16.         return Math.sign(getComlexity(a) - getComlexity(b));
  17.     }
  18.  
  19.     const requests = [...Array(20).keys()].map((value) => requestApi(ingredientsArgs, value + 1));
  20.  
  21.     return Promise.all(requests).then(res => {
  22.         const sortedRecipies = res.reduce((memo, current) => memo.concat(current), [])
  23.                                 .sort(compareRecipes)
  24.                                 .slice(0, 3);
  25.  
  26.         return sortedRecipies.map(r => ({
  27.                 Title: r.title,
  28.                 Link: r.href,
  29.                 Ingredients: r.ingredients
  30.             }));
  31.       });
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement