Class: StringDirection::CharactersStrategy

Inherits:
Strategy
  • Object
show all
Defined in:
lib/string-direction/strategies/characters_strategy.rb

Overview

Strategy to detect direction from the scripts to which string characters belong

Constant Summary collapse

CHAR_IGNORE_REGEX =

Ignored characters: unicode marks, punctuations, symbols, separator and other general categories

/[\p{M}\p{P}\p{S}\p{Z}\p{C}]/.freeze

Instance Method Summary collapse

Instance Method Details

#run(string) ⇒ String?

Inspect to wich scripts characters belongs and infer from them the string direction. right-to-left scripts are those in StringDirection::Configuration#rtl_scripts

params [String] The string to inspect

Returns:

  • (String, nil)


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

def run(string)
  string = string.to_s
  if ltr_characters?(string) && rtl_characters?(string)
    bidi
  elsif ltr_characters?(string)
    ltr
  elsif rtl_characters?(string)
    rtl
  end
end