Skip to content

Commit

Permalink
refactor: str_is_integer? method
Browse files Browse the repository at this point in the history
  • Loading branch information
benzend committed Oct 18, 2024
1 parent c91006d commit d9b4779
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/glare/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Glare
class Util
def self.str_is_integer?(str)
[ # In descending order of likeliness:
/^[-+]?[1-9]([0-9]*)?$/, # decimal
/^0[0-7]+$/, # octal
/^0x[0-9A-Fa-f]+$/, # hexadecimal
/^0b[01]+$/ # binary
].each do |match_pattern|
return true if str =~ match_pattern
end
false
end
end
end
11 changes: 10 additions & 1 deletion lib/glare/ux_metrics.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "glare/util"

module Glare
module UxMetrics
class Error < StandardError; end
Expand All @@ -21,7 +23,8 @@ def valid?
return false unless missing_attributes.empty?

return false unless choices.values.all? do |v|
return v.to_i.to_s == v || v.to_f.to_s == v if v.is_a?(String)
return Glare::Util.str_is_integer?(v) if v.is_a?(String)

true
end

Expand Down Expand Up @@ -598,3 +601,9 @@ def in_hotspot?
end
end
end

class String
def integer?

end
end
5 changes: 5 additions & 0 deletions sig/glare/util.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Glare
class Util
def self.str_is_integer?: (String) -> bool
end
end

0 comments on commit d9b4779

Please sign in to comment.