Sha256: f4283c035b71f1873f1de19830fa95f0ccf1d44d11cc3609037ac72394c14ee9

Contents?: true

Size: 1.57 KB

Versions: 33

Compression:

Stored size: 1.57 KB

Contents

module Metanorma
  module Utils
    module Array
      def stringify_all_keys
        map do |v|
          case v
          when ::Hash, ::Array
            v.stringify_all_keys
          else
            v
          end
        end
      end

      def symbolize_all_keys
        map do |v|
          case v
          when ::Hash, ::Array
            v.symbolize_all_keys
          else
            v
          end
        end
      end
    end
  end
end

module Metanorma
  module Utils
    module Hash
      def stringify_all_keys
        result = {}
        each do |k, v|
          result[k.to_s] = case v
                           when ::Hash, ::Array
                             v.stringify_all_keys
                           else
                             v
                           end
        end
        result
      end

      def symbolize_all_keys
        result = {}
        each do |k, v|
          result[k.to_sym] = case v
                             when ::Hash, ::Array
                               v.symbolize_all_keys
                             else
                               v
                             end
        end
        result
      end

      def deep_merge(second)
        merger = proc { |_, v1, v2|
          if ::Hash === v1 && ::Hash === v2
            v1.merge(v2, &merger)
          elsif ::Array === v1 && ::Array === v2
            v1 | v2
          elsif [:undefined, nil,
                 :nil].include?(v2)
            v1
          else
            v2
          end
        }
        merge(second.to_h, &merger)
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
metanorma-utils-1.8.2 lib/utils/hash_transform_keys.rb
metanorma-utils-1.8.1 lib/utils/hash_transform_keys.rb
metanorma-utils-1.8.0 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.7 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.6 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.5 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.4 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.3 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.2 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.1 lib/utils/hash_transform_keys.rb
metanorma-utils-1.7.0 lib/utils/hash_transform_keys.rb
metanorma-utils-1.6.5 lib/utils/hash_transform_keys.rb
metanorma-utils-1.6.4 lib/utils/hash_transform_keys.rb
metanorma-utils-1.6.3 lib/utils/hash_transform_keys.rb
metanorma-utils-1.6.2 lib/utils/hash_transform_keys.rb
metanorma-utils-1.6.1 lib/utils/hash_transform_keys.rb
metanorma-utils-1.6.0 lib/utils/hash_transform_keys.rb
metanorma-utils-1.5.5 lib/utils/hash_transform_keys.rb
metanorma-utils-1.5.4 lib/utils/hash_transform_keys.rb
metanorma-utils-1.5.1 lib/utils/hash_transform_keys.rb