Module: Flipper::UI::Util

Defined in:
lib/flipper/ui/util.rb

Constant Summary collapse

NON_WHITESPACE_REGEXP =

Private: 0x3000: fullwidth whitespace

/[^\s#{[0x3000].pack("U")}]/

Class Method Summary collapse

Class Method Details

.blank?(str) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/flipper/ui/util.rb', line 7

def self.blank?(str)
  str.to_s !~ NON_WHITESPACE_REGEXP
end

.pluralize(count, singular, plural) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/flipper/ui/util.rb', line 27

def self.pluralize(count, singular, plural)
  if count == 1
    "#{count} #{singular}"
  else
    "#{count} #{plural}"
  end
end

.present?(str) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/flipper/ui/util.rb', line 11

def self.present?(str)
  !blank?(str)
end

.titleize(str) ⇒ Object



15
16
17
# File 'lib/flipper/ui/util.rb', line 15

def self.titleize(str)
  str.to_s.split('_').map(&:capitalize).join(' ')
end

.to_sentence(array, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flipper/ui/util.rb', line 35

def self.to_sentence(array, options = {})
  default_connectors = {
    words_connector: ", ",
    two_words_connector: " and ",
    last_word_connector: ", and "
  }
  options = default_connectors.merge!(options)

  case array.length
  when 0
    ""
  when 1
    "#{array[0]}"
  when 2
    "#{array[0]}#{options[:two_words_connector]}#{array[1]}"
  else
    "#{array[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{array[-1]}"
  end
end

.truncate(str, length: 30) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/flipper/ui/util.rb', line 19

def self.truncate(str, length: 30)
  if str.length > length
    str[0..length]
  else
    str
  end
end