Fix Searchable
when collection contains nil
.
This commit is contained in:
parent
c5f92bfb8b
commit
8f462ddb5d
@ -17,7 +17,8 @@ module Searchable
|
|||||||
def search_regex(regex)
|
def search_regex(regex)
|
||||||
select do |*args|
|
select do |*args|
|
||||||
args = yield(*args) if block_given?
|
args = yield(*args) if block_given?
|
||||||
[*args].any? { |arg| arg.match?(regex) }
|
args = [*args].compact
|
||||||
|
args.any? { |arg| arg.match?(regex) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -25,7 +26,8 @@ module Searchable
|
|||||||
simplified_string = simplify_string(string)
|
simplified_string = simplify_string(string)
|
||||||
select do |*args|
|
select do |*args|
|
||||||
args = yield(*args) if block_given?
|
args = yield(*args) if block_given?
|
||||||
[*args].any? { |arg| simplify_string(arg).include?(simplified_string) }
|
args = [*args].compact
|
||||||
|
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
require "searchable"
|
require "searchable"
|
||||||
|
|
||||||
describe Searchable do
|
describe Searchable do
|
||||||
subject { ary.extend(described_class) }
|
subject { collection.extend(described_class) }
|
||||||
|
|
||||||
let(:ary) { ["with-dashes"] }
|
let(:collection) { ["with-dashes"] }
|
||||||
|
|
||||||
describe "#search" do
|
describe "#search" do
|
||||||
context "when given a block" do
|
context "when given a block" do
|
||||||
let(:ary) { [["with-dashes", "withdashes"]] }
|
let(:collection) { [["with-dashes", "withdashes"]] }
|
||||||
|
|
||||||
it "searches by the selected argument" do
|
it "searches by the selected argument" do
|
||||||
expect(subject.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
|
expect(subject.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
|
||||||
@ -26,5 +26,21 @@ describe Searchable do
|
|||||||
expect(subject.search("with dashes")).to eq ["with-dashes"]
|
expect(subject.search("with dashes")).to eq ["with-dashes"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when searching a Hash" do
|
||||||
|
let(:collection) { { "foo" => "bar" } }
|
||||||
|
|
||||||
|
it "returns a Hash" do
|
||||||
|
expect(subject.search("foo")).to eq "foo" => "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
context "containing nil" do
|
||||||
|
let(:collection) { { "foo" => nil } }
|
||||||
|
|
||||||
|
it "does not raise an error" do
|
||||||
|
expect(subject.search("foo")).to eq "foo" => nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user