<commit_before>require 'version'

module SimpleCurrencyFormat
  class CurrencyFormat
    def self.format(value, options={})
      currency = CurrencyFormat.new(value)
      currency.prepare_separators(options)
      currency.format_currency
    end

    def initialize(value)
      @value = value
    end

    def prepare_separators(options)
      @symbol = options[:with_symbol] || '$'
      if options[:millions]
        @millions = "'"
      end
      @thousands = options[:thousands]
      @decimal = options[:decimal] || '.'

      @decimal = ',' if @thousands == '.' and @decimal == '.'
    end
    def format_currency
      currency = sprintf("%0.2f", @value).split(".")
      millions_amount = prepare_millions(currency[0])
      thousands = prepare_thousands(millions_amount)
      currency[0] = thousands.join(@millions).reverse!
      currency = currency.join(@decimal)
      "#{@symbol}#{currency}"
    end

    def prepare_millions(amount)
      millions = []
      amount.reverse!

      if amount.size > 6
        final_amount = []

        until amount.size < 6
          final_amount << amount.slice!(0..5)
        end

        final_amount << amount
        millions << final_amount
      else
        millions << amount
      end

      millions
    end

    def prepare_thousands(amount)
      thousands_amount = []
      amount.each do |piece_amount|
        thousands_amount << slice_amount(piece_amount)
      end

      thousands_amount
    end

    def slice_amount(piece_amount)
      final_amount = []
      if piece_amount.size > 2
        until piece_amount.size < 3
          final_amount << piece_amount.slice!(0..2)
        end

        final_amount << piece_amount
        final_amount.join(@thousands)
      else
        piece_amount
      end
    end
  end
end<commit_msg>clean code for thousands and millions<commit_after>require 'version'

module SimpleCurrencyFormat
  class CurrencyFormat
    def self.format(value, options={})
      currency = CurrencyFormat.new(value)
      currency.prepare_separators(options)
      currency.format_currency
    end

    def initialize(value)
      @value = value
    end

    def prepare_separators(options)
      @symbol = options[:with_symbol] || '$'
      if options[:millions]
        @millions = "'"
      end
      @thousands = options[:thousands]
      @decimal = options[:decimal] || '.'

      @decimal = ',' if @thousands == '.' and @decimal == '.'
    end

    def format_currency
      currency = sprintf("%0.2f", @value).split(".")

      millions = [prepare_millions(currency[0])]
      thousands = prepare_thousands(millions)

      currency[0] = thousands.join(@millions).reverse!
      currency = currency.join(@decimal)

      "#{@symbol}#{currency}"
    end

    def prepare_millions(amount)
      slice_amount(amount.reverse!, 6, false)
    end

    def prepare_thousands(thousands)
      thousands.collect! { |amount| slice_amount(amount, 3, true) }
    end

    def slice_amount(amount, unit_size, join_thousands)
      return amount unless amount.size > unit_size

      range = unit_size - 1
      min_size = unit_size + 1

      final_amount = []
      final_amount << amount.slice!(0..range) until amount.size < min_size
      final_amount << amount

      if join_thousands
        final_amount.join(@thousands)
      else
        final_amount
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10607407407407407, 'fraction_numerical': 0.006222222222222222, 'mean_word_length': 2.4804123711340207, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083000', 'n_tokens_mistral': 1019, 'n_tokens_neox': 989, 'n_words': 210}