string_pattern 2.2.3 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/lib/string/pattern/add_to_ruby.rb +43 -13
- data/lib/string/pattern/generate.rb +10 -0
- data/lib/string_pattern.rb +8 -1
- metadata +14 -14
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 7b9ea7310d3fb561b37d8b0753360ddd5742188a2f71b68560a121d716d65083
         | 
| 4 | 
            +
              data.tar.gz: a0504e61ff66a749d7c0b0fcc36ad629474d793abd78bc0456bdd6a9ddeae7f7
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3ade8ead8f7434fb1893f62e44523245fb52776a24ffc9f9f548a16f790ec035f2411d223203886571decd7c7455b6f56cf41a7e62c491910762c210754e11f7
         | 
| 7 | 
            +
              data.tar.gz: bbb68322396f6017f6f6f1b6190b1e14cc0a3cea804efa66f6527f892be032fe5cebc321e39ea399e0af9f69ee6f5e4ef1c7a9241ac97541cf6d4aac3191c658
         | 
    
        data/README.md
    CHANGED
    
    | @@ -428,6 +428,17 @@ StringPattern.optimistic = true | |
| 428 428 | 
             
            #>SAAERfixedtext988
         | 
| 429 429 | 
             
            ```
         | 
| 430 430 |  | 
| 431 | 
            +
            #### block_list
         | 
| 432 | 
            +
             | 
| 433 | 
            +
            To specify which words will be avoided from the results
         | 
| 434 | 
            +
             | 
| 435 | 
            +
            ```ruby
         | 
| 436 | 
            +
            StringPattern.block_list = ['example', 'wrong', 'ugly']
         | 
| 437 | 
            +
            StringPattern.block_list_enabled = true
         | 
| 438 | 
            +
            "2-20:Tn".gen #>AAñ34Ef99éNOP
         | 
| 439 | 
            +
            ```
         | 
| 440 | 
            +
             | 
| 441 | 
            +
             | 
| 431 442 | 
             
            ## Contributing
         | 
| 432 443 |  | 
| 433 444 | 
             
            Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.
         | 
| @@ -90,11 +90,14 @@ class Regexp | |
| 90 90 | 
             
                pats = ""
         | 
| 91 91 | 
             
                patg = [] # for (aa|bb|cc) group
         | 
| 92 92 | 
             
                set = false
         | 
| 93 | 
            +
                set_negate = false
         | 
| 94 | 
            +
                options = []
         | 
| 93 95 | 
             
                capture = false
         | 
| 94 96 |  | 
| 95 97 | 
             
                range = ""
         | 
| 96 98 | 
             
                fixed_text = false
         | 
| 97 | 
            -
                 | 
| 99 | 
            +
                options = regexp.to_s.scan(/\A\(\?([mix]*)\-[mix]*:/).join.split('')
         | 
| 100 | 
            +
                last_char = (regexp.to_s.gsub(/\A\(\?[mix]*\-[mix]*:/, "").length) - 2
         | 
| 98 101 | 
             
                Regexp::Scanner.scan regexp do |type, token, text, ts, te|
         | 
| 99 102 | 
             
                  if type == :escape
         | 
| 100 103 | 
             
                    if token == :dot
         | 
| @@ -126,9 +129,9 @@ class Regexp | |
| 126 129 | 
             
                          pata[-1] += pats.chop
         | 
| 127 130 | 
             
                        else
         | 
| 128 131 | 
             
                          if pats.size == 2
         | 
| 129 | 
            -
                            pata << pats.chop | 
| 132 | 
            +
                            pata << pats.chop
         | 
| 130 133 | 
             
                          else
         | 
| 131 | 
            -
                            pata << "1:[#{pats}" | 
| 134 | 
            +
                            pata << "1:[#{pats}"
         | 
| 132 135 | 
             
                          end
         | 
| 133 136 | 
             
                          if last_char == te and type == :literal and token == :literal
         | 
| 134 137 | 
             
                            pata << text
         | 
| @@ -147,7 +150,6 @@ class Regexp | |
| 147 150 | 
             
                    pats = ""
         | 
| 148 151 | 
             
                  end
         | 
| 149 152 | 
             
                  fixed_text = false
         | 
| 150 | 
            -
             | 
| 151 153 | 
             
                  case token
         | 
| 152 154 | 
             
                  when :open
         | 
| 153 155 | 
             
                    set = true
         | 
| @@ -158,7 +160,13 @@ class Regexp | |
| 158 160 | 
             
                      if pats[-1] == "["
         | 
| 159 161 | 
             
                        pats.chop!
         | 
| 160 162 | 
             
                      else
         | 
| 161 | 
            -
                         | 
| 163 | 
            +
                        if set_negate
         | 
| 164 | 
            +
                          pats+="%]*"
         | 
| 165 | 
            +
                          set_negate = false
         | 
| 166 | 
            +
                        else
         | 
| 167 | 
            +
                          pats += "]"
         | 
| 168 | 
            +
                        end    
         | 
| 169 | 
            +
             | 
| 162 170 | 
             
                      end
         | 
| 163 171 | 
             
                    elsif type == :group
         | 
| 164 172 | 
             
                      capture = false
         | 
| @@ -169,6 +177,11 @@ class Regexp | |
| 169 177 | 
             
                        pats = ""
         | 
| 170 178 | 
             
                      end
         | 
| 171 179 | 
             
                    end
         | 
| 180 | 
            +
                  when :negate
         | 
| 181 | 
            +
                    if set and pats[-1] == '['
         | 
| 182 | 
            +
                      pats+="%"
         | 
| 183 | 
            +
                      set_negate = true
         | 
| 184 | 
            +
                    end
         | 
| 172 185 | 
             
                  when :capture
         | 
| 173 186 | 
             
                    capture = true if type == :group
         | 
| 174 187 | 
             
                  when :alternation
         | 
| @@ -182,6 +195,7 @@ class Regexp | |
| 182 195 | 
             
                      end
         | 
| 183 196 | 
             
                    end
         | 
| 184 197 | 
             
                  when :range
         | 
| 198 | 
            +
                    pats.chop! if options.include?('i')
         | 
| 185 199 | 
             
                    range = pats[-1]
         | 
| 186 200 | 
             
                    pats.chop!
         | 
| 187 201 | 
             
                  when :digit
         | 
| @@ -212,28 +226,44 @@ class Regexp | |
| 212 226 | 
             
                        pats = text[-1]
         | 
| 213 227 | 
             
                      else
         | 
| 214 228 | 
             
                        pats += text
         | 
| 229 | 
            +
                        pats += text.upcase if options.include?('i')
         | 
| 215 230 | 
             
                      end
         | 
| 216 231 | 
             
                    else
         | 
| 217 232 | 
             
                      range = range + "-" + text
         | 
| 218 233 | 
             
                      if range == "a-z"
         | 
| 219 | 
            -
                         | 
| 234 | 
            +
                        if options.include?('i')
         | 
| 235 | 
            +
                          pats = "L" + pats
         | 
| 236 | 
            +
                        else
         | 
| 237 | 
            +
                          pats = "x" + pats
         | 
| 238 | 
            +
                        end
         | 
| 220 239 | 
             
                      elsif range == "A-Z"
         | 
| 221 | 
            -
                         | 
| 240 | 
            +
                        if options.include?('i')
         | 
| 241 | 
            +
                          pats = "L" + pats
         | 
| 242 | 
            +
                        else
         | 
| 243 | 
            +
                          pats = "X" + pats
         | 
| 244 | 
            +
                        end
         | 
| 222 245 | 
             
                      elsif range == "0-9"
         | 
| 223 246 | 
             
                        pats = "n" + pats
         | 
| 224 247 | 
             
                      else
         | 
| 225 | 
            -
                         | 
| 226 | 
            -
             | 
| 227 | 
            -
             | 
| 228 | 
            -
             | 
| 229 | 
            -
             | 
| 248 | 
            +
                        if set
         | 
| 249 | 
            +
                          pats += (range[0]..range[2]).to_a.join
         | 
| 250 | 
            +
                          if options.include?('i')
         | 
| 251 | 
            +
                            pats += (range[0]..range[2]).to_a.join.upcase
         | 
| 252 | 
            +
                          end
         | 
| 253 | 
            +
                        else
         | 
| 254 | 
            +
                          trange = (range[0]..range[2]).to_a.join
         | 
| 255 | 
            +
                          if options.include?('i')
         | 
| 256 | 
            +
                            trange += trange.upcase
         | 
| 257 | 
            +
                          end
         | 
| 258 | 
            +
                          pats += "[" + trange + "]"
         | 
| 259 | 
            +
                        end
         | 
| 230 260 | 
             
                      end
         | 
| 231 261 | 
             
                      range = ""
         | 
| 232 262 | 
             
                    end
         | 
| 233 263 | 
             
                    pats = "[" + pats + "]" unless set
         | 
| 234 264 | 
             
                  when :interval
         | 
| 235 265 | 
             
                    size = text.sub(",", "-").sub("{", "").sub("}", "")
         | 
| 236 | 
            -
                    size.chop | 
| 266 | 
            +
                    size+=(default_infinite+size.chop.to_i).to_s if size[-1] == "-"
         | 
| 237 267 | 
             
                    pats = size + ":" + pats
         | 
| 238 268 | 
             
                    if !patg.empty?
         | 
| 239 269 | 
             
                      patg << pats
         | 
| @@ -568,6 +568,16 @@ class StringPattern | |
| 568 568 | 
             
                      good_result = true
         | 
| 569 569 | 
             
                    end
         | 
| 570 570 | 
             
                  end
         | 
| 571 | 
            +
                  if @block_list_enabled
         | 
| 572 | 
            +
                    if @block_list.is_a?(Array)
         | 
| 573 | 
            +
                      @block_list.each do |bl|
         | 
| 574 | 
            +
                        if string.match?(/#{bl}/i)
         | 
| 575 | 
            +
                          good_result = false
         | 
| 576 | 
            +
                          break
         | 
| 577 | 
            +
                        end
         | 
| 578 | 
            +
                      end
         | 
| 579 | 
            +
                    end
         | 
| 580 | 
            +
                  end
         | 
| 571 581 | 
             
                end until good_result or tries > 10000
         | 
| 572 582 | 
             
                unless good_result
         | 
| 573 583 | 
             
                  puts "Not possible to generate the string on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
         | 
    
        data/lib/string_pattern.rb
    CHANGED
    
    | @@ -25,9 +25,13 @@ require_relative "string/pattern/validate" | |
| 25 25 | 
             
            #             In case using regular expressions the maximum when using * or + for repetitions
         | 
| 26 26 | 
             
            # word_separator: (String, default: '_')
         | 
| 27 27 | 
             
            #             When generating words using symbol types 'w' or 'p' the character to separate the english or spanish words.
         | 
| 28 | 
            +
            # block_list: (Array, default: empty)
         | 
| 29 | 
            +
            #             Array of words to be avoided from resultant strings.
         | 
| 30 | 
            +
            # block_list_enabled: (TrueFalse, default: false)
         | 
| 31 | 
            +
            #             If true block_list will be take in consideration
         | 
| 28 32 | 
             
            class StringPattern
         | 
| 29 33 | 
             
              class << self
         | 
| 30 | 
            -
                attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values, :default_infinite, :word_separator
         | 
| 34 | 
            +
                attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values, :default_infinite, :word_separator, :block_list, :block_list_enabled
         | 
| 31 35 | 
             
              end
         | 
| 32 36 | 
             
              @national_chars = (("a".."z").to_a + ("A".."Z").to_a).join
         | 
| 33 37 | 
             
              @optimistic = true
         | 
| @@ -36,6 +40,8 @@ class StringPattern | |
| 36 40 | 
             
              @dont_repeat = false
         | 
| 37 41 | 
             
              @default_infinite = 10
         | 
| 38 42 | 
             
              @word_separator = "_"
         | 
| 43 | 
            +
              @block_list_enabled = false
         | 
| 44 | 
            +
              @block_list = []
         | 
| 39 45 | 
             
              NUMBER_SET = ("0".."9").to_a
         | 
| 40 46 | 
             
              SPECIAL_SET = [" ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "{", "}", "[", "]", "'", ";", ":", "?", ">", "<", "`", "|", "/", '"']
         | 
| 41 47 | 
             
              ALPHA_SET_LOWER = ("a".."z").to_a
         | 
| @@ -56,4 +62,5 @@ class StringPattern | |
| 56 62 | 
             
                @cache = Hash.new()
         | 
| 57 63 | 
             
                @national_chars = par
         | 
| 58 64 | 
             
              end
         | 
| 65 | 
            +
             | 
| 59 66 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,35 +1,35 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: string_pattern
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 2. | 
| 4 | 
            +
              version: 2.3.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Mario Ruiz
         | 
| 8 | 
            -
            autorequire: | 
| 8 | 
            +
            autorequire:
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2022- | 
| 11 | 
            +
            date: 2022-09-20 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: regexp_parser
         | 
| 15 15 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 16 | 
             
                requirements:
         | 
| 17 | 
            -
                - - ">="
         | 
| 18 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            -
                    version: 1.3.0
         | 
| 20 17 | 
             
                - - "~>"
         | 
| 21 18 | 
             
                  - !ruby/object:Gem::Version
         | 
| 22 | 
            -
                    version: ' | 
| 19 | 
            +
                    version: '2.5'
         | 
| 20 | 
            +
                - - ">="
         | 
| 21 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 22 | 
            +
                    version: 2.5.0
         | 
| 23 23 | 
             
              type: :runtime
         | 
| 24 24 | 
             
              prerelease: false
         | 
| 25 25 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 26 26 | 
             
                requirements:
         | 
| 27 | 
            -
                - - ">="
         | 
| 28 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 29 | 
            -
                    version: 1.3.0
         | 
| 30 27 | 
             
                - - "~>"
         | 
| 31 28 | 
             
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            -
                    version: ' | 
| 29 | 
            +
                    version: '2.5'
         | 
| 30 | 
            +
                - - ">="
         | 
| 31 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                    version: 2.5.0
         | 
| 33 33 | 
             
            description: 'Easily generate strings supplying a very simple pattern. ''10-20:Xn/x/''.generate
         | 
| 34 34 | 
             
              #>qBstvc6JN8ra. Generate random strings using a regular expression (Regexp): /[a-z0-9]{2,5}w+/.gen
         | 
| 35 35 | 
             
              . Also generate words in English or Spanish. Perfect to be used in test data factories.
         | 
| @@ -68,7 +68,7 @@ homepage: https://github.com/MarioRuiz/string_pattern | |
| 68 68 | 
             
            licenses:
         | 
| 69 69 | 
             
            - MIT
         | 
| 70 70 | 
             
            metadata: {}
         | 
| 71 | 
            -
            post_install_message: | 
| 71 | 
            +
            post_install_message:
         | 
| 72 72 | 
             
            rdoc_options: []
         | 
| 73 73 | 
             
            require_paths:
         | 
| 74 74 | 
             
            - lib
         | 
| @@ -83,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 83 83 | 
             
                - !ruby/object:Gem::Version
         | 
| 84 84 | 
             
                  version: '0'
         | 
| 85 85 | 
             
            requirements: []
         | 
| 86 | 
            -
            rubygems_version: 3. | 
| 87 | 
            -
            signing_key: | 
| 86 | 
            +
            rubygems_version: 3.2.15
         | 
| 87 | 
            +
            signing_key:
         | 
| 88 88 | 
             
            specification_version: 4
         | 
| 89 89 | 
             
            summary: 'Generate easily random strings following a simple pattern or regular expression.
         | 
| 90 90 | 
             
              ''10-20:Xn/x/''.generate #>qBstvc6JN8ra. Also generate words in English or Spanish.
         |