File: util.rb

package info (click to toggle)
ruby-flipper 0.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,288 kB
  • sloc: ruby: 16,377; sh: 61; javascript: 24; makefile: 14
file content (56 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (2)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Flipper
  module UI
    module Util
      # Private: 0x3000: fullwidth whitespace
      NON_WHITESPACE_REGEXP = /[^\s#{[0x3000].pack("U")}]/

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

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

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

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

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

      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
    end
  end
end