Skip to content

Commit 308a56b

Browse files
mathiascamertron
andauthoredNov 5, 2024··
Don't instantiate or use Regexp when string.start_with? will do (#3177)
Co-authored-by: Cameron Dutro <camertron@gmail.com>
1 parent 3625de7 commit 308a56b

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed
 

‎.changeset/chilly-rocks-compare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': patch
3+
---
4+
5+
Switch Ruby match from Regexps to String's starts_with method

‎lib/primer/classify/utilities.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ class Utilities
1818

1919
# Replacements for some classnames that end up being a different argument key
2020
REPLACEMENT_KEYS = {
21-
Regexp.new("^f") => "font_size",
22-
Regexp.new("^anim") => "animation",
23-
Regexp.new("^v-align") => "vertical_align",
24-
Regexp.new("^d") => "display",
25-
Regexp.new("^wb") => "word_break",
26-
Regexp.new("^v") => "visibility",
27-
Regexp.new("^width") => "w",
28-
Regexp.new("^height") => "h",
29-
Regexp.new("^color-bg") => "bg",
30-
Regexp.new("^color-border") => "border_color",
31-
Regexp.new("^color-fg") => "color",
32-
Regexp.new("^rounded") => "border_radius"
21+
"f" => "font_size",
22+
"anim" => "animation",
23+
"v-align" => "vertical_align",
24+
"d" => "display",
25+
"wb" => "word_break",
26+
"v" => "visibility",
27+
"width" => "w",
28+
"height" => "h",
29+
"color-bg" => "bg",
30+
"color-border" => "border_color",
31+
"color-fg" => "color",
32+
"rounded" => "border_radius"
3333
}.freeze
3434

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

191191
def infer_selector_key(selector)
192192
REPLACEMENT_KEYS.each do |k, v|
193-
return v.to_sym if selector.match?(k)
193+
return v.to_sym if selector.start_with?(k)
194194
end
195195
selector.split("-").first.to_sym
196196
end

‎lib/tasks/utilities.rake

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ namespace :utilities do
5252

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

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

6161
# If we didn't find a replacement, grab the first text before hyphen

‎test/lib/css_coverage_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def setup
2929
# Cleanup the data to make sure it's only one selector per item
3030
@css_data = @css_data
3131
.flat_map { |c| c.gsub(/(\w)\./, '\1 .').split(/[\s:\[+>]+/) }
32-
.select { |c| c.starts_with?(".") }
32+
.select { |c| c.start_with?(".") }
3333
.uniq
3434
end
3535

0 commit comments

Comments
 (0)
Please sign in to comment.