Merge pull request #14916 from dduugg/refactor-searchable
Refactor searchable
This commit is contained in:
commit
bd309509da
@ -5,7 +5,6 @@ require "cask/cask_loader"
|
|||||||
require "cask/config"
|
require "cask/config"
|
||||||
require "cask/dsl"
|
require "cask/dsl"
|
||||||
require "cask/metadata"
|
require "cask/metadata"
|
||||||
require "searchable"
|
|
||||||
require "utils/bottles"
|
require "utils/bottles"
|
||||||
|
|
||||||
module Cask
|
module Cask
|
||||||
@ -16,7 +15,6 @@ module Cask
|
|||||||
extend T::Sig
|
extend T::Sig
|
||||||
|
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
extend Searchable
|
|
||||||
extend Predicable
|
extend Predicable
|
||||||
include Metadata
|
include Metadata
|
||||||
|
|
||||||
|
|||||||
@ -3,15 +3,12 @@
|
|||||||
|
|
||||||
require "set"
|
require "set"
|
||||||
require "cache_store"
|
require "cache_store"
|
||||||
require "searchable"
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used
|
# {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used
|
||||||
# by the `brew desc` and `brew search` commands.
|
# by the `brew desc` and `brew search` commands.
|
||||||
#
|
#
|
||||||
class DescriptionCacheStore < CacheStore
|
class DescriptionCacheStore < CacheStore
|
||||||
include Searchable
|
|
||||||
|
|
||||||
# Inserts a formula description into the cache if it does not exist or
|
# Inserts a formula description into the cache if it does not exist or
|
||||||
# updates the formula description if it does exist.
|
# updates the formula description if it does exist.
|
||||||
#
|
#
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
require "formula"
|
require "formula"
|
||||||
require "formula_versions"
|
require "formula_versions"
|
||||||
require "searchable"
|
require "search"
|
||||||
|
|
||||||
# Helper class for printing and searching descriptions.
|
# Helper class for printing and searching descriptions.
|
||||||
#
|
#
|
||||||
@ -15,11 +15,11 @@ class Descriptions
|
|||||||
|
|
||||||
results = case field
|
results = case field
|
||||||
when :name
|
when :name
|
||||||
cache_store.search(string_or_regex) { |name, _| name }
|
Homebrew::Search.search(cache_store, string_or_regex) { |name, _| name }
|
||||||
when :desc
|
when :desc
|
||||||
cache_store.search(string_or_regex) { |_, desc| desc }
|
Homebrew::Search.search(cache_store, string_or_regex) { |_, desc| desc }
|
||||||
when :either
|
when :either
|
||||||
cache_store.search(string_or_regex)
|
Homebrew::Search.search(cache_store, string_or_regex)
|
||||||
end
|
end
|
||||||
|
|
||||||
new(results)
|
new(results)
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
# typed: false
|
# typed: true
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "searchable"
|
|
||||||
require "description_cache_store"
|
require "description_cache_store"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
@ -98,11 +97,7 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
aliases = Formula.alias_full_names
|
aliases = Formula.alias_full_names
|
||||||
results = (Formula.full_names + aliases)
|
results = search(Formula.full_names + aliases, string_or_regex).sort
|
||||||
.extend(Searchable)
|
|
||||||
.search(string_or_regex)
|
|
||||||
.sort
|
|
||||||
|
|
||||||
results |= Formula.fuzzy_search(string_or_regex).map { |n| Formulary.factory(n).full_name }
|
results |= Formula.fuzzy_search(string_or_regex).map { |n| Formulary.factory(n).full_name }
|
||||||
|
|
||||||
results.map do |name|
|
results.map do |name|
|
||||||
@ -141,9 +136,7 @@ module Homebrew
|
|||||||
cask_tokens += Homebrew::API::Cask.all_casks.keys
|
cask_tokens += Homebrew::API::Cask.all_casks.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
results = cask_tokens.extend(Searchable)
|
results = search(cask_tokens, string_or_regex)
|
||||||
.search(string_or_regex)
|
|
||||||
|
|
||||||
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
|
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
|
||||||
.correct(string_or_regex)
|
.correct(string_or_regex)
|
||||||
|
|
||||||
@ -176,5 +169,35 @@ module Homebrew
|
|||||||
|
|
||||||
[all_formulae, all_casks]
|
[all_formulae, all_casks]
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
5
Library/Homebrew/search.rbi
Normal file
5
Library/Homebrew/search.rbi
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# typed: strict
|
||||||
|
|
||||||
|
module Homebrew::Search
|
||||||
|
include Kernel
|
||||||
|
end
|
||||||
@ -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
|
|
||||||
@ -59,4 +59,45 @@ describe Homebrew::Search do
|
|||||||
expect { described_class.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
expect { described_class.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@ -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
|
|
||||||
Loading…
x
Reference in New Issue
Block a user