<commit_before># frozen_string_literal: true

require "how_is/sources/github"
require "date"

module HowIs
  module Sources
    module GithubHelpers
      def obj_to_array_of_hashes(object)
        object.to_a.map(&:to_h)
      end

      # Given an Array of issues or pulls, return a Hash specifying how many
      # issues or pulls use each label.
      #
      # Returned hash maps labels to frequency.
      # E.g., given 10 issues/pulls with label "label1" and 5 with label "label2",
      # {
      #   "label1" => 10,
      #   "label2" => 5
      # }
      def num_with_label(issues_or_pulls)
        hash = Hash.new(0)
        issues_or_pulls.each do |iop|
          next unless iop["labels"]

          iop["labels"].each do |label|
            hash[label["name"]] += 1
          end
        end
        hash
      end

      # Returns the number of issues with no label.
      def num_with_no_label(issues)
        issues.select { |x| x["labels"].empty? }.length
      end

      # Given an Array of dates, average the timestamps and return the date that
      # represents.
      def average_date_for(issues_or_pulls)
        timestamps = issues_or_pulls.map { |iop| DateTime.parse(iop["createdAt"]).strftime("%s").to_i }
        average_timestamp = timestamps.reduce(:+) / issues_or_pulls.length

        DateTime.strptime(average_timestamp.to_s, "%s")
      end

      # Given an Array of issues or pulls, return the average age of them.
      # Returns nil if no issues or pulls are provided.
      def average_age_for(issues_or_pulls)
        return nil if issues_or_pulls.empty?

        ages = issues_or_pulls.map { |iop| time_ago_in_seconds(iop["createdAt"]) }
        average_age_in_seconds = ages.reduce(:+) / ages.length

        values = period_pairs_for(average_age_in_seconds) \
                 .reject { |(v, _)| v.zero? } \
                 .map { |(v, k)| pluralize(k, v) }

        value = values[0, 2].join(" and ")

        "approximately #{value}"
      end

      def sort_iops_by_created_at(issues_or_pulls)<commit_msg>Reformat code in GithubHelpers to match new Rubocop rule.<commit_after># frozen_string_literal: true

require "how_is/sources/github"
require "date"

module HowIs
  module Sources
    module GithubHelpers
      def obj_to_array_of_hashes(object)
        object.to_a.map(&:to_h)
      end

      # Given an Array of issues or pulls, return a Hash specifying how many
      # issues or pulls use each label.
      #
      # Returned hash maps labels to frequency.
      # E.g., given 10 issues/pulls with label "label1" and 5 with label "label2",
      # {
      #   "label1" => 10,
      #   "label2" => 5
      # }
      def num_with_label(issues_or_pulls)
        hash = Hash.new(0)
        issues_or_pulls.each do |iop|
          next unless iop["labels"]

          iop["labels"].each do |label|
            hash[label["name"]] += 1
          end
        end
        hash
      end

      # Returns the number of issues with no label.
      def num_with_no_label(issues)
        issues.select { |x| x["labels"].empty? }.length
      end

      # Given an Array of dates, average the timestamps and return the date that
      # represents.
      def average_date_for(issues_or_pulls)
        timestamps = issues_or_pulls.map { |iop| DateTime.parse(iop["createdAt"]).strftime("%s").to_i }
        average_timestamp = timestamps.reduce(:+) / issues_or_pulls.length

        DateTime.strptime(average_timestamp.to_s, "%s")
      end

      # Given an Array of issues or pulls, return the average age of them.
      # Returns nil if no issues or pulls are provided.
      def average_age_for(issues_or_pulls)
        return nil if issues_or_pulls.empty?

        ages = issues_or_pulls.map { |iop| time_ago_in_seconds(iop["createdAt"]) }
        average_age_in_seconds = ages.reduce(:+) / ages.length

        values =
          period_pairs_for(average_age_in_seconds) \
            .reject { |(v, _)| v.zero? } \
            .map { |(v, k)| pluralize(k, v) }

        value = values[0, 2].join(" and ")

        "approximately #{value}"
      end

      def sort_iops_by_created_at(issues_or_pulls)
{'fraction_non_alphanumeric': 0.10043352601156069, 'fraction_numerical': 0.00674373795761079, 'mean_word_length': 2.3224, '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': '4874935', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1297, 'n_words': 351}