Sha256: 444f6363296a9c46f3c2a82c53a5a271e18dfb3bcdf5d1e4d8bf90e23c8f8c6d

Contents?: true

Size: 817 Bytes

Versions: 4

Compression:

Stored size: 817 Bytes

Contents

# frozen_string_literal: true

module Rordash
  module RegexUtil
    EMAIL_MATCH = URI::MailTo::EMAIL_REGEXP
    POSTAL_CODE_MATCH = /[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d/.freeze
    DOTTED_INDEX_MATCH = /\w+\[\d+\]/.freeze
    URI_MATCH = URI::DEFAULT_PARSER.make_regexp.freeze

    TYPE = {
      email: EMAIL_MATCH,
      uri: URI_MATCH,
      postal_code: POSTAL_CODE_MATCH,
      dotted_index: DOTTED_INDEX_MATCH
    }.freeze

    class << self
      def match?(type, value)
        TYPE[type].match?(value)
      end

      def match(type, value)
        TYPE[type].match(value)
      end

      def replace(type, value, replacement)
        return value unless value.is_a?(String)

        regex = TYPE[type]
        return value if regex.nil?

        value.sub(regex, replacement)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rordash-0.1.3 lib/rordash/regex_util.rb
rordash-0.1.2 lib/rordash/regex_util.rb
rordash-0.1.1 lib/rordash/regex_util.rb
rordash-0.1.0 lib/rordash/regex_util.rb