Sha256: 70d0797b577de97be6f9adfde41d09a8b3fae3c688a799b94cf0a8034faa2ec7

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

require_relative 'constants/es'

module Humanize
  class Es
    def humanize(number)
      iteration = 0
      parts = []
      use_and = false
      million = false
      until number.zero?
        number, remainder = number.divmod(1000)

        unless remainder.zero?
          if iteration.zero? && remainder < 100
            use_and = true
          else
            million = true
            add_grouping(parts, use_and, iteration, remainder)
          end
          parts << check_millions(SUB_ONE_GROUPING[remainder], million) unless exactly_one_thousand?(remainder, parts)
        end

        iteration += 1
      end
      parts
    end

    private

    def exactly_one_thousand?(remainder, parts)
      remainder == 1 && parts.last.to_s.strip == 'mil'
    end

    def check_millions(grouping, million)
      return 'un' if million && grouping == 'uno'

      grouping
    end

    def add_grouping(parts, _use_and, iteration, remainder)
      grouping = LOTS[iteration]
      grouping = 'millones' if grouping == 'millón' && remainder > 1
      return unless grouping

      parts << grouping
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
humanize-3.0.0 lib/humanize/locales/es.rb
humanize-2.5.1 lib/humanize/locales/es.rb
humanize-2.5.0 lib/humanize/locales/es.rb
humanize-2.4.3 lib/humanize/locales/es.rb
humanize-2.4.2 lib/humanize/locales/es.rb
humanize-2.4.1 lib/humanize/locales/es.rb