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,8 +61,9 @@ describe Utils do
describe ".underscore" do
# commented out entries require acronyms inflections
let (:words) {
[["API", "api"],
let(:words) {
[
["API", "api"],
["APIController", "api_controller"],
["Nokogiri::HTML", "nokogiri/html"],
# ["HTTPAPI", "http_api"],
@ -83,7 +84,8 @@ describe Utils do
["HttpsApis", "https_apis"],
["Html5", "html5"],
["Restfully", "restfully"],
["RoRails", "ro_rails"]]
["RoRails", "ro_rails"],
]
}
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) }
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.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.downcase!
word