module OnebusawaySDK::Internal::Util

def dig(data, pick, &blk)

Returns:
  • (Object, nil) -

Parameters:
  • blk (Proc, nil) --
  • pick (Symbol, Integer, Array, Proc, nil) --
  • data (Hash{Symbol=>Object}, Array, Object) --
    Other tags:
      Api: - private
    def dig(data, pick, &blk)
      case [data, pick]
      in [_, nil]
        data
      in [Hash, Symbol] | [Array, Integer]
        data.fetch(pick) { blk&.call }
      in [Hash | Array, Array]
        pick.reduce(data) do |acc, key|
          case acc
          in Hash if acc.key?(key)
            acc.fetch(key)
          in Array if key.is_a?(Integer) && key < acc.length
            acc[key]
          else
            return blk&.call
          end
        end
      in [_, Proc]
        pick.call(data)
      else
        blk&.call
      end
    end