Class: Cogger::Formatters::Sanitizers::Escape

Inherits:
Object
  • Object
show all
Defined in:
lib/cogger/formatters/sanitizers/escape.rb

Overview

Sanitizes value as fully quoted string for emojis, spaces, and control characters.

Constant Summary collapse

PATTERN =
/
  \A              # Search string start.
  .*              # Match zero or more characters.
  (               # Conditional start.
  (?!\p{Number})  # Look ahead and ignore unicode numbers.
  \p{Emoji}       # Match unicode emoji only.
  |               # Or.
  [[:space:]]     # Match spaces, tabs, and new lines.
  |               # Or.
  [[:cntrl:]]     # Match control characters.
  )               # Conditional end.
  .*              # Match zero or more characters.
  \z              # Search string end.
/xu

Instance Method Summary collapse

Constructor Details

#initialize(pattern: PATTERN) ⇒ Escape

Returns a new instance of Escape.



23
24
25
# File 'lib/cogger/formatters/sanitizers/escape.rb', line 23

def initialize pattern: PATTERN
  @pattern = pattern
end

Instance Method Details

#call(value) ⇒ Object



27
28
29
30
31
32
# File 'lib/cogger/formatters/sanitizers/escape.rb', line 27

def call value
  return dump value unless value.is_a? Array

  value.reduce(+"") { |text, item| text << dump(item) << ", " }
       .then { |text| %([#{text.delete_suffix ", "}]).dump }
end