brew style

This commit is contained in:
Douglas Eichelberger 2023-03-07 10:14:57 -08:00
parent 544960b4fc
commit 6ffda80a8a
2 changed files with 29 additions and 24 deletions

View File

@ -61,29 +61,31 @@ describe Utils do
describe ".underscore" do describe ".underscore" do
# commented out entries require acronyms inflections # commented out entries require acronyms inflections
let (:words) { let(:words) {
[["API", "api"], [
["APIController", "api_controller"], ["API", "api"],
["Nokogiri::HTML", "nokogiri/html"], ["APIController", "api_controller"],
# ["HTTPAPI", "http_api"], ["Nokogiri::HTML", "nokogiri/html"],
["HTTP::Get", "http/get"], # ["HTTPAPI", "http_api"],
["SSLError", "ssl_error"], ["HTTP::Get", "http/get"],
# ["RESTful", "restful"], ["SSLError", "ssl_error"],
# ["RESTfulController", "restful_controller"], # ["RESTful", "restful"],
# ["Nested::RESTful", "nested/restful"], # ["RESTfulController", "restful_controller"],
# ["IHeartW3C", "i_heart_w3c"], # ["Nested::RESTful", "nested/restful"],
# ["PhDRequired", "phd_required"], # ["IHeartW3C", "i_heart_w3c"],
# ["IRoRU", "i_ror_u"], # ["PhDRequired", "phd_required"],
# ["RESTfulHTTPAPI", "restful_http_api"], # ["IRoRU", "i_ror_u"],
# ["HTTP::RESTful", "http/restful"], # ["RESTfulHTTPAPI", "restful_http_api"],
# ["HTTP::RESTfulAPI", "http/restful_api"], # ["HTTP::RESTful", "http/restful"],
# ["APIRESTful", "api_restful"], # ["HTTP::RESTfulAPI", "http/restful_api"],
["Capistrano", "capistrano"], # ["APIRESTful", "api_restful"],
["CapiController", "capi_controller"], ["Capistrano", "capistrano"],
["HttpsApis", "https_apis"], ["CapiController", "capi_controller"],
["Html5", "html5"], ["HttpsApis", "https_apis"],
["Restfully", "restfully"], ["Html5", "html5"],
["RoRails", "ro_rails"]] ["Restfully", "restfully"],
["RoRails", "ro_rails"],
]
} }
it "converts strings to underscore case" do it "converts strings to underscore case" do

View File

@ -144,8 +144,11 @@ module Utils
sig { params(camel_cased_word: T.any(String, Symbol)).returns(String) } sig { params(camel_cased_word: T.any(String, Symbol)).returns(String) }
def self.underscore(camel_cased_word) def self.underscore(camel_cased_word)
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word) return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
word = camel_cased_word.to_s.gsub("::", "/") word = camel_cased_word.to_s.gsub("::", "/")
word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" } word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) do
(::Regexp.last_match(1) || ::Regexp.last_match(2)) << "_"
end
word.tr!("-", "_") word.tr!("-", "_")
word.downcase! word.downcase!
word word