lib/json/ld/compact.rb in json-ld-2.1.3 vs lib/json/ld/compact.rb in json-ld-2.1.4
- old
+ new
@@ -28,11 +28,11 @@
result = element.map {|item| compact(item, property: property)}.compact
# If element has a single member and the active property has no
# @container mapping to @list or @set, the compacted value is that
# member; otherwise the compacted value is element
- if result.length == 1 && context.container(property).nil? && @options[:compactArrays]
+ if result.length == 1 && !context.as_array?(property) && @options[:compactArrays]
#log_debug("=> extract single element: #{result.first.inspect}")
result.first
else
#log_debug("=> array result: #{result.inspect}")
result
@@ -83,11 +83,11 @@
compacted_value = compact(expanded_value, property: '@reverse')
#log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"}
compacted_value.each do |prop, value|
if context.reverse?(prop)
value = [value] if !value.is_a?(Array) &&
- (context.container(prop) == '@set' || !@options[:compactArrays])
+ (context.as_array?(prop) || !@options[:compactArrays])
#log_debug("") {"merge #{prop} => #{value.inspect}"}
merge_compacted_value(result, prop, value)
compacted_value.delete(prop)
end
@@ -99,10 +99,21 @@
result[al] = compacted_value
end
next
end
+ if expanded_property == '@preserve'
+ # Compact using `property`
+ compacted_value = compact(expanded_value, property: property)
+ #log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"}
+
+ unless compacted_value.is_a?(Array) && compacted_value.empty?
+ result['@preserve'] = compacted_value
+ end
+ next
+ end
+
if expanded_property == '@index' && context.container(property) == '@index'
#log_debug("@index") {"drop @index"}
next
end
@@ -148,10 +159,11 @@
else
result
end
container = context.container(item_active_property)
+ as_array = context.as_array?(item_active_property)
value = list?(expanded_item) ? expanded_item['@list'] : expanded_item
compacted_item = compact(value, property: item_active_property)
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
if list?(expanded_item)
@@ -193,17 +205,14 @@
when 1 then compacted_item[type_prop] = types.first
else compacted_item[type_prop] = types
end
compacted_item
end
+ compacted_item = [compacted_item] if as_array && !compacted_item.is_a?(Array)
merge_compacted_value(map_object, map_key, compacted_item)
else
compacted_item = [compacted_item] if
- !compacted_item.is_a?(Array) && (
- !@options[:compactArrays] ||
- %w(@set @list).include?(container) ||
- %w(@list @graph).include?(expanded_property)
- )
+ !compacted_item.is_a?(Array) && (!@options[:compactArrays] || as_array)
merge_compacted_value(nest_result, item_active_property, compacted_item)
end
end
end