File: marks_strategy.rb

package info (click to toggle)
ruby-string-direction 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: ruby: 657; makefile: 7; sh: 3
file content (35 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module StringDirection
  # Strategy to detect direction looking for the presence of unicode marks
  class MarksStrategy < Strategy
    # left-to-right unicode mark
    LTR_MARK = "\u200e".freeze

    # right-to-right unicode mark
    RTL_MARK = "\u200f".freeze

    # Look for the presence of unicode marks in given string and infers from them its direction
    #
    # params [String] The string to inspect
    # @return [String, nil]
    def run(string)
      string = string.to_s
      if ltr_mark?(string) && rtl_mark?(string)
        bidi
      elsif ltr_mark?(string)
        ltr
      elsif rtl_mark?(string)
        rtl
      end
    end

    private

    def ltr_mark?(string)
      string.include?(LTR_MARK)
    end

    def rtl_mark?(string)
      string.include?(RTL_MARK)
    end
  end
end