<commit_before>import { DimItem } from 'app/inventory/item-types';
import { RootState } from 'app/store/types';
import { emptyArray } from 'app/utils/empty';
import _ from 'lodash';
import { createSelector } from 'reselect';
import { getInventoryWishListRoll, InventoryWishListRoll } from './wishlists';

export const wishListsSelector = (state: RootState) => state.wishLists;

export const wishListsLastFetchedSelector = (state: RootState) =>
  wishListsSelector(state).lastFetched;

export const wishListsByHashSelector = createSelector(wishListsSelector, (wls) =>
  _.groupBy(wls.wishListAndInfo.wishListRolls?.filter(Boolean), (r) => r.itemHash)
);

export const wishListRollsForItemHashSelector = (itemHash: number) => (state: RootState) =>
  wishListsByHashSelector(state)[itemHash] ?? emptyArray();

export const hasWishListSelector = createSelector(
  wishListsByHashSelector,
  (wishlists) => !_.isEmpty(wishlists)
);

/** Returns a memoized function to look up wishlist by item, which is reset when the wishlist changes. Prefer wishListSelector */
export const wishListFunctionSelector = createSelector(
  wishListsByHashSelector,
  (wishlists): ((item: DimItem) => InventoryWishListRoll | undefined) => {
    // Cache of inventory item id to roll. For this to work, make sure vendor/collections rolls have unique ids.
    const cache = new Map<string, InventoryWishListRoll | undefined>();
    return (item: DimItem) => {
      if (cache.has(item.id)) {
        return cache.get(item.id);
      }
      const roll = getInventoryWishListRoll(item, wishlists);
      cache.set(item.id, roll);
      return roll;
    };
  }
);

/**
 * A reverse-curried selector that is easier to use in useSelector. This will re-render the component only when
 * this item's wishlist state changes.
 *
 * @example
 *
 * useSelector(wishListSelector(item))
 */
export const wishListSelector = (item: DimItem) => (state: RootState) =>
  wishListFunctionSelector(state)(item);
<commit_msg>eliminate false wishlist positives in vendor page<commit_after>import { DimItem } from 'app/inventory/item-types';
import { RootState } from 'app/store/types';
import { emptyArray } from 'app/utils/empty';
import _ from 'lodash';
import { createSelector } from 'reselect';
import { getInventoryWishListRoll, InventoryWishListRoll } from './wishlists';

export const wishListsSelector = (state: RootState) => state.wishLists;

export const wishListsLastFetchedSelector = (state: RootState) =>
  wishListsSelector(state).lastFetched;

export const wishListsByHashSelector = createSelector(wishListsSelector, (wls) =>
  _.groupBy(wls.wishListAndInfo.wishListRolls?.filter(Boolean), (r) => r.itemHash)
);

export const wishListRollsForItemHashSelector = (itemHash: number) => (state: RootState) =>
  wishListsByHashSelector(state)[itemHash] ?? emptyArray();

export const hasWishListSelector = createSelector(
  wishListsByHashSelector,
  (wishlists) => !_.isEmpty(wishlists)
);

/** Returns a memoized function to look up wishlist by item, which is reset when the wishlist changes. Prefer wishListSelector */
export const wishListFunctionSelector = createSelector(
  wishListsByHashSelector,
  (wishlists): ((item: DimItem) => InventoryWishListRoll | undefined) => {
    // Cache of inventory item id to roll. For this to work, make sure vendor/collections rolls have unique ids.
    const cache = new Map<string, InventoryWishListRoll | undefined>();
    return (item: DimItem) => {
      // don't cache vendor items because they don't have a safe cache key (yet)
      if (item.isVendorItem) {
        return getInventoryWishListRoll(item, wishlists);
      }

      if (cache.has(item.id)) {
        return cache.get(item.id);
      }
      const roll = getInventoryWishListRoll(item, wishlists);
      cache.set(item.id, roll);
      return roll;
    };
  }
);

/**
 * A reverse-curried selector that is easier to use in useSelector. This will re-render the component only when
 * this item's wishlist state changes.
 *
 * @example
 *
 * useSelector(wishListSelector(item))
 */
export const wishListSelector = (item: DimItem) => (state: RootState) =>
  wishListFunctionSelector(state)(item);

{'fraction_non_alphanumeric': 0.11244305921841286, 'fraction_numerical': 0.0, 'mean_word_length': 4.683923705722071, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4714863', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1236, 'n_words': 344}