Class: StringDirection::DominantStrategy

Inherits:
CharactersStrategy show all
Defined in:
lib/string-direction/strategies/dominant_strategy.rb

Overview

Strategy to decide direction between ltr or rtl in function of which is the main type

Constant Summary

Constants inherited from CharactersStrategy

CharactersStrategy::IGNORED_CHARS

Instance Method Summary collapse

Instance Method Details

#run(string) ⇒ String?

Get the number of ltr and rtl characters in the supplied string and infer direction from the most common type. For this strategy the direction can be ltr or rtl, but never bidi. In case of draw it returns nil.

params [String] The string to inspect

Returns:

  • (String, nil)


10
11
12
13
14
15
16
17
18
# File 'lib/string-direction/strategies/dominant_strategy.rb', line 10

def run(string)
  string = string.to_s
  ltr_count = chars_count(string, ltr_regex)
  rtl_count = chars_count(string, rtl_regex)
  diff = ltr_count - rtl_count
  return ltr if diff > 0
  return rtl if diff < 0
  nil
end