lib/json/ld/extensions.rb in json-ld-2.1.5 vs lib/json/ld/extensions.rb in json-ld-2.1.6

- old
+ new

@@ -15,16 +15,17 @@ # @yieldparam [Object] a # @yieldparam [Object] b # @yieldreturn [Integer] # @return [Array] KW_ORDER = %w(@base @id @value @type @language @vocab @container @graph @list @set @index).freeze + KW_ORDER_CACHE = KW_ORDER.each_with_object({}) do |kw, memo| + memo[kw] = "@#{KW_ORDER.index(kw)}" + end.freeze # Order, considering keywords to come before other strings def kw_sort self.sort do |a, b| - a = "@#{KW_ORDER.index(a)}" if KW_ORDER.include?(a) - b = "@#{KW_ORDER.index(b)}" if KW_ORDER.include?(b) - a <=> b + KW_ORDER_CACHE.fetch(a, a) <=> KW_ORDER_CACHE.fetch(b, b) end end # Order terms, length first, then lexographically def term_sort