Sha256: 9310ce6d79e6f5d6d14192d1ffa437ee7a14afe9bea112f2226dd14cd0c6d9e4

Contents?: true

Size: 898 Bytes

Versions: 5

Compression:

Stored size: 898 Bytes

Contents

# frozen_string_literal: true

# Invoca ::Hash extensions
class Hash
  def select_hash(&block)
    res = {}
    each { |k, v| res[k] = v if (block.arity == 1 ? yield(v) : yield(k, v)) } # rubocop:disable Style/ParenthesesAroundCondition
    res
  end

  def map_hash(&block)
    res = {}
    each { |k, v| res[k] = block.arity == 1 ? yield(v) : yield(k, v) }
    res
  end

  def partition_hash(keys = nil)
    yes = {}
    no = {}
    each do |k, v|
      if block_given? ? yield(k, v) : keys.include?(k)
        yes[k] = v
      else
        no[k] = v
      end
    end
    [yes, no]
  end

  # rubocop:disable Naming/BinaryOperatorParameterName
  def -(keys)
    res = {}
    each_pair { |k, v| res[k] = v unless k.in?(keys) }
    res
  end

  def &(keys)
    res = {}
    keys.each { |k| res[k] = self[k] if has_key?(k) }
    res
  end
  # rubocop:enable Naming/BinaryOperatorParameterName
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
invoca-utils-0.6.0 lib/invoca/utils/hash.rb
invoca-utils-0.5.1 lib/invoca/utils/hash.rb
invoca-utils-0.5.0 lib/invoca/utils/hash.rb
invoca-utils-0.4.1 lib/invoca/utils/hash.rb
invoca-utils-0.4.0 lib/invoca/utils/hash.rb