Merge pull request #14916 from dduugg/refactor-searchable

Refactor searchable
This commit is contained in:
Mike McQuaid 2023-03-07 17:31:36 +00:00 committed by GitHub
commit bd309509da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 107 deletions

View File

@ -5,7 +5,6 @@ require "cask/cask_loader"
require "cask/config"
require "cask/dsl"
require "cask/metadata"
require "searchable"
require "utils/bottles"
module Cask
@ -16,7 +15,6 @@ module Cask
extend T::Sig
extend Forwardable
extend Searchable
extend Predicable
include Metadata

View File

@ -3,15 +3,12 @@
require "set"
require "cache_store"
require "searchable"
#
# {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used
# by the `brew desc` and `brew search` commands.
#
class DescriptionCacheStore < CacheStore
include Searchable
# Inserts a formula description into the cache if it does not exist or
# updates the formula description if it does exist.
#

View File

@ -3,7 +3,7 @@
require "formula"
require "formula_versions"
require "searchable"
require "search"
# Helper class for printing and searching descriptions.
#
@ -15,11 +15,11 @@ class Descriptions
results = case field
when :name
cache_store.search(string_or_regex) { |name, _| name }
Homebrew::Search.search(cache_store, string_or_regex) { |name, _| name }
when :desc
cache_store.search(string_or_regex) { |_, desc| desc }
Homebrew::Search.search(cache_store, string_or_regex) { |_, desc| desc }
when :either
cache_store.search(string_or_regex)
Homebrew::Search.search(cache_store, string_or_regex)
end
new(results)

View File

@ -1,7 +1,6 @@
# typed: false
# typed: true
# frozen_string_literal: true
require "searchable"
require "description_cache_store"
module Homebrew
@ -98,11 +97,7 @@ module Homebrew
end
aliases = Formula.alias_full_names
results = (Formula.full_names + aliases)
.extend(Searchable)
.search(string_or_regex)
.sort
results = search(Formula.full_names + aliases, string_or_regex).sort
results |= Formula.fuzzy_search(string_or_regex).map { |n| Formulary.factory(n).full_name }
results.map do |name|
@ -141,9 +136,7 @@ module Homebrew
cask_tokens += Homebrew::API::Cask.all_casks.keys
end
results = cask_tokens.extend(Searchable)
.search(string_or_regex)
results = search(cask_tokens, string_or_regex)
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
.correct(string_or_regex)
@ -176,5 +169,35 @@ module Homebrew
[all_formulae, all_casks]
end
def search(array, string_or_regex, &block)
case string_or_regex
when Regexp
search_regex(array, string_or_regex, &block)
else
search_string(array, string_or_regex.to_str, &block)
end
end
def simplify_string(string)
string.downcase.gsub(/[^a-z\d]/i, "")
end
def search_regex(array, regex)
array.select do |*args|
args = yield(*args) if block_given?
args = Array(args).flatten.compact
args.any? { |arg| arg.match?(regex) }
end
end
def search_string(array, string)
simplified_string = simplify_string(string)
array.select do |*args|
args = yield(*args) if block_given?
args = Array(args).flatten.compact
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
end
end
end
end

View File

@ -0,0 +1,5 @@
# typed: strict
module Homebrew::Search
include Kernel
end

View File

@ -1,39 +0,0 @@
# typed: false
# frozen_string_literal: true
# Helper module for making a class searchable with both regular expressions and strings.
#
# @api private
module Searchable
def search(string_or_regex, &block)
case string_or_regex
when Regexp
search_regex(string_or_regex, &block)
else
search_string(string_or_regex.to_str, &block)
end
end
private
def simplify_string(string)
string.downcase.gsub(/[^a-z\d]/i, "")
end
def search_regex(regex)
select do |*args|
args = yield(*args) if block_given?
args = Array(args).flatten.compact
args.any? { |arg| arg.match?(regex) }
end
end
def search_string(string)
simplified_string = simplify_string(string)
select do |*args|
args = yield(*args) if block_given?
args = Array(args).flatten.compact
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
end
end
end

View File

@ -59,4 +59,45 @@ describe Homebrew::Search do
expect { described_class.query_regexp("/+/") }.to raise_error(/not a valid regex/)
end
end
describe "#search" do
let(:collection) { ["with-dashes"] }
context "when given a block" do
let(:collection) { [["with-dashes", "withdashes"]] }
it "searches by the selected argument" do
expect(described_class.search(collection, /withdashes/) { |_, short_name| short_name }).not_to be_empty
expect(described_class.search(collection, /withdashes/) { |long_name, _| long_name }).to be_empty
end
end
context "when given a regex" do
it "does not simplify strings" do
expect(described_class.search(collection, /with-dashes/)).to eq ["with-dashes"]
end
end
context "when given a string" do
it "simplifies both the query and searched strings" do
expect(described_class.search(collection, "with dashes")).to eq ["with-dashes"]
end
end
context "when searching a Hash" do
let(:collection) { { "foo" => "bar" } }
it "returns a Hash" do
expect(described_class.search(collection, "foo")).to eq "foo" => "bar"
end
context "with a nil value" do
let(:collection) { { "foo" => nil } }
it "does not raise an error" do
expect(described_class.search(collection, "foo")).to eq "foo" => nil
end
end
end
end
end

View File

@ -1,49 +0,0 @@
# typed: false
# frozen_string_literal: true
require "searchable"
describe Searchable do
subject(:searchable_collection) { collection.extend(described_class) }
let(:collection) { ["with-dashes"] }
describe "#search" do
context "when given a block" do
let(:collection) { [["with-dashes", "withdashes"]] }
it "searches by the selected argument" do
expect(searchable_collection.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
expect(searchable_collection.search(/withdashes/) { |long_name, _| long_name }).to be_empty
end
end
context "when given a regex" do
it "does not simplify strings" do
expect(searchable_collection.search(/with-dashes/)).to eq ["with-dashes"]
end
end
context "when given a string" do
it "simplifies both the query and searched strings" do
expect(searchable_collection.search("with dashes")).to eq ["with-dashes"]
end
end
context "when searching a Hash" do
let(:collection) { { "foo" => "bar" } }
it "returns a Hash" do
expect(searchable_collection.search("foo")).to eq "foo" => "bar"
end
context "with a nil value" do
let(:collection) { { "foo" => nil } }
it "does not raise an error" do
expect(searchable_collection.search("foo")).to eq "foo" => nil
end
end
end
end
end