Sha256: 456faa5625e89fb2fb9228a9696e777f69020c3f3b37ea25c5b0b0468f61806d

Contents?: true

Size: 1.57 KB

Versions: 6930

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for dealing with duplication.
    module Duplication
      private

      # Whether the `collection` contains any duplicates.
      #
      # @param [Array] collection an array to check for duplicates
      # @return [Boolean] whether the array contains any duplicates
      def duplicates?(collection)
        collection.size > 1 && collection.size > collection.uniq.size
      end

      # Returns all duplicates, including the first instance of the duplicated
      # elements.
      #
      # @param [Array] collection an array to return duplicates for
      # @return [Array] all the duplicates
      def duplicates(collection)
        grouped_duplicates(collection).flatten
      end

      # Returns the consecutive duplicates, leaving out the first instance of
      # the duplicated elements.
      #
      # @param [Array] collection an array to return consecutive duplicates for
      # @return [Array] the consecutive duplicates
      def consecutive_duplicates(collection)
        grouped_duplicates(collection).flat_map { |items| items[1..-1] }
      end

      # Returns a hash of grouped duplicates. The key will be the first
      # instance of the element, and  the value an `array` of the initial
      # element and all duplicate instances.
      #
      # @param [Array] collection an array to group duplicates for
      # @return [Array] the grouped duplicates
      def grouped_duplicates(collection)
        collection.group_by { |item| item }.values.reject(&:one?)
      end
    end
  end
end

Version data entries

6,930 entries across 6,904 versions & 35 rubygems

Version Path
cybrid_api_organization_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_bank_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_id_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_organization_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_bank_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_organization_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_id_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_bank_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_id_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_organization_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
ory-client-1.15.17 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_bank_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_organization_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_id_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_organization_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_bank_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_id_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_organization_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_bank_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb
cybrid_api_id_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/mixin/duplication.rb