Sha256: e2576ef93df595a82e5594d59ea87b0d19f71a84b261a697cabd13bf994f062c

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

require 'transproc/conditional'

module Transproc
  # Recursive transformation functions
  #
  # @example
  #   require 'transproc/recursion'
  #
  #   include Transproc::Helper
  #
  #   fn = t(:hash_recursion, t(:symbolize_keys))
  #
  #   fn["name" => "Jane", "address" => { "street" => "Street 1", "zipcode" => "123" }]
  #   # => {:name=>"Jane", :address=>{:street=>"Street 1", :zipcode=>"123"}}
  #
  # @api public
  module Recursion
    extend Functions

    IF_ARRAY = -> fn { Transproc(:is, Array, fn) }

    IF_HASH = -> fn { Transproc(:is, Hash, fn) }

    # Recursively apply the provided transformation function to an array
    #
    # @example
    #   Transproc(:array_recursion, -> s { s.compact })[
    #     [['Joe', 'Jane', nil], ['Smith', 'Doe', nil]]
    #   ]
    #   # =>  [["Joe", "Jane"], ["Smith", "Doe"]]
    #
    # @param [Array]
    #
    # @return [Array]
    #
    # @api public
    def array_recursion(value, fn)
      result = fn[value]
      guarded = IF_ARRAY[-> v { Transproc(:array_recursion, fn)[v] }]

      result.map! do |item|
        guarded[item]
      end
    end

    # Recursively apply the provided transformation function to a hash
    #
    # @example
    #   Transproc(:hash_recursion, Transproc(:symbolize_keys))[
    #     ["name" => "Jane", "address" => { "street" => "Street 1", "zipcode" => "123" }]
    #   ]
    #   # =>  {:name=>"Jane", :address=>{:street=>"Street 1", :zipcode=>"123"}}
    #
    # @param [Hash]
    #
    # @return [Hash]
    #
    # @api public
    def hash_recursion(value, fn)
      result = fn[value]
      guarded = IF_HASH[-> v { Transproc(:hash_recursion, fn)[v] }]

      result.keys.each do |key|
        result[key] = guarded[result.delete(key)]
      end

      result
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
transproc-0.2.3 lib/transproc/recursion.rb
transproc-0.2.2 lib/transproc/recursion.rb
transproc-0.2.1 lib/transproc/recursion.rb
transproc-0.2.0 lib/transproc/recursion.rb