• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jnbt / java-properties / 4254520611

pending completion
4254520611

push

github

GitHub
Merge pull request #21 from jnbt/update-ruby

334 of 339 relevant lines covered (98.53%)

134.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/java-properties/parsing/normalizer.rb
1
module JavaProperties
7✔
2
  module Parsing
7✔
3
    # Module to normalize the content of a properties file
4
    # 
5
    # @example Normalizes:
6
    #   # Comment 1
7
    #   ! Comment 2
8
    #   item0
9
    #   item1 = item1 
10
    #   item2 : item2 
11
    #   item3=line 1 \
12
    #         line 2
13
    #
14
    # @example Into:
15
    #
16
    #   item0
17
    #   item1=item1 
18
    #   item2=item2 
19
    #   item3=line 1 line 2
20
    #
21
    module Normalizer
7✔
22

23
      # Describes a single normalization rule by replacing content
24
      class Rule
7✔
25
        # Initializes a new rules base on a matching regexp
26
        # and a replacement as substitution
27
        # @param matcher [Regexp]
28
        # @param replacement [String]
29
        def initialize(matcher, replacement = '')
7✔
30
          @matcher     = matcher
35✔
31
          @replacement = replacement
35✔
32
        end
33

34
        # Apply the substitution to the text in place
35
        # @param text [string]
36
        # @return [String]
37
        def apply!(text)
7✔
38
          text.gsub!(@matcher, @replacement)
175✔
39
        end
40
      end
41

42
      # Collection of ordered rules
43
      RULES = []
7✔
44

45
      # Removes comments
46
      RULES << Rule.new(/^\s*[!\#].*$/)
7✔
47
      
48
      # Removes leading whitepsace
49
      RULES << Rule.new(/^\s+/)
7✔
50
      
51
      # Removes tailing whitepsace
52
      RULES << Rule.new(/\s+$/)
7✔
53

54
      # Strings ending with \ are concatenated
55
      RULES << Rule.new(/\\\s*$[\n\r]+/)
7✔
56

57
      # Remove whitespace around delimiters and replace with =
58
      RULES << Rule.new(/^((?:(?:\\[=: \t])|[^=: \t])+)[ \t]*[=: \t][ \t]*/, '\1=')
7✔
59

60
      RULES.freeze
7✔
61

62
      # Normalizes the content of a properties file content by applying the RULES
63
      # @param text [String]
64
      # @return [String] 
65
      def self.normalize!(text)
7✔
66
        RULES.each do |rule|
35✔
67
          rule.apply!(text)
175✔
68
        end
69
        text
35✔
70
      end
71

72
    end
73
  end
74
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc