Class: UnitMeasurements::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/unit_measurements/normalizer.rb

Overview

The UnitMeasurements::Normalizer class defines useful methods for handling input strings that may contain non-standard representations of numbers.

It maps special symbols of exponents and fractions to their standard counterparts. The class ensures consistent handling of input strings within the library.

See Also:

Author:

Since:

  • 1.0.0

Constant Summary collapse

EXPONENTS_SYMBOLS =

A mapping of superscript numbers, plus, and minus signs to their regular counterparts.

Author:

Since:

  • 1.0.0

{
  "" => "0",
  "¹" => "1",
  "²" => "2",
  "³" => "3",
  "" => "4",
  "" => "5",
  "" => "6",
  "" => "7",
  "" => "8",
  "" => "9",
  "" => "+",
  "" => "-",
}.freeze
FRACTIONS_SYMBOLS =

A mapping of special fraction symbols to their equivalent numeric fractions.

Author:

Since:

  • 1.0.0

{
  "¼"  => "1/4",
  "½"  => "1/2",
  "¾"  => "3/4",
  ""  => "1/3",
  ""  => "2/3",
  ""  => "1/5",
  ""  => "2/5",
  ""  => "3/5",
  ""  => "4/5",
  ""  => "1/6",
  ""  => "5/6",
  ""  => "1/7",
  ""  => "1/8",
  ""  => "3/8",
  ""  => "5/8",
  ""  => "7/8",
  ""  => "1/9",
  "" => "1/10",
  ""  => "0/3",
}.freeze
EXPONENT_REGEX =

Matches a combination of digits, optional exponent notation, and optional plus or minus sign.

Author:

Since:

  • 1.0.0

/
  (                                     # Start of the first capturing group
    [\d]+                               # One or more digits
    [Ee]?                               # Match an optional 'E' or 'e' for scientific notation
    [+-]?                               # Match an optional plus or minus sign
  )                                     # End of the first capturing group
  (                                     # Start of the second capturing group
    #{EXPONENTS_SYMBOLS.keys.join("|")} # Match any of the exponent symbols defined in EXPONENTS_SYMBOLS
  )                                     # End of the second capturing group
/x.freeze
FRACTION_REGEX =

Matches special fraction symbols.

Author:

Since:

  • 1.0.0

/
  (                                     # Start of the capturing group
    #{FRACTIONS_SYMBOLS.keys.join("|")} # Match any of the fraction symbols defined in FRACTIONS_SYMBOLS
  )                                     # End of the capturing group
/x.freeze
RATIO_REGEX =

Matches a ratio in the format of numerator:denominator.

Author:

Since:

  • 1.0.0

/
  (                                     # Start of the first capturing group
    [\d]+                               # One or more digits, representing the numerator
  )                                     # End of the first capturing group
  :                                     # Match a colon (:)
  (                                     # Start of the second capturing group
    [\d]+                               # One or more digits, representing the denominator
  )                                     # End of the second capturing group
/x.freeze

Class Method Summary collapse

Class Method Details

.normalize(string) ⇒ String

Normalizes a string containing special symbols of exponents and fractions, and ratio.

The normalize method in the UnitMeasurements::Normalizer module processes the input string to replace special symbols with their equivalent representations, such as converting superscript numbers to regular numbers, special fraction symbols to their numeric fractions, and ratios to fractional format, and remove leading and trailing whitespaces from the string.

Examples:

Normalizing special symbol of fraction:

UnitMeasurements::Normalizer.normalize("¾")
=> "3/4"

UnitMeasurements::Normalizer.normalize(" 1¾ ")
=> "1 3/4"

Normalizing ratio:

UnitMeasurements::Normalizer.normalize("1:2")
=> "1/2"

Normalizing special symbol of exponents:

UnitMeasurements::Normalizer.normalize("1e⁺²")
=> "1e+2"

UnitMeasurements::Normalizer.normalize("1e⁻²")
=> "1e-2"

Parameters:

  • string (String)

    The input string to be normalized.

Returns:

  • (String)

    The normalized version of the input string.

See Also:

Author:

Since:

  • 1.0.0



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/unit_measurements/normalizer.rb', line 138

def normalize(string)
  string.dup.tap do |str|
    if str =~ Regexp.new(EXPONENT_REGEX)
      EXPONENTS_SYMBOLS.each do |search, replace|
        str.gsub!(search) { "#{replace}" }
      end
    end

    str.gsub!(FRACTION_REGEX) { " #{FRACTIONS_SYMBOLS[$1]}" }

    str.gsub!(RATIO_REGEX)    { "#{$1.to_i}/#{$2.to_i}" }

    str.strip!
  end
end