Class: CpfCnpjPlus::Formatter::Cnpj

Inherits:
Object
  • Object
show all
Defined in:
lib/cpf_cnpj_plus/formatter/cnpj.rb

Overview

Responsável por formatar números de CNPJ para exibição. Suporta o formato numérico tradicional e o novo formato alfanumérico, aplicando a máscara padrão XX.XXX.XXX/XXXX-XX.

Class Method Summary collapse

Class Method Details

.format(cnpj, strict: false) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/cpf_cnpj_plus/formatter/cnpj.rb', line 9

def self.format(cnpj, strict: false)
  cleaned = cnpj.to_s.upcase.gsub(/[^A-Z0-9]/, "")
  return nil unless cleaned.length == 14
  return nil if strict && !Validator::Cnpj.valid?(cleaned)

  "#{cleaned[0..1]}.#{cleaned[2..4]}.#{cleaned[5..7]}/#{cleaned[8..11]}-#{cleaned[12..13]}"
end