Skip to content

Commit 969dad3

Browse files
committed
Fix uses of .keys.any?, .keys.include?, .keys == %w(), .keys.length
1 parent 91e4d8f commit 969dad3

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

lib/json/ld/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def self.expand(input, options = {}, &block)
174174
end
175175

176176
# If, after the algorithm outlined above is run, the resulting element is an JSON object with just a @graph property, element is set to the value of @graph's value.
177-
result = result['@graph'] if result.is_a?(Hash) && result.keys == %w(@graph)
177+
result = result['@graph'] if result.is_a?(Hash) && result.length == 1 && result.key?('@graph')
178178

179179
# Finally, if element is a JSON object, it is wrapped into an array.
180180
result = [result].compact unless result.is_a?(Array)

lib/json/ld/compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def compact(element, property: nil)
4343
# @null objects are used in framing
4444
return nil if element.has_key?('@null')
4545

46-
if element.keys.any? {|k| %w(@id @value).include?(k)}
46+
if element.key?('@id') || element.key?('@value')
4747
result = context.compact_value(property, element, log_depth: @options[:log_depth])
4848
unless result.is_a?(Hash)
4949
#log_debug("") {"=> scalar result: #{result.inspect}"}

lib/json/ld/context.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def create_term_definition(local_context, term, defined)
615615

616616
if value.has_key?('@reverse')
617617
raise JsonLdError::InvalidReverseProperty, "unexpected key in #{value.inspect} on term #{term.inspect}" if
618-
value.keys.any? {|k| %w(@id @nest).include?(k)}
618+
value.key?('@id') || value.key?('@nest')
619619
raise JsonLdError::InvalidIRIMapping, "expected value of @reverse to be a string: #{value['@reverse'].inspect} on term #{term.inspect}" unless
620620
value['@reverse'].is_a?(String)
621621

@@ -1297,7 +1297,7 @@ def expand_value(property, value, useNativeTypes: false, **options)
12971297
def compact_value(property, value, options = {})
12981298
#log_debug("compact_value") {"property: #{property.inspect}, value: #{value.inspect}"}
12991299

1300-
num_members = value.keys.length
1300+
num_members = value.length
13011301

13021302
num_members -= 1 if index?(value) && container(property) == '@index'
13031303
if num_members > 2

lib/json/ld/expand.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ def expand(input, active_property, context, ordered: true)
8787
elsif !output_object.fetch('@type', []).is_a?(Array)
8888
# Otherwise, if result contains the key @type and its associated value is not an array, set it to an array containing only the associated value.
8989
output_object['@type'] = [output_object['@type']]
90-
elsif output_object.keys.any? {|k| %w(@set @list).include?(k)}
90+
elsif output_object.key?('@set') || output_object.key?('@list')
9191
# Otherwise, if result contains the key @set or @list:
9292
# The result must contain at most one other key and that key must be @index. Otherwise, an invalid set or list object error has been detected and processing is aborted.
9393
raise JsonLdError::InvalidSetOrListObject,
9494
"@set or @list may only contain @index: #{output_object.keys.inspect}" unless
9595
(output_object.keys - %w(@set @list @index)).empty?
9696

9797
# If result contains the key @set, then set result to the key's associated value.
98-
return output_object['@set'] if output_object.keys.include?('@set')
98+
return output_object['@set'] if output_object.key?('@set')
9999
end
100100

101101
# If result contains only the key @language, set result to null.
102-
return nil if output_object.keys == %w(@language)
102+
return nil if output_object.length == 1 && output_object.key?('@language')
103103

104104
# If active property is null or @graph, drop free-floating values as follows:
105105
if (active_property || '@graph') == '@graph' &&
106-
(output_object.keys.any? {|k| %w(@value @list).include?(k)} ||
106+
(output_object.key?('@value') || output_object.key?('@list') ||
107107
(output_object.keys - %w(@id)).empty? && !framing)
108108
#log_debug(" =>") { "empty top-level: " + output_object.inspect}
109109
return nil

0 commit comments

Comments
 (0)