<commit_before>// parses an avatar name
// Searches for a dot or white space that separates the first and last names
// if neither is given, then 'resident' will be used for the last name

// first.last and first last will become {first: 'first', last: 'last'}

function cleanName (name: string) {
  // deletes characters that will be in names but shouldn't
  const trimmed = name.trim().replace(/["\0]/gi, '')
  const upperCased = trimmed.charAt(0).toUpperCase() + // name -> Name
    trimmed.substring(1).toLowerCase()
  return upperCased
}

export default class AvatarName {
  first: string
  last: string
  displayName: string
  isUsingDisplayName: boolean
  didLoadDisplayName: boolean
  isLoadingDisplayName: boolean

  constructor (name: AvatarName | string | { first: string, last: string }) {
    if (name instanceof AvatarName) {
      this.first = name.first
      this.last = name.last
      this.displayName = name.displayName
      this.isUsingDisplayName = name.isUsingDisplayName
      this.didLoadDisplayName = name.didLoadDisplayName
      this.isLoadingDisplayName = name.isLoadingDisplayName
      return
    } else if (typeof name === 'object' && typeof name.first === 'string') {
      this.first = cleanName(name.first)
      this.last = cleanName(name.last || 'Resident')
    } else if (typeof name === 'string' && arguments.length === 1) {
      var separator = name.match(/[.\s]/) // either a dot or a space
      if (separator) {
        var parts = name.split(separator[0])
        this.first = cleanName(parts[0])
        this.last = cleanName(parts[1])
      } else {
        this.first = cleanName(name)
        this.last = 'Resident'
      }
    } else if (typeof name === 'string' && typeof arguments[1] === 'string') {
      this.first = cleanName(name)
      this.last = cleanName(arguments[1])
    } else {
      throw new TypeError(`couldn't parse ${name}`)
    }
    this.displayName = ''
    this.isUsingDisplayName = false
    this.didLoadDisplayName = false
    this.isLoadingDisplayName = false
  }

  getFullName () {
    return `${this.first} ${this.last}`
  }

  getName () {
    if (this.last === 'Resident') {
      return this.first
    } else {
      return this.getFullName()
    }
  }

  getDisplayName () {
    if (this.didLoadDisplayName && this.isUsingDisplayName) {
      return `${this.displayName} (${this.getName()})`
    } else {
      return this.getName()
    }
  }

  toString () {
    return this.getDisplayName()
  }

  compare (other: AvatarName, strict: boolean = false) {
    if (strict && !(other instanceof AvatarName)) {
      return false
    }

    const otherName = typeof other === 'string' ? new AvatarName(other) : other

    return otherName.first === this.first && otherName.last === this.last
  }

  willHaveDisplayName () {
    return this.didLoadDisplayName || this.isLoadingDisplayName || this.displayName.length > 0
  }

  withIsLoadingSetTo (isLoading: boolean) {
    const next = new AvatarName(this)
    next.isLoadingDisplayName = isLoading
    return next
  }

  withDisplayNameSetTo (displayName: string, legacyFirstName: string, legacyLastName: string) {
    const next = new AvatarName(this)

    if (legacyFirstName != null) {
      next.first = legacyFirstName
    }
    if (legacyLastName != null) {
      next.last = legacyLastName
    }

    next.isLoadingDisplayName = false
    next.didLoadDisplayName = true
    next.displayName = displayName
    next.isUsingDisplayName = next.getName() !== displayName
    return next
  }
}
<commit_msg>refactor(avatarName): add optional arguments<commit_after>// parses an avatar name
// Searches for a dot or white space that separates the first and last names
// if neither is given, then 'resident' will be used for the last name

// first.last and first last will become {first: 'first', last: 'last'}

function cleanName (name: string) {
  // deletes characters that will be in names but shouldn't
  const trimmed = name.trim().replace(/["\0]/gi, '')
  const upperCased = trimmed.charAt(0).toUpperCase() + // name -> Name
    trimmed.substring(1).toLowerCase()
  return upperCased
}

export default class AvatarName {
  first: string
  last: string
  displayName: string
  isUsingDisplayName: boolean
  didLoadDisplayName: boolean
  isLoadingDisplayName: boolean

  constructor (name: AvatarName | string | { first: string, last?: string }, lastName?: string) {
    if (name instanceof AvatarName) {
      this.first = name.first
      this.last = name.last
      this.displayName = name.displayName
      this.isUsingDisplayName = name.isUsingDisplayName
      this.didLoadDisplayName = name.didLoadDisplayName
      this.isLoadingDisplayName = name.isLoadingDisplayName
      return
    } else if (typeof name === 'object' && typeof name.first === 'string') {
      this.first = cleanName(name.first)
      this.last = cleanName(name.last || 'Resident')
    } else if (typeof name === 'string' && arguments.length === 1) {
      var separator = name.match(/[.\s]/) // either a dot or a space
      if (separator) {
        var parts = name.split(separator[0])
        this.first = cleanName(parts[0])
        this.last = cleanName(parts[1])
      } else {
        this.first = cleanName(name)
        this.last = 'Resident'
      }
    } else if (typeof name === 'string' && typeof lastName === 'string') {
      this.first = cleanName(name)
      this.last = cleanName(lastName)
    } else {
      throw new TypeError(`couldn't parse ${name}`)
    }
    this.displayName = ''
    this.isUsingDisplayName = false
    this.didLoadDisplayName = false
    this.isLoadingDisplayName = false
  }

  getFullName () {
    return `${this.first} ${this.last}`
  }

  getName () {
    if (this.last === 'Resident') {
      return this.first
    } else {
      return this.getFullName()
    }
  }

  getDisplayName () {
    if (this.didLoadDisplayName && this.isUsingDisplayName) {
      return `${this.displayName} (${this.getName()})`
    } else {
      return this.getName()
    }
  }

  toString () {
    return this.getDisplayName()
  }

  compare (other: AvatarName, strict: boolean = false) {
    if (strict && !(other instanceof AvatarName)) {
      return false
    }

    const otherName = typeof other === 'string' ? new AvatarName(other) : other

    return otherName.first === this.first && otherName.last === this.last
  }

  willHaveDisplayName () {
    return this.didLoadDisplayName || this.isLoadingDisplayName || this.displayName.length > 0
  }

  withIsLoadingSetTo (isLoading: boolean) {
    const next = new AvatarName(this)
    next.isLoadingDisplayName = isLoading
    return next
  }

  withDisplayNameSetTo (displayName: string, legacyFirstName: string, legacyLastName: string) {
    const next = new AvatarName(this)

    if (legacyFirstName != null) {
      next.first = legacyFirstName
    }
    if (legacyLastName != null) {
      next.last = legacyLastName
    }

    next.isLoadingDisplayName = false
    next.didLoadDisplayName = true
    next.displayName = displayName
    next.isUsingDisplayName = next.getName() !== displayName
    return next
  }
}

{'fraction_non_alphanumeric': 0.11901081916537867, 'fraction_numerical': 0.0025291555430658986, 'mean_word_length': 2.998876404494382, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4180728', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2109, 'n_words': 557}