33import _ from 'underscore' ;
44
55type OptionsProps = {
6- emptyValues : Array < any > ,
7- ignoreHtml : boolean
6+ /**
7+ * List of values to consider "empty", these values will be treated as equivalent.
8+ */
9+ emptyValues ?: Array < any > ,
10+
11+ /**
12+ * If true, HTML tags will be stripped from string values.
13+ */
14+ ignoreHtml ?: boolean ,
15+
16+ /**
17+ * If true, whitespace will be stripped from string values.
18+ */
19+ ignoreWhitespace ?: boolean ,
20+
21+ /**
22+ * If true, "empty" keys/values will be removed from objects prior to equality check.
23+ */
24+ removeEmptyValues ?: boolean
825} ;
926
1027const EMPTY_VALUES = [
@@ -18,7 +35,8 @@ const EMPTY_VALUES = [
1835const DEFAULT_OPTIONS = {
1936 emptyValues : EMPTY_VALUES ,
2037 ignoreHtml : true ,
21- ignoreWhitespace : true
38+ ignoreWhitespace : true ,
39+ removeEmptyValues : false
2240} ;
2341
2442const HTML_REGEX = / ( < ( [ ^ > ] + ) > ) / gi;
@@ -85,8 +103,16 @@ const isEqual = (a: any, b: any, userOptions: OptionsProps = {}) => {
85103 }
86104
87105 if ( a !== null && typeof a === 'object' && b !== null && typeof b === 'object' ) {
88- const aKeys = _ . keys ( a ) ;
89- const bKeys = _ . keys ( b ) ;
106+ let aObject = a ;
107+ let bObject = b ;
108+
109+ if ( options . removeEmptyValues ) {
110+ aObject = _ . omit ( a , ( value ) => _ . contains ( options . emptyValues , value ) ) ;
111+ bObject = _ . omit ( b , ( value ) => _ . contains ( options . emptyValues , value ) ) ;
112+ }
113+
114+ const aKeys = _ . keys ( aObject ) ;
115+ const bKeys = _ . keys ( bObject ) ;
90116
91117 // If the objects contain different number of keys, return false
92118 if ( aKeys . length !== bKeys . length ) {
@@ -96,8 +122,8 @@ const isEqual = (a: any, b: any, userOptions: OptionsProps = {}) => {
96122 // Recursively check each key for equality
97123 let equal = true ;
98124
99- _ . each ( _ . keys ( a ) , ( key ) => {
100- if ( ! ( _ . has ( b , key ) && isEqual ( a [ key ] , b [ key ] ) ) ) {
125+ _ . each ( _ . keys ( aObject ) , ( key ) => {
126+ if ( ! ( _ . has ( bObject , key ) && isEqual ( aObject [ key ] , bObject [ key ] ) ) ) {
101127 equal = false ;
102128 }
103129 } ) ;
0 commit comments