brew/Library/Homebrew/extend/enumerable.rb

31 lines
966 B
Ruby
Raw Normal View History

2024-01-11 14:19:44 -08:00
# typed: strict
2024-01-10 12:11:55 -08:00
# frozen_string_literal: true
module Enumerable
# The negative of the <tt>Enumerable#include?</tt>. Returns +true+ if the
# collection does not include the object.
2024-01-11 14:19:44 -08:00
sig { params(object: T.untyped).returns(T::Boolean) }
def exclude?(object) = !include?(object)
2024-01-10 12:11:55 -08:00
# Returns a new +Array+ without the blank items.
# Uses Object#blank? for determining if an item is blank.
#
# [1, "", nil, 2, " ", [], {}, false, true].compact_blank
# # => [1, 2, true]
2024-01-10 12:11:55 -08:00
#
# Set.new([nil, "", 1, false]).compact_blank
# # => [1]
2024-01-10 12:11:55 -08:00
#
# When called on a +Hash+, returns a new +Hash+ without the blank values.
#
# { a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank
# # => { b: 1, f: true }
2024-01-11 14:19:44 -08:00
sig { returns(T.self_type) }
def compact_blank = T.unsafe(self).reject(&:blank?)
2024-01-10 12:11:55 -08:00
end
class Hash
# Hash#reject has its own definition, so this needs one too.
2024-01-22 10:12:22 -08:00
def compact_blank = reject { |_k, v| T.unsafe(v).blank? }
2024-01-10 12:11:55 -08:00
end