Replace String#underscore with util
This commit is contained in:
parent
fdd7b04ccf
commit
544960b4fc
@ -111,7 +111,7 @@ module Homebrew
|
||||
constant = Strategy.const_get(const_symbol)
|
||||
next unless constant.is_a?(Class)
|
||||
|
||||
key = const_symbol.to_s.underscore.to_sym
|
||||
key = Utils.underscore(const_symbol).to_sym
|
||||
@strategies[key] = constant
|
||||
end
|
||||
@strategies
|
||||
|
||||
@ -58,4 +58,39 @@ describe Utils do
|
||||
expect(described_class.pluralize("foo", 2, singular: "o", plural: "es")).to eq("fooes")
|
||||
end
|
||||
end
|
||||
|
||||
describe ".underscore" do
|
||||
# commented out entries require acronyms inflections
|
||||
let (:words) {
|
||||
[["API", "api"],
|
||||
["APIController", "api_controller"],
|
||||
["Nokogiri::HTML", "nokogiri/html"],
|
||||
# ["HTTPAPI", "http_api"],
|
||||
["HTTP::Get", "http/get"],
|
||||
["SSLError", "ssl_error"],
|
||||
# ["RESTful", "restful"],
|
||||
# ["RESTfulController", "restful_controller"],
|
||||
# ["Nested::RESTful", "nested/restful"],
|
||||
# ["IHeartW3C", "i_heart_w3c"],
|
||||
# ["PhDRequired", "phd_required"],
|
||||
# ["IRoRU", "i_ror_u"],
|
||||
# ["RESTfulHTTPAPI", "restful_http_api"],
|
||||
# ["HTTP::RESTful", "http/restful"],
|
||||
# ["HTTP::RESTfulAPI", "http/restful_api"],
|
||||
# ["APIRESTful", "api_restful"],
|
||||
["Capistrano", "capistrano"],
|
||||
["CapiController", "capi_controller"],
|
||||
["HttpsApis", "https_apis"],
|
||||
["Html5", "html5"],
|
||||
["Restfully", "restfully"],
|
||||
["RoRails", "ro_rails"]]
|
||||
}
|
||||
|
||||
it "converts strings to underscore case" do
|
||||
words.each do |camel, under|
|
||||
expect(described_class.underscore(camel)).to eq(under)
|
||||
expect(described_class.underscore(under)).to eq(under)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -131,4 +131,23 @@ module Utils
|
||||
suffix = (count == 1) ? singular : plural
|
||||
"#{stem}#{suffix}"
|
||||
end
|
||||
|
||||
# Makes an underscored, lowercase form from the expression in the string.
|
||||
#
|
||||
# Changes '::' to '/' to convert namespaces to paths.
|
||||
#
|
||||
# underscore('ActiveModel') # => "active_model"
|
||||
# underscore('ActiveModel::Errors') # => "active_model/errors"
|
||||
#
|
||||
# @see https://github.com/rails/rails/blob/v6.1.7.2/activesupport/lib/active_support/inflector/methods.rb#L81-L100
|
||||
# `ActiveSupport::Inflector.underscore`
|
||||
sig { params(camel_cased_word: T.any(String, Symbol)).returns(String) }
|
||||
def self.underscore(camel_cased_word)
|
||||
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
|
||||
word = camel_cased_word.to_s.gsub("::", "/")
|
||||
word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" }
|
||||
word.tr!("-", "_")
|
||||
word.downcase!
|
||||
word
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user