Sha256: 5928bf6901b1d30f3993d9b4407c0045f28b77e736fad46d550675be62e00d19

Contents?: true

Size: 904 Bytes

Versions: 1

Compression:

Stored size: 904 Bytes

Contents

require "ca_postal_code/version"
require "ca_postal_code/region_guessing"

module CAPostalCode
  extend RegionGuessing

  # > Postal codes do not include the letters D, F, I, O, Q or U,
  # > and the first position also does not make use of the letters W or Z.
  PATTERN = %r{
    [ABCEGHJKLMNPRSTVXY]
    [0-9]
    [ABCEGHJKLMNPRSTVWXYZ]
    \ ?
    [0-9]
    [ABCEGHJKLMNPRSTVWXYZ]
    [0-9]
  }x

  def self.valid?(string)
    return false unless string = normalize(string)
    strict_valid?(string)
  end

  def self.strict_valid?(string)
    string.match? PATTERN
  end

  # Normalize postal code into "A1A 1A1" format.
  #
  # Will return `nil` if the normalized string does not have 6 characters.
  def self.normalize(string)
    string.strip!
    string.upcase!
    string.gsub!(/\W/, '')

    return unless string.length == 6

    string = string[0..2] + " " + string[3..5]
    string
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ca_postal_code-0.2.0 lib/ca_postal_code.rb