<commit_before>var bracketData = require('bracket-data')
var BracketValidator = require('bracket-validator')
var _extend = require('lodash/assign')
var _defaults = require('lodash/defaults')
var _pick = require('lodash/pick')
var _find = require('lodash/find')
var _each = require('lodash/forEach')
var _map = require('lodash/map')
var _isNumber = require('lodash/isNumber')
var _isArray = require('lodash/isArray')
var _values = require('lodash/values')
var _compact = require('lodash/compact')
var _intersection = require('lodash/intersection')
var _shuffle = require('lodash/shuffle')

var _constant = function (item) {
  return item
}

var allIndices = function (arr, val) {
  var indices = []
  var i = -1
  while ((i = arr.indexOf(val, i + 1)) !== -1) {
    indices.push(i)
  }
  return indices
}

var teamNameMatches = function (team1, team2) {
  var team1Name = team1 && team1.name
  var team1Names = team1 && team1.names
  var team2Name = team2 && team2.name
  var team2Names = team2 && team2.names

  if (!_isArray(team1Name)) {
    team1Name = [team1Name]
  }

  if (team1Names) {
    team1Name = team1Name.concat(team1Names)
  }

  team1Name = _compact(team1Name.map(function (name) {
    return typeof name === 'string' ? name.toLowerCase() : null
  }))

  if (!_isArray(team2Name)) {
    team2Name = [team2Name]
  }

  if (team2Names) {
    team2Name = team1Name.concat(team2Names)
  }

  team2Name = _compact(team2Name.map(function (name) {
    return typeof name === 'string' ? name.toLowerCase() : null
  }))

  if (team1Name.length && team2Name.length) {
    return _intersection(team1Name, team2Name).length > 0
  }

  return false
}

var seedMatches = function (team1, team2) {
  return team1 && team2 && parseInt(team1.seed) === parseInt(team2.seed)
}

function Updater (options) {
  this.bracketData = bracketData({
    sport: options.sport,
    year: options.year
  })

  this.validator = new BracketValidator({
    sport: options.sport,
    year: options.year
  })

  return this.reset(options)
}

Updater.prototype.reset = function (options) {
  _defaults(options || {}, {
    winner: {},
    loser: {},
    fromRegion: ''
  })

  if (typeof options.winner === 'number' || !isNaN(options.winner)) options.winner = {seed: parseInt(options.winner, 10)}
  if (typeof options.loser === 'number' || !isNaN(options.loser)) options.loser = {seed: parseInt(options.loser, 10)}
  if (typeof options.winner === 'string' && isNaN(options.winner)) options.winner = {name: options.winner}
  if (typeof options.loser === 'string' && isNaN(options.loser)) options.loser = {name: options.loser}

  // If we got passed in null or something, set the properties we need to not break
  if (!options.winner) options.winner = {}
  if (!options.loser) options.loser = {}
  if (!options.winner.name) options.winner.name = ''
  if (!options.loser.name) options.loser.name = ''

  _extend(this, _pick(options, 'winner', 'loser', 'fromRegion', 'currentMaster'))

  return this
}

Updater.prototype.hasWinner = function () {
  return !!(this.winner && (this.winner.name || this.winner.seed))
}

Updater.prototype.hasLoser = function () {
  return !!(this.loser && (this.loser.name || this.loser.seed))
}

Updater.prototype.isFinal = function () {
  var finalName = this.bracketData.constants.FINAL_NAME.toLowerCase()
  var finalFullname = this.bracketData.constants.FINAL_FULLNAME.toLowerCase()
  var finalId = this.bracketData.constants.FINAL_ID.toLowerCase()
  var region = this.fromRegion.toLowerCase()

  return region === finalName || region === finalFullname || region === finalId
}

Updater.prototype.isChampionship = function () {
  var championshipName = this.bracketData.constants.FINAL_CHAMPIONSHIP_NAME
  return championshipName ? this.fromRegion.toLowerCase() === championshipName.toLowerCase() : false
}

Updater.prototype.teamMatches = function (team1, team2) {
  if (this.isFinal()) {
    return teamNameMatches(team1, team2)
  } else {
    return seedMatches(team1, team2)
  }
}

Updater.prototype.gameMatches = function (winner, loser) {
  return this.teamMatches(winner, this.winner) && this.teamMatches(loser, this.loser)
}

Updater.prototype.getSeed = function (winner) {
  if (this.isFinal()) {
    var finalTeams = this.validated[this.bracketData.constants.FINAL_ID].rounds[0]
    var finalTeam = _find(finalTeams, function (team) {
      return teamNameMatches(team, winner)
    })
    return {fromRegion: finalTeam.fromRegion}
  } else {
    return {seed: winner.seed}
  }
}

Updater.prototype.flatten = function (bracket) {
  var self = this
  var flattenedBracket = ''
  _each(bracket, function (bracketRegion) {
    var regionString = _map(bracketRegion.rounds, function (round, roundIndex) {
      if (roundIndex === 0) return ''
      return _map(round, function (roundGame) {
        var roundValue
        var pc = roundGame && (roundGame.winsIn || roundGame.playedCompetitions)
        if (roundGame === null) {
          return self.bracketData.constants.UNPICKED_MATCH
        } else if (_isNumber(roundGame) || !isNaN(roundGame)) {
          roundValue = roundGame<commit_msg>only use seed if available; otherwise fallback to name<commit_after>var bracketData = require('bracket-data')
var BracketValidator = require('bracket-validator')
var _extend = require('lodash/assign')
var _defaults = require('lodash/defaults')
var _pick = require('lodash/pick')
var _find = require('lodash/find')
var _each = require('lodash/forEach')
var _map = require('lodash/map')
var _isNumber = require('lodash/isNumber')
var _isArray = require('lodash/isArray')
var _values = require('lodash/values')
var _compact = require('lodash/compact')
var _intersection = require('lodash/intersection')
var _shuffle = require('lodash/shuffle')

var _constant = function (item) {
  return item
}

var allIndices = function (arr, val) {
  var indices = []
  var i = -1
  while ((i = arr.indexOf(val, i + 1)) !== -1) {
    indices.push(i)
  }
  return indices
}

var teamNameMatches = function (team1, team2) {
  var team1Name = team1 && team1.name
  var team1Names = team1 && team1.names
  var team2Name = team2 && team2.name
  var team2Names = team2 && team2.names

  if (!_isArray(team1Name)) {
    team1Name = [team1Name]
  }

  if (team1Names) {
    team1Name = team1Name.concat(team1Names)
  }

  team1Name = _compact(team1Name.map(function (name) {
    return typeof name === 'string' ? name.toLowerCase() : null
  }))

  if (!_isArray(team2Name)) {
    team2Name = [team2Name]
  }

  if (team2Names) {
    team2Name = team1Name.concat(team2Names)
  }

  team2Name = _compact(team2Name.map(function (name) {
    return typeof name === 'string' ? name.toLowerCase() : null
  }))

  if (team1Name.length && team2Name.length) {
    return _intersection(team1Name, team2Name).length > 0
  }

  return false
}

var seedMatches = function (team1, team2) {
  return team1 && team2 && parseInt(team1.seed) === parseInt(team2.seed)
}

function Updater (options) {
  this.bracketData = bracketData({
    sport: options.sport,
    year: options.year
  })

  this.validator = new BracketValidator({
    sport: options.sport,
    year: options.year
  })

  return this.reset(options)
}

Updater.prototype.reset = function (options) {
  _defaults(options || {}, {
    winner: {},
    loser: {},
    fromRegion: ''
  })

  if (typeof options.winner === 'number' || !isNaN(options.winner)) options.winner = {seed: parseInt(options.winner, 10)}
  if (typeof options.loser === 'number' || !isNaN(options.loser)) options.loser = {seed: parseInt(options.loser, 10)}
  if (typeof options.winner === 'string' && isNaN(options.winner)) options.winner = {name: options.winner}
  if (typeof options.loser === 'string' && isNaN(options.loser)) options.loser = {name: options.loser}

  // If we got passed in null or something, set the properties we need to not break
  if (!options.winner) options.winner = {}
  if (!options.loser) options.loser = {}
  if (!options.winner.name) options.winner.name = ''
  if (!options.loser.name) options.loser.name = ''

  _extend(this, _pick(options, 'winner', 'loser', 'fromRegion', 'currentMaster'))

  return this
}

Updater.prototype.hasWinner = function () {
  return !!(this.winner && (this.winner.name || this.winner.seed))
}

Updater.prototype.hasLoser = function () {
  return !!(this.loser && (this.loser.name || this.loser.seed))
}

Updater.prototype.isFinal = function () {
  var finalName = this.bracketData.constants.FINAL_NAME.toLowerCase()
  var finalFullname = this.bracketData.constants.FINAL_FULLNAME.toLowerCase()
  var finalId = this.bracketData.constants.FINAL_ID.toLowerCase()
  var region = this.fromRegion.toLowerCase()

  return region === finalName || region === finalFullname || region === finalId
}

Updater.prototype.isChampionship = function () {
  var championshipName = this.bracketData.constants.FINAL_CHAMPIONSHIP_NAME
  return championshipName ? this.fromRegion.toLowerCase() === championshipName.toLowerCase() : false
}

Updater.prototype.teamMatches = function (team1, team2) {
  if (this.isFinal()) {
    return teamNameMatches(team1, team2)
  }

  if (team1.seed && team2.seed) {
    return seedMatches(team1, team2)
  }

  return teamNameMatches(team1, team2)
}

Updater.prototype.gameMatches = function (winner, loser) {
  return this.teamMatches(winner, this.winner) && this.teamMatches(loser, this.loser)
}

Updater.prototype.getSeed = function (winner) {
  if (this.isFinal()) {
    var finalTeams = this.validated[this.bracketData.constants.FINAL_ID].rounds[0]
    var finalTeam = _find(finalTeams, function (team) {
      return teamNameMatches(team, winner)
    })
    return {fromRegion: finalTeam.fromRegion}
  } else {
    return {seed: winner.seed}
  }
}

Updater.prototype.flatten = function (bracket) {
  var self = this
  var flattenedBracket = ''
  _each(bracket, function (bracketRegion) {
    var regionString = _map(bracketRegion.rounds, function (round, roundIndex) {
      if (roundIndex === 0) return ''
      return _map(round, function (roundGame) {
        var roundValue
        var pc = roundGame && (roundGame.winsIn || roundGame.playedCompetitions)
        if (roundGame === null) {
          return self.bracketData.constants.UNPICKED_MATCH
        } else if (_isNumber(roundGame) || !isNaN(roundGame)) {
          roundValue = roundGame
{'fraction_non_alphanumeric': 0.1392466350343759, 'fraction_numerical': 0.011620025176721217, 'mean_word_length': 4.080177078209543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1098898', 'n_tokens_mistral': 3488, 'n_tokens_neox': 3453, 'n_words': 746}