lib/json/ld/compact.rb in json-ld-2.1.5 vs lib/json/ld/compact.rb in json-ld-2.1.6
- old
+ new
@@ -41,11 +41,11 @@
# Otherwise element is a JSON object.
# @null objects are used in framing
return nil if element.has_key?('@null')
- if element.keys.any? {|k| %w(@id @value).include?(k)}
+ if element.key?('@id') || element.key?('@value')
result = context.compact_value(property, element, log_depth: @options[:log_depth])
unless result.is_a?(Hash)
#log_debug("") {"=> scalar result: #{result.inspect}"}
return result
end
@@ -56,11 +56,11 @@
element.keys.sort.each do |expanded_property|
expanded_value = element[expanded_property]
#log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"}
- if %w(@id @type).include?(expanded_property)
+ if expanded_property == '@id' || expanded_property == '@type'
compacted_value = [expanded_value].flatten.compact.map do |expanded_type|
context.compact_iri(expanded_type, vocab: (expanded_property == '@type'), log_depth: @options[:log_depth])
end
# If key is @type and any compacted value is a term having a local context, overlay that context.
@@ -116,18 +116,18 @@
#log_debug("@index") {"drop @index"}
next
end
# Otherwise, if expanded property is @index, @value, or @language:
- if %w(@index @value @language).include?(expanded_property)
+ if expanded_property == '@index' || expanded_property == '@value' || expanded_property == '@language'
al = context.compact_iri(expanded_property, vocab: true, quiet: true)
#log_debug(expanded_property) {"#{al} => #{expanded_value.inspect}"}
result[al] = expanded_value
next
end
- if expanded_value == []
+ if expanded_value.empty?
item_active_property =
context.compact_iri(expanded_property,
value: expanded_value,
vocab: true,
reverse: inside_reverse,
@@ -179,11 +179,11 @@
raise JsonLdError::CompactionToListOfLists,
"key cannot have more than one list value" if nest_result.has_key?(item_active_property)
end
end
- if %w(@language @index @id @type).include?(container)
+ if container == '@language' || container == '@index' || container == '@id' || container == '@type'
map_object = nest_result[item_active_property] ||= {}
compacted_item = case container
when '@id'
id_prop = context.compact_iri('@id', vocab: true, quiet: true)
map_key = compacted_item[id_prop]
@@ -216,10 +216,10 @@
end
end
end
# Re-order result keys
- result.keys.kw_sort.inject({}) {|map, kk| map[kk] = result[kk]; map}
+ result.keys.kw_sort.each_with_object({}) {|kk, memo| memo[kk] = result[kk]}
else
# For other types, the compacted value is the element value
#log_debug("compact") {element.class.to_s}
element
end