Sha256: 1567d32f7c26a3297c3fa2acc14540a6713ec6b1d537b9a4389e7830796a81a9

Contents?: true

Size: 368 Bytes

Versions: 3

Compression:

Stored size: 368 Bytes

Contents

# frozen_string_literal: true

class CPF
  class Formatter
    STRICT_REGEX = /[.-]/.freeze
    LOOSE_REGEX = /[^\d]/.freeze

    def self.format(number)
      strip(number).gsub(/\A(\d{3})(\d{3})(\d{3})(\d{2})\Z/, "\\1.\\2.\\3-\\4")
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cpf_cnpj-1.0.1 lib/cpf/formatter.rb
cpf_cnpj-1.0.0 lib/cpf/formatter.rb
cpf_cnpj-0.6.0 lib/cpf/formatter.rb