Sha256: 087d1ca5720ebab04bb0ecd9589950647683d2d2596d28d8dbabbbca5cf319f8
Contents?: true
Size: 955 Bytes
Versions: 126
Compression:
Stored size: 955 Bytes
Contents
# frozen_string_literal: true require "concurrent/map" class ReeObject::IsBlank include Ree::FnDSL fn :is_blank BLANK_RE = /\A[[:space:]]*\z/ ENCODED_BLANKS = Concurrent::Map.new do |h, enc| h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING) end doc(<<~DOC) An object is blank if it's false, empty, or a whitespace string. For example, +nil+, '', ' ', [], {}, and +false+ are all blank. DOC contract(Any => Bool) def call(obj) return is_string_blank?(obj) if obj.is_a?(String) return obj.empty? if obj.is_a?(Array) || obj.is_a?(Hash) || obj.is_a?(Set) return true if obj.nil? return true if obj == false return false if obj == true false end private def is_string_blank?(str) str.empty? || begin BLANK_RE.match?(str) rescue Encoding::CompatibilityError ENCODED_BLANKS[str.encoding].match?(str) end end end
Version data entries
126 entries across 126 versions & 1 rubygems