Sha256: 87dd9ff5579260dc9ec4b19eeb896801168f6a9679a63f724e24bc17d7c095b0

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

class AccentBuster::Buster
  ACCENT_DOWNCASE    = "áéíóúâêîôûäëïöüãõñç"
  NO_ACCENT_DOWNCASE = "aeiouaeiouaeiouaonc"
  ACCENT_UPCASE      = "ÁÉÍÓÚÂÊÎÔÛÄËÏÖÜÃÕÑÇ"
  NO_ACCENT_UPCASE   = "AEIOUAEIOUAEIOUAONC"

  # Wrap an object to add accent wise (diacritic wise)
  # methods.
  #
  # It works for latin languages only.
  #
  # @param a_string [#to_s]
  def initialize(a_string)
    @a_string = a_string.to_s
  end

  # Convert diacritics chars to their non-diacritic equivalents.
  #
  # "áéíóú âêîôû äëïöü ãõ ñç" => "aeiou aeiou aeiou ao nc"
  #
  # @return the string with diacritics removed.
  def bust
    @a_string.tr(ACCENT_DOWNCASE + ACCENT_UPCASE, NO_ACCENT_DOWNCASE + NO_ACCENT_UPCASE)
  end

  # Upcase the string, correctly converting diacritcs.
  #
  # Example: "ótimo" => "ÓTIMO"
  #
  # @return the string upper case
  def up
    @a_string.tr(ACCENT_DOWNCASE, ACCENT_UPCASE).upcase
  end

  # Downcase the string, correctly converting diacritcs.
  #
  # Example: "ÓTIMO" => "ótimo"
  #
  # @return the string lower case
  def down
    @a_string.tr(ACCENT_UPCASE, ACCENT_DOWNCASE).downcase
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
accent-buster-2.0.0 lib/accent-buster/buster.rb