Advertisement
ptmdmusique

Redux Filter plugin

Jan 25th, 2021
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. filter: action((state, { dataList, defaultFilter, customFilter }) => {
  2.       if (!customFilter && !defaultFilter) {
  3.         throw new Error(internalErrorMessage.invalidFilterParam);
  4.       }
  5.  
  6.       const filterCache = { ...state.filterCache };
  7.       let filterResult = [...dataList];
  8.  
  9.       if (
  10.         (defaultFilter && defaultFilter.filterValueList.length > 0) ||
  11.         customFilter
  12.       ) {
  13.         // Filter idea:
  14.         //  Filter based on the criteria
  15.         //  Store the result back in the temporary storage (cache)
  16.         //  Find the intersection of all the temporary result
  17.         //    => The result will satisfy all the filter criteria
  18.         filterResult = dataList.filter((entry) => {
  19.           if (customFilter) {
  20.             return customFilter.filterHandler(entry);
  21.           }
  22.  
  23.           if (!defaultFilter) {
  24.             throw new Error(internalErrorMessage.invalidFilterParam);
  25.           }
  26.  
  27.           const { filterKey, filterValueList, filterType } = defaultFilter;
  28.           const toCompare = lodash.get(entry, filterKey);
  29.           if (Array.isArray(toCompare)) {
  30.             const intersection = lodash.intersectionWith(
  31.               toCompare,
  32.               filterValueList,
  33.               lodash.isEqual
  34.             );
  35.  
  36.             switch (filterType) {
  37.               case "matchSubset":
  38.                 // toCompare is the subset of filterValueList
  39.                 return intersection.length === toCompare.length;
  40.               case "matchSuperset":
  41.                 // filterValueList is the subset of toCompare
  42.                 return intersection.length === filterValueList.length;
  43.               case "matchExact":
  44.                 return (
  45.                   intersection.length === toCompare.length &&
  46.                   intersection.length === filterValueList.length
  47.                 );
  48.               default:
  49.               case "matchOneOf":
  50.                 return intersection.length > 0;
  51.             }
  52.           }
  53.           return (
  54.             filterValueList.findIndex((value) =>
  55.               lodash.isEqual(toCompare, value)
  56.             ) >= 0
  57.           );
  58.         });
  59.       }
  60.       filterCache[
  61.         customFilter?.customKey || (defaultFilter?.filterKey as string)
  62.       ] = filterResult;
  63.  
  64.       // Intersect the result of every filter by in the cache
  65.       state.filterResult = lodash.intersectionWith(
  66.         ...Object.values(filterCache),
  67.         lodash.isEqual
  68.       );
  69.       // Reset the cache
  70.       state.filterCache = filterCache;
  71.     }),
  72.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement