Skip to content

Commit

Permalink
Support diacrities for underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
cllns committed Sep 9, 2024
1 parent c357c57 commit 3efbaa7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/dry/inflector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ def underscore(input)
m2 = Regexp.last_match(2)
"#{m1 ? "_" : ""}#{m2.downcase}"
end
input.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
input.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
input.gsub!(/([[:upper:][:digit:]]+)([[:upper:]][[:lower:]])/, '\1_\2')
input.gsub!(/([[:lower:][:digit:]])([[:upper:]])/, '\1_\2')
input.tr!("-", "_")
input.downcase!
input
Expand Down
40 changes: 40 additions & 0 deletions spec/unit/dry/inflector/underscore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
expect(subject.underscore(i("CLIRunner"))).to eq("cli_runner")
end

it "underscores aCli as a_cli" do
expect(subject.underscore(i("aCLI"))).to eq("a_cli")
end

it "accepts symbols" do
expect(subject.underscore(:DataMapper)).to eq("data_mapper")
end
Expand All @@ -44,5 +48,41 @@
expect(subject.underscore(i("OpenSSL::HMAC"))).to eql("openssl/hmac")
expect(subject.underscore(i("OpenSSL::Digest"))).to eql("openssl/digest")
end

it "handles diacritics" do
expect(subject.underscore(i("éclairFest"))).to eql("éclair_fest")
expect(subject.underscore(i("éuaTrip"))).to eql("éua_trip")
expect(subject.underscore(i("festival-des-éclairs"))).to eql("festival_des_éclairs")
expect(subject.underscore(i("ÉBellisimo"))).to eql("é_bellisimo")
expect(subject.underscore(i("éBellisimo"))).to eql("é_bellisimo")
end

it "handles number followed by uppercase letter" do
expect(subject.underscore(i("9Lives"))).to eql("9_lives")
end

pending "handles number followed by lowercase letter" do
pending "this should be `7_ate_9`"
expect(subject.underscore(i("7Ate9"))).to eql("7_ate_9")
end

it "handles number followed by lowercase letter" do
pending "this should be 9_lives"
expect(subject.underscore(i("9lives"))).to eql("9_lives")
end

it "handle leading diacritic" do
expect(subject.underscore(i("éBellisimo"))).to eql("é_bellisimo")
end

it "handles leading number " do
pending "this should be 1_is_one"
expect(subject.underscore(i("1isOne"))).to eql("1_is_one")
end

it "handles leading number with diacritic" do
pending "this should be 1_é_bellisimo"
expect(subject.underscore(i("1éBellisimo"))).to eql("1_é_bellisimo")
end
end
end

0 comments on commit 3efbaa7

Please sign in to comment.