Skip to content

Commit

Permalink
Don't instantiate or use Regexp when string.start_with? will do (#3177
Browse files Browse the repository at this point in the history
)

Co-authored-by: Cameron Dutro <camertron@gmail.com>
  • Loading branch information
mathias and camertron authored Nov 5, 2024
1 parent 3625de7 commit 308a56b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-rocks-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Switch Ruby match from Regexps to String's starts_with method
26 changes: 13 additions & 13 deletions lib/primer/classify/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class Utilities

# Replacements for some classnames that end up being a different argument key
REPLACEMENT_KEYS = {
Regexp.new("^f") => "font_size",
Regexp.new("^anim") => "animation",
Regexp.new("^v-align") => "vertical_align",
Regexp.new("^d") => "display",
Regexp.new("^wb") => "word_break",
Regexp.new("^v") => "visibility",
Regexp.new("^width") => "w",
Regexp.new("^height") => "h",
Regexp.new("^color-bg") => "bg",
Regexp.new("^color-border") => "border_color",
Regexp.new("^color-fg") => "color",
Regexp.new("^rounded") => "border_radius"
"f" => "font_size",
"anim" => "animation",
"v-align" => "vertical_align",
"d" => "display",
"wb" => "word_break",
"v" => "visibility",
"width" => "w",
"height" => "h",
"color-bg" => "bg",
"color-border" => "border_color",
"color-fg" => "color",
"rounded" => "border_radius"
}.freeze

SUPPORTED_KEY_CACHE = Hash.new { |h, k| h[k] = !UTILITIES[k].nil? }
Expand Down Expand Up @@ -190,7 +190,7 @@ def find_selector(selector)

def infer_selector_key(selector)
REPLACEMENT_KEYS.each do |k, v|
return v.to_sym if selector.match?(k)
return v.to_sym if selector.start_with?(k)
end
selector.split("-").first.to_sym
end
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/utilities.rake
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ namespace :utilities do

# Look for a replacement key
Primer::Classify::Utilities::REPLACEMENT_KEYS.each do |k, v|
next unless classname.match?(Regexp.new(k))
next unless classname.start_with?(k)

key = v
classname.sub!(Regexp.new("#{k}-"), "")
classname.sub!(Regexp.new("\A#{k}-"), "")
end

# If we didn't find a replacement, grab the first text before hyphen
Expand Down
2 changes: 1 addition & 1 deletion test/lib/css_coverage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setup
# Cleanup the data to make sure it's only one selector per item
@css_data = @css_data
.flat_map { |c| c.gsub(/(\w)\./, '\1 .').split(/[\s:\[+>]+/) }
.select { |c| c.starts_with?(".") }
.select { |c| c.start_with?(".") }
.uniq
end

Expand Down

0 comments on commit 308a56b

Please sign in to comment.