File: lcs_compare_arrays.rb

package info (click to toggle)
ruby-hashdiff 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 1,296; makefile: 4
file content (32 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

module Hashdiff
  # @private
  # Used to compare arrays using the lcs algorithm
  class LcsCompareArrays
    class << self
      def call(obj1, obj2, opts = {})
        result = []

        changeset = Hashdiff.diff_array_lcs(obj1, obj2, opts) do |lcs|
          # use a's index for similarity
          lcs.each do |pair|
            prefix = Hashdiff.prefix_append_array_index(opts[:prefix], pair[0], opts)

            result.concat(Hashdiff.diff(obj1[pair[0]], obj2[pair[1]], opts.merge(prefix: prefix)))
          end
        end

        changeset.each do |change|
          next if change[0] != '-' && change[0] != '+'

          change_key = Hashdiff.prefix_append_array_index(opts[:prefix], change[1], opts)

          result << [change[0], change_key, change[2]]
        end

        result
      end
    end
  end
end