Sha256: de4b0f6b62382b82bc9594b07e38291a3f22e75a206a78472d25f894ff473114

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

class Object
  def equivalent_jsonld?(other, ordered: false)
    self == other
  end
end

class Hash
  def equivalent_jsonld?(other, ordered: false)
    return false unless other.is_a?(Hash) && other.length == length
    all? do |key, value|
      # List values are still ordered
      if key == '@language' && value.is_a?(String)
        value.downcase.equivalent_jsonld?(other[key].to_s.downcase, ordered: key == '@list')
      else
        value.equivalent_jsonld?(other[key], ordered: key == '@list')
      end
    end
  end

  def diff(other)
    self.keys.inject({}) do |memo, key|
      unless self[key] == other[key]
        memo[key] = [self[key], other[key]] 
      end
      memo
    end
  end
end

class Array
  def equivalent_jsonld?(other, ordered: false)
    return false unless other.is_a?(Array) && other.length == length
    if ordered
      b = other.dup
      # All elements must match in order
      all? {|av| av.equivalent_jsonld?(b.shift)}
    else
      # Look for any element which matches
      all? do |av|
        other.any? {|bv| av.equivalent_jsonld?(bv)}
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
json-ld-3.2.4 spec/support/extensions.rb
json-ld-3.2.3 spec/support/extensions.rb
json-ld-3.2.2 spec/support/extensions.rb
json-ld-3.2.1 spec/support/extensions.rb
json-ld-3.2.0 spec/support/extensions.rb
json-ld-3.1.10 spec/support/extensions.rb
json-ld-3.1.9 spec/support/extensions.rb
json-ld-3.1.8 spec/support/extensions.rb
json-ld-3.1.7 spec/support/extensions.rb
json-ld-3.1.6 spec/support/extensions.rb
json-ld-3.1.5 spec/support/extensions.rb
json-ld-3.1.4 spec/support/extensions.rb
json-ld-3.1.3 spec/support/extensions.rb
json-ld-3.1.2 spec/support/extensions.rb
json-ld-3.1.1 spec/support/extensions.rb
json-ld-3.1.0 spec/support/extensions.rb