<commit_before>const $ = require('jquery');

const logger = require('logger.js');

function prepareDupeSpace() {
  $('[data-fate-weapon-name]').each(function(index,element) {
    if ($(this).children('.fate-dupe.fate-glyph.fglyph-knives').length > 0) {
      return;
    }
    $(this).append($('<div>', {'class': 'fate-glyph fate-dupe fglyph-knives', 'style':'display:none'}));
  });
}

function calculateWorkingSet() {
  const weapons = new Map();
  $('[data-fate-weapon-name]').each(function(index,element) {
    const weaponName = $(this).attr('data-fate-weapon-name');
    const weaponData = {
      name: weaponName,
      domElement: this,
      light: parseInt($(this).children('.item-stat').text().replace(/M/,''))
    };
    if (weapons.has(weaponName)) {
        weapons.set(weaponName, weapons.get(weaponName).concat(weaponData));
    } else {
        weapons.set(weaponName, [weaponData]);
    }
  });
  return weapons;
}

function styleDupeIndicators(weapons) {
  for (let [weaponName, weaponInstances] of weapons) {
    if (weaponInstances.length === 1) {
      $(weaponInstances[0].domElement).children('.fate-dupe').hide();
      continue;
    }
    const maxLight = Math.max(...weaponInstances.map(function(w) {return w.light;}));
    weaponInstances.forEach(function(weapon) {
      $(weapon.domElement).find('.fate-dupe').removeClass('fate-negative fate-positive');
      $(weapon.domElement).find('.fate-dupe').addClass(weapon.light < maxLight ? 'fate-negative' : 'fate-positive');
      $(weapon.domElement).find('.fate-dupe').show();
    });
  }
}

function onMouseEnter() {
  const weaponName = $(this).parent().attr('data-fate-weapon-name');
  $('[data-fate-weapon-name]').not('[data-fate-weapon-name="'+weaponName+'"]').addClass('fate-search-hidden');
}

function onMouseLeave() {
  $('.fate-search-hidden').toggleClass();
}

function registerListeners() {
  $('.fate-dupe:visible').on('mouseenter.dupe', onMouseEnter);
  $('.fate-dupe:visible').on('mouseleave.dupe', onMouseLeave);
}

fateBus.subscribe(module, 'fate.refresh', function() {
  logger.log('dupeIndicator.js: Calculating duplicates');
  prepareDupeSpace();
  styleDupeIndicators(calculateWorkingSet());
  registerListeners();
});

/*
  jasmine-jquery doesn't seem to play well these days. Not sure why but it can't
  seem to trigger events via $.trigger, so we're going to use our bus to test the
  events.
*/
fateBus.subscribe(module, 'fate.test.mouseenter.dupe', function() {
  $('.fate-dupe:visible').each(onMouseEnter);
});

fateBus.subscribe(module, 'fate.test.mouseleave.dupe', function() {
  $('.fate-dupe:visible').each(onMouseLeave);
});
<commit_msg>Fix a bug due to misunderstanding of $.toggleClass()<commit_after>const $ = require('jquery');

const logger = require('logger.js');

function prepareDupeSpace() {
  $('[data-fate-weapon-name]').each(function(index,element) {
    if ($(this).children('.fate-dupe.fate-glyph.fglyph-knives').length > 0) {
      return;
    }
    $(this).append($('<div>', {'class': 'fate-glyph fate-dupe fglyph-knives', 'style':'display:none'}));
  });
}

function calculateWorkingSet() {
  const weapons = new Map();
  $('[data-fate-weapon-name]').each(function(index,element) {
    const weaponName = $(this).attr('data-fate-weapon-name');
    const weaponData = {
      name: weaponName,
      domElement: this,
      light: parseInt($(this).children('.item-stat').text().replace(/M/,''))
    };
    if (weapons.has(weaponName)) {
        weapons.set(weaponName, weapons.get(weaponName).concat(weaponData));
    } else {
        weapons.set(weaponName, [weaponData]);
    }
  });
  return weapons;
}

function styleDupeIndicators(weapons) {
  for (let [weaponName, weaponInstances] of weapons) {
    if (weaponInstances.length === 1) {
      $(weaponInstances[0].domElement).children('.fate-dupe').hide();
      continue;
    }
    const maxLight = Math.max(...weaponInstances.map(function(w) {return w.light;}));
    weaponInstances.forEach(function(weapon) {
      $(weapon.domElement).find('.fate-dupe').removeClass('fate-negative fate-positive');
      $(weapon.domElement).find('.fate-dupe').addClass(weapon.light < maxLight ? 'fate-negative' : 'fate-positive');
      $(weapon.domElement).find('.fate-dupe').show();
    });
  }
}

function onMouseEnter() {
  const weaponName = $(this).parent().attr('data-fate-weapon-name');
  $('[data-fate-weapon-name]').not('[data-fate-weapon-name="'+weaponName+'"]').addClass('fate-search-hidden');
}

function onMouseLeave() {
  $('.fate-search-hidden').removeClass('fate-search-hidden');
}

function registerListeners() {
  $('.fate-dupe:visible').on('mouseenter.dupe', onMouseEnter);
  $('.fate-dupe:visible').on('mouseleave.dupe', onMouseLeave);
}

fateBus.subscribe(module, 'fate.refresh', function() {
  logger.log('dupeIndicator.js: Calculating duplicates');
  prepareDupeSpace();
  styleDupeIndicators(calculateWorkingSet());
  registerListeners();
});

/*
  jasmine-jquery doesn't seem to play well these days. Not sure why but it can't
  seem to trigger events via $.trigger, so we're going to use our bus to test the
  events.
*/
fateBus.subscribe(module, 'fate.test.mouseenter.dupe', function() {
  $('.fate-dupe:visible').each(onMouseEnter);
});

fateBus.subscribe(module, 'fate.test.mouseleave.dupe', function() {
  $('.fate-dupe:visible').each(onMouseLeave);
});

{'fraction_non_alphanumeric': 0.19570494864612512, 'fraction_numerical': 0.0011204481792717086, 'mean_word_length': 4.448626653102747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6918272', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1833, 'n_words': 275}