Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support diacritics #51

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/dry/inflector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def humanize(input)
result = inflections.humans.apply_to(input)
result.delete_suffix!("_id")
result.tr!("_", " ")
match = /(\W)/.match(result)
match = /([^[:alnum:]])/.match(result)
separator = match ? match[0] : DEFAULT_SEPARATOR
result.split(separator).map.with_index { |word, index|
inflections.acronyms.apply_to(word, capitalize: index.zero?)
Expand Down 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 Expand Up @@ -328,8 +328,10 @@ def to_s
# @api private
def internal_camelize(input, upper)
input = input.to_s.dup
input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match, capitalize: upper) }
input.gsub!(%r{(?:[_-]|(/))([a-z\d]*)}i) do
input.sub!(/^[[:lower:][:digit:]]*/) do |match|
inflections.acronyms.apply_to(match, capitalize: upper)
end
input.gsub!(%r{(?:[_-]|(/))([[:lower:][:digit:]]*)}i) do
m1 = Regexp.last_match(1)
m2 = Regexp.last_match(2)
"#{m1}#{inflections.acronyms.apply_to(m2)}"
Expand Down
9 changes: 8 additions & 1 deletion spec/unit/dry/inflector/camelize_lower_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
context "custom acronyms" do
subject do
Dry::Inflector.new do |inflect|
inflect.acronym("IP", "HTML", "XML", "BSD")
inflect.acronym("IP", "HTML", "XML", "BSD", "ÉUA", "4K")
end
end

Expand All @@ -38,6 +38,13 @@
expect(subject.camelize_lower(i("force_xml_controller"))).to eql("forceXMLController")
expect(subject.camelize_lower(i("free_bsd"))).to eql("freeBSD")
end

it "handles diacritics" do
expect(subject.camelize_lower(i("éclair_fest"))).to eql("éclairFest")
expect(subject.camelize_lower(i("éua-trip"))).to eql("ÉUATrip")
expect(subject.camelize_lower(i("festival_des_éclairs"))).to eql("festivalDesÉclairs")
expect(subject.camelize_lower(i("festival_des_4k"))).to eql("festivalDes4K")
end
end
end
end
10 changes: 9 additions & 1 deletion spec/unit/dry/inflector/camelize_upper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
context "custom acronyms" do
subject do
Dry::Inflector.new do |inflect|
inflect.acronym("IP", "HTML", "XML", "BSD")
inflect.acronym("IP", "HTML", "XML", "BSD", "ÉUA", "4K")
end
end

Expand All @@ -41,6 +41,14 @@
expect(subject.camelize_upper(i("html_tidy_generator"))).to eql("HTMLTidyGenerator")
expect(subject.camelize_upper(i("force_xml_controller"))).to eql("ForceXMLController")
expect(subject.camelize_upper(i("free_bsd"))).to eql("FreeBSD")
expect(subject.camelize_upper(i("4k-television"))).to eql("4KTelevision")
end

it "handles diacritics" do
expect(subject.camelize_upper(i("éclair_fest"))).to eql("ÉclairFest")
expect(subject.camelize_upper(i("éua-trip"))).to eql("ÉUATrip")
expect(subject.camelize_upper(i("festival_des_éclairs"))).to eql("FestivalDesÉclairs")
expect(subject.camelize_upper(i("festival_des_4k"))).to eql("FestivalDes4K")
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/dry/inflector/humanize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
expect(subject.humanize(i("openssl/hmac"))).to eql("OpenSSL/HMAC")
expect(subject.humanize(i("openssl/digest"))).to eql("OpenSSL/digest")
end

it "handles diacritics" do
expect(subject.humanize(i("éclair"))).to eql("Éclair")
end
end
end
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
Loading