@@ -122,23 +122,14 @@ defmodule Jsonpatch do
122122 def diff ( source , destination )
123123
124124 def diff ( % { } = source , % { } = destination ) do
125- destination = Map . to_list ( destination )
126-
127- acc = diff_adds_and_replaces ( destination , source , "" )
128- acc = diff_removes ( destination , source , "" , acc )
129-
130- acc
125+ Map . to_list ( destination )
126+ |> do_diff ( source , "" )
131127 end
132128
133129 def diff ( source , destination ) when is_list ( source ) and is_list ( destination ) do
134- destination =
135- Enum . with_index ( destination )
136- |> Enum . map ( fn { v , k } -> { k , v } end )
137-
138- acc = diff_adds_and_replaces ( destination , source , "" )
139- acc = diff_removes ( destination , source , "" , acc )
140-
141- acc
130+ Enum . with_index ( destination )
131+ |> Enum . map ( fn { v , k } -> { k , v } end )
132+ |> do_diff ( source , "" )
142133 end
143134
144135 def diff ( _ , _ ) do
@@ -153,23 +144,13 @@ defmodule Jsonpatch do
153144 defguardp are_unequal_lists ( val1 , val2 )
154145 when val1 != val2 and is_list ( val2 ) and is_list ( val1 )
155146
156- defp diff_removes ( destination , source , current_path , acc )
157-
158- defp diff_removes ( [ ] , _ , _ , acc ) do
159- acc
160- end
161-
162- defp diff_removes ( [ { key , val } | tail ] , source , current_path , acc ) do
163- acc
164- end
165-
166- defp diff_adds_and_replaces ( destination , source , current_path , acc \\ [ ] )
147+ defp do_diff ( destination , source , current_path , acc \\ [ ] )
167148
168- defp diff_adds_and_replaces ( [ ] , _ , _ , acc ) do
149+ defp do_diff ( [ ] , _ , _ , acc ) do
169150 acc
170151 end
171152
172- defp diff_adds_and_replaces ( [ { key , val } | tail ] , source , ancestor_path , acc )
153+ defp do_diff ( [ { key , val } | tail ] , source , ancestor_path , acc )
173154 when is_list ( source ) or is_map ( source ) do
174155 current_path = "#{ ancestor_path } /#{ escape ( key ) } "
175156
@@ -189,7 +170,7 @@ defmodule Jsonpatch do
189170 source_val
190171 when are_unequal_lists ( source_val , val ) or are_unequal_maps ( source_val , val ) ->
191172 # Enter next level
192- diff_adds_and_replaces ( next_level ( val ) , source_val , current_path , acc )
173+ do_diff ( next_level ( val ) , source_val , current_path , acc )
193174
194175 # Scalar source val that is not equal
195176 source_val when source_val != val ->
@@ -200,7 +181,7 @@ defmodule Jsonpatch do
200181 end
201182
202183 # Diff next value of same level
203- diff_adds_and_replaces ( tail , source , ancestor_path , acc )
184+ do_diff ( tail , source , ancestor_path , acc )
204185 end
205186
206187 # Transforms a map into a tuple list and a list also into a tuple list with indizes
0 commit comments