File: modifier.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (96 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

module TTY
  class Prompt
    class Question
      # A class representing String modifications.
      class Modifier
        attr_reader :modifiers

        # Initialize a Modifier
        #
        # @api public
        def initialize(modifiers)
          @modifiers = modifiers
        end

        # Change supplied value according to the given string transformation.
        # Valid settings are:
        #
        # @param [String] value
        #   the string to be modified
        #
        # @return [String]
        #
        # @api private
        def apply_to(value)
          modifiers.reduce(value) do |result, mod|
            result = Modifier.letter_case(mod, result)
            Modifier.whitespace(mod, result)
          end
        end

        # Changes letter casing in a string according to valid modifications.
        # For invalid modification option the string is preserved.
        #
        # @param [Symbol] mod
        #  the modification to change the string
        #
        # @option mod [Symbol] :up        change to upper case
        # @option mod [Symbol] :upcase    change to upper case
        # @option mod [Symbol] :uppercase change to upper case
        # @option mod [Symbol] :down      change to lower case
        # @option mod [Symbol] :downcase  change to lower case
        # @option mod [Symbol] :capitalize change all words to start
        #                                  with uppercase case letter
        #
        # @return [String]
        #
        # @api public
        def self.letter_case(mod, value)
          return value unless value.is_a?(String)

          case mod
          when :up, :upcase, :uppercase
            value.upcase
          when :down, :downcase, :lowercase
            value.downcase
          when :capitalize
            value.capitalize
          else
            value
          end
        end

        # Changes whitespace in a string according to valid modifications.
        #
        # @param [Symbol] mod
        #   the modification to change the string
        #
        # @option mod [String] :trim, :strip
        #   remove whitespace for the start and end
        # @option mod [String] :chomp     remove record separator from the end
        # @option mod [String] :collapse  remove any duplicate whitespace
        # @option mod [String] :remove    remove all whitespace
        #
        # @api public
        def self.whitespace(mod, value)
          return value unless value.is_a?(String)

          case mod
          when :trim, :strip
            value.strip
          when :chomp
            value.chomp
          when :collapse
            value.gsub(/\s+/, " ")
          when :remove
            value.gsub(/\s+/, "")
          else
            value
          end
        end
      end # Modifier
    end # Question
  end # Prompt
end # TTY