Sha256: 5585cee8c7832d6b87f201556eed113e0fd9e2750da0f6de0b4bb9835ea68d18

Contents?: true

Size: 392 Bytes

Versions: 1

Compression:

Stored size: 392 Bytes

Contents

# frozen_string_literal: true

class CNPJ
  class Formatter
    STRICT_REGEX = %r{[/.-]}.freeze
    LOOSE_REGEX = /[^\d]/.freeze

    def self.format(number)
      strip(number)
        .gsub(/\A(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})\Z/, "\\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

1 entries across 1 versions & 1 rubygems

Version Path
cpf_cnpj-0.6.0 lib/cnpj/formatter.rb