Skip to content

Commit

Permalink
Merge pull request #41303 from Shopify/underscore-allocations
Browse files Browse the repository at this point in the history
Optimize underscore inflector
  • Loading branch information
kaspth committed Feb 2, 2021
2 parents f43e701 + 58147cd commit a2a5385
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def camelize(term, uppercase_first_letter = true)
string = string.sub(inflections.acronyms_camelize_regex) { |match| match.downcase! || match }
end
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do
substituted = inflections.acronyms[$2] || $2.capitalize! || $2
word = $2
substituted = inflections.acronyms[word] || word.capitalize! || word
$1 ? "::#{substituted}" : substituted
end
string
Expand All @@ -95,8 +96,13 @@ def underscore(camel_cased_word)
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
word = camel_cased_word.to_s.gsub("::", "/")
word.gsub!(inflections.acronyms_underscore_regex) { "#{$1 && '_' }#{$2.downcase}" }
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.gsub!(/([A-Z\d]+)([A-Z][a-z])|([a-z\d])([A-Z])/) do
if first_match = $1
first_match << "_" << $2
else
$3 << "_" << $4
end
end
word.tr!("-", "_")
word.downcase!
word
Expand Down

0 comments on commit a2a5385

Please sign in to comment.