Sha256: c4e7ad858b92222170bd1e598e25a649dd3133616196c8c8ec1d54ee496ed8e5

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

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

    # Convert diacritics chars to their non-diacritic equivalents.
    #
    # Works for latin languages only.
    #
    # Returns a copy of the string with diacritics removed.
    def accent_buster
      self.tr(ACCENT_DOWNCASE + ACCENT_UPCASE, NO_ACCENT_DOWNCASE + NO_ACCENT_UPCASE)
    end

    # Convert diacritics chars to their non-diacritic equivalents.
    #
    # Works for latin languages only.
    #
    # Convert the string in place.
    #
    # Returns the string or nil if no changes were made.
    def accent_buster!
      self.tr!(ACCENT_DOWNCASE + ACCENT_UPCASE, NO_ACCENT_DOWNCASE + NO_ACCENT_UPCASE)
    end

    def downcase
      super.tr(ACCENT_UPCASE, ACCENT_DOWNCASE)
    end

    def downcase!
      super
      self.tr!(ACCENT_UPCASE, ACCENT_DOWNCASE)
    end

    def upcase
      super.tr(ACCENT_DOWNCASE, ACCENT_UPCASE)
    end

    def upcase!
      super
      self.tr!(ACCENT_DOWNCASE, ACCENT_UPCASE)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
accent-buster-1.1.1 lib/accent-buster/string_extension.rb
accent-buster-1.1.0 lib/accent-buster/string_extension.rb