# frozen_string_literal: true

module RubyEventStore
  class TransformKeys
    class << self
      def stringify(data)
        deep_transform(data, &:to_s)
      end

      def symbolize(data)
        deep_transform(data, &:to_sym)
      end

      private

      def deep_transform(data, &block)
        data.each_with_object({}) do |(k, v), h|
          h[yield(k)] =
            case v
            when Hash
              deep_transform(v, &block)
            when Array
              v.map { |i| Hash === i ? deep_transform(i, &block) : i }
            else
              v
            end
        end
      end
    end
  end
end

# frozen_string_literal: true

module RubyEventStore
  class TransformKeys
    class << self
      def stringify(data)
        deep_transform(data, &:to_s)
      end

      def symbolize(data)
        deep_transform(data, &:to_sym)
      end

      private

      def deep_transform(data, &block)
        case data
        when Hash
          data.each_with_object({}) do |(key, value), hash|
            hash[yield(key)] = deep_transform(value, &block)
          end
        when Array
          data.map { |i| deep_transform(i, &block) }
        else
          data
        end
      end
    end
  end
end

Refactor to handle Array of arrays
Refactor to handle Array of arrays
