aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorDana Sherson <robot@dana.sh>2019-06-05 17:25:18 +1000
committerDana Sherson <robot@dana.sh>2019-06-05 18:18:17 +1000
commitc8847c17a7c9ae75c44c522c56ccd9c5fca25ea7 (patch)
tree057901f401f10fb98615b9bca73c88d110c13fba /activesupport/test
parentee10e05af27facf42b1c49f8aaa0463d3af6989c (diff)
downloadrails-c8847c17a7c9ae75c44c522c56ccd9c5fca25ea7.tar.gz
rails-c8847c17a7c9ae75c44c522c56ccd9c5fca25ea7.tar.bz2
rails-c8847c17a7c9ae75c44c522c56ccd9c5fca25ea7.zip
Add compact_blank shortcut for reject(&:blank?)
I frequently find myself having to .compact but for blank. which means on an array reject(&:blank?) (this is fine), or, on a hash `.reject { |_k, v| v.blank? }` which is slightly more frustrating and i usually write it as .reject(&:blank?) first and am confused when it's trying to check if the keys are blank. I've added the analagous .compact_blank! where there's a reject! to build on (there's also a reject! in Set, but there's no other core_ext touching Set so i've left that alone)
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 381b5a1f32..a9bf4b82f4 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -242,4 +242,28 @@ class EnumerableTests < ActiveSupport::TestCase
])
assert_equal [[5, 99], [15, 0], [10, 50]], payments.pluck(:dollars, :cents)
end
+
+ def test_compact_blank
+ values = GenericEnumerable.new([1, "", nil, 2, " ", [], {}, false, true])
+
+ assert_equal [1, 2, true], values.compact_blank
+ end
+
+ def test_array_compact_blank!
+ values = [1, "", nil, 2, " ", [], {}, false, true]
+ values.compact_blank!
+
+ assert_equal [1, 2, true], values
+ end
+
+ def test_hash_compact_blank
+ values = { a: "", b: 1, c: nil, d: [], e: false, f: true }
+ assert_equal({ b: 1, f: true }, values.compact_blank)
+ end
+
+ def test_hash_compact_blank!
+ values = { a: "", b: 1, c: nil, d: [], e: false, f: true }
+ values.compact_blank!
+ assert_equal({ b: 1, f: true }, values)
+ end
end