@@ -89,12 +89,12 @@ function isIdenticalArrays(a, b) {
8989 return toCompatible ( )
9090}
9191
92- function isCompatibleObjects ( expected , actual , compareValues = true , compareExact = false ) {
92+ function isCompatibleObjects ( expected , actual , config ) {
9393 if ( Array . isArray ( expected ) ) {
94- return isCompatibleArrays ( expected , actual , compareValues , compareExact )
94+ return isCompatibleArrays ( expected , actual , config )
9595 }
9696
97- if ( compareExact ) {
97+ if ( config . compareExact ) {
9898 const compatibilityMessage = isIdenticalArrays ( Object . keys ( expected ) , Object . keys ( actual ) )
9999 if ( ! compatibilityMessage . isEqual ) {
100100 return compatibilityMessage
@@ -103,13 +103,17 @@ function isCompatibleObjects(expected, actual, compareValues = true, compareExac
103103
104104 for ( const key of Object . keys ( expected ) ) {
105105
106+ if ( config . ignoreJsonKeys . includes ( key ) ) {
107+ continue
108+ }
109+
106110 if ( expected [ key ] instanceof Object ) {
107111
108112 if ( ! ( actual [ key ] instanceof Object ) ) {
109113 return toNotCompatible ( expected , actual , '!(' + actual [ key ] + ') instanceof Object' )
110114 }
111115
112- let compatibleObjects = isCompatibleObjects ( expected [ key ] , actual [ key ] , compareValues , compareExact )
116+ let compatibleObjects = isCompatibleObjects ( expected [ key ] , actual [ key ] , config )
113117 if ( ! compatibleObjects . isEqual ) {
114118 return compatibleObjects
115119 }
@@ -120,7 +124,7 @@ function isCompatibleObjects(expected, actual, compareValues = true, compareExac
120124 return toNotCompatible ( expected , actual , '!Array.isArray(' + actual [ key ] + ')' )
121125 }
122126
123- let compatibleArrays = isCompatibleArrays ( expected [ key ] , actual [ key ] , compareValues , compareExact )
127+ let compatibleArrays = isCompatibleArrays ( expected [ key ] , actual [ key ] , config )
124128 if ( ! compatibleArrays . isEqual ) {
125129 return compatibleArrays
126130 }
@@ -131,15 +135,15 @@ function isCompatibleObjects(expected, actual, compareValues = true, compareExac
131135 return toNotCompatible ( expected , actual , '!' + actual + '.hasOwnProperty(' + key + ')' )
132136 }
133137
134- if ( compareValues && ! isValueEqual ( expected [ key ] , actual [ key ] , compareExact ) ) {
138+ if ( config . compareValues && ! isValueEqual ( expected [ key ] , actual [ key ] , config . compareExact ) ) {
135139 return toNotCompatible ( expected , actual , '!isValueEqual(' + expected [ key ] + ', ' + actual [ key ] + ')' )
136140 }
137141 }
138142 }
139143 return toCompatible ( )
140144}
141145
142- function isCompatibleArrays ( expected , actual , compareValues = true , compareExact = false ) {
146+ function isCompatibleArrays ( expected , actual , config ) {
143147 if ( ! Array . isArray ( actual ) ) {
144148 return toNotCompatible ( expected , actual , 'expected array was object' )
145149 }
@@ -161,13 +165,13 @@ function isCompatibleArrays(expected, actual, compareValues = true, compareExact
161165 const actualValue = actualToCompare [ j ]
162166
163167 if ( Array . isArray ( expectedValue ) ) {
164- let compatibilityMessage = isCompatibleArrays ( expectedValue , actualValue , compareValues , compareExact )
168+ let compatibilityMessage = isCompatibleArrays ( expectedValue , actualValue , config )
165169 if ( ! compatibilityMessage . isEqual ) {
166170 return compatibilityMessage
167171 }
168172 }
169173 else {
170- let compatibilityMessage = isCompatibleObjects ( expectedValue , actualValue , compareValues , compareExact )
174+ let compatibilityMessage = isCompatibleObjects ( expectedValue , actualValue , config )
171175 if ( compatibilityMessage . isEqual ) {
172176 expectedFound . push ( expectedValue )
173177 actualToCompare [ j ] = undefined
@@ -177,7 +181,7 @@ function isCompatibleArrays(expected, actual, compareValues = true, compareExact
177181 }
178182 }
179183
180- if ( compareExact ) {
184+ if ( config . compareExact ) {
181185 const actualNotFound = actualToCompare . filter ( a => a !== undefined )
182186 if ( actualNotFound . length > 0 ) {
183187 return toNotCompatible ( expected , actual , 'Json structures not exact equals' , {
@@ -188,7 +192,7 @@ function isCompatibleArrays(expected, actual, compareValues = true, compareExact
188192 }
189193 }
190194
191- if ( compareValues ) {
195+ if ( config . compareValues ) {
192196 return expectedFound . length === expected . length
193197 ? toCompatible ( )
194198 : toNotCompatible ( expected , actual , 'Json structures not compatible' , {
@@ -210,28 +214,10 @@ function isCompatibleArrays(expected, actual, compareValues = true, compareExact
210214 }
211215}
212216
213- function isJsonStructureCompatible ( expected , actual ) {
214- return Array . isArray ( expected )
215- ? isCompatibleArrays ( expected , actual , false , false )
216- : isCompatibleObjects ( expected , actual , false , false )
217- }
218-
219- function isJsonCompatible ( expected , actual ) {
220- return Array . isArray ( expected )
221- ? isCompatibleArrays ( expected , actual , true , false )
222- : isCompatibleObjects ( expected , actual , true , false )
223- }
224-
225- function isJsonStructureExact ( expected , actual ) {
217+ export function compareJson ( expected , actual , config ) {
226218 return Array . isArray ( expected )
227- ? isCompatibleArrays ( expected , actual , false , true )
228- : isCompatibleObjects ( expected , actual , false , true )
229- }
230-
231- function isJsonIdentical ( expected , actual ) {
232- return Array . isArray ( expected )
233- ? isCompatibleArrays ( expected , actual , true , true )
234- : isCompatibleObjects ( expected , actual , true , true )
219+ ? isCompatibleArrays ( expected , actual , config )
220+ : isCompatibleObjects ( expected , actual , config )
235221}
236222
237223export const COMPARISON = {
@@ -241,20 +227,30 @@ export const COMPARISON = {
241227 EXACT : 'exact'
242228}
243229
244- export function compareJson ( expected , actual , comparison ) {
230+ export function toConfig ( comparison , ignoreJsonKeys , ignoreJsonPaths , allValuePaths ) {
245231 switch ( comparison . toLowerCase ( ) ) {
246232 case COMPARISON . COMPATIBLE_STRUCTURE :
247- return isJsonStructureCompatible ( expected , actual )
233+ return toConfigDto ( false , false , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
248234 case COMPARISON . COMPATIBLE :
249- return isJsonCompatible ( expected , actual )
235+ return toConfigDto ( true , false , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
250236 case COMPARISON . EXACT_STRUCTURE :
251- return isJsonStructureExact ( expected , actual )
237+ return toConfigDto ( false , true , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
252238 case COMPARISON . EXACT :
253- return isJsonIdentical ( expected , actual )
239+ return toConfigDto ( true , true , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
254240 default :
255241 throw {
256242 error : 'Comparison unsupported: ' + comparison . toLowerCase ( ) ,
257243 comparisons : COMPARISON
258244 }
259245 }
260246}
247+
248+ function toConfigDto ( compareValues , compareExact , ignoreJsonKeys , ignoreJsonPaths , allValuePaths ) {
249+ return {
250+ compareValues,
251+ compareExact,
252+ ignoreJsonKeys,
253+ ignoreJsonPaths,
254+ allValuePaths
255+ }
256+ }
0 commit comments