Sha256: cdc73750e812655a399911da51f77a875a760eb52d9e3acb3e33b22ebc97b0a6

Contents?: true

Size: 683 Bytes

Versions: 1

Compression:

Stored size: 683 Bytes

Contents

# frozen_string_literal: true

module BraDocuments
  class Formatter
    FORMATS = {
      cpf: { pattern: /\A(\d{3})(\d{3})(\d{3})(\d{2})\z/, mask: '%s.%s.%s-%s' },
      cnpj: { pattern: /\A(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})\z/, mask: '%s.%s.%s/%s-%s'}
    }.freeze

    # Formats a only numbers CPF or CNPJ in their own mask
    #
    # BraDocuments::Formatter.format('86027265892', as: :cpf) # => "860.272.658-92"
    # BraDocuments::Formatter.format('53855973879456', as: :cnpj) # => "53.855.973/8794-56"
    def self.format(number, as:)
      format_data = FORMATS[as]

      Kernel.format(format_data[:mask], *format_data[:pattern].match(number).captures)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bra_documents-1.0.0 lib/bra_documents/formatter.rb