Sha256: d967fe9cb3de2203d19a62ce958742100e18490cfbdf22e13c6f2e53cffd511d

Contents?: true

Size: 465 Bytes

Versions: 2

Compression:

Stored size: 465 Bytes

Contents

# frozen_string_literal: true

class CNPJ
  class Formatter
    STRICT_REGEX = %r{[/.-]}.freeze
    LOOSE_REGEX = /[^A-Z\d]/.freeze
    REPLACE_REGEX = /
      \A([A-Z\d]{2})([A-Z\d]{3})([A-Z\d]{3})([A-Z\d]{4})([A-Z\d]{2})\Z
    /x.freeze

    def self.format(number)
      strip(number).gsub(REPLACE_REGEX, "\\1.\\2.\\3/\\4-\\5")
    end

    def self.strip(number, strict = false)
      number.to_s.gsub(strict ? STRICT_REGEX : LOOSE_REGEX, "")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cpf_cnpj-1.0.1 lib/cnpj/formatter.rb
cpf_cnpj-1.0.0 lib/cnpj/formatter.rb