1+ import { flatten } from "flat" ;
2+
13function isRegex ( o ) {
24 return o instanceof RegExp ;
35}
@@ -21,4 +23,59 @@ function isUndefined(a) {
2123 return typeof a === "undefined" ;
2224}
2325
24- export { isRegex , isFunction , isPlainObject , isUndefined } ;
26+ /**
27+ * According to Webpack docs, a "test" should be the following:
28+ *
29+ * - A string
30+ * - A RegExp
31+ * - A function
32+ * - An array of conditions (may be nested)
33+ * - An object of conditions (may be nested)
34+ *
35+ * https://webpack.js.org/configuration/module/#condition
36+ */
37+ function isSameCondition ( a , b ) {
38+ if ( ! a || ! b ) {
39+ return a === b ;
40+ }
41+ if (
42+ typeof a === 'string' || typeof b === 'string' ||
43+ isRegex ( a ) || isRegex ( b ) ||
44+ isFunction ( a ) || isFunction ( b )
45+ ) {
46+ return a . toString ( ) === b . toString ( ) ;
47+ }
48+
49+ const entriesA = Object . entries ( flatten < any , object > ( a ) ) ;
50+ const entriesB = Object . entries ( flatten < any , object > ( b ) ) ;
51+ if ( entriesA . length !== entriesB . length ) {
52+ return false ;
53+ }
54+
55+ for ( let i = 0 ; i < entriesA . length ; i ++ ) {
56+ entriesA [ i ] [ 0 ] = entriesA [ i ] [ 0 ] . replace ( / \b \d + \b / g, "[]" ) ;
57+ entriesB [ i ] [ 0 ] = entriesB [ i ] [ 0 ] . replace ( / \b \d + \b / g, "[]" ) ;
58+ }
59+
60+ function cmp ( [ k1 , v1 ] , [ k2 , v2 ] ) {
61+ if ( k1 < k2 ) return - 1 ;
62+ if ( k1 > k2 ) return 1 ;
63+ if ( v1 < v2 ) return - 1 ;
64+ if ( v1 > v2 ) return 1 ;
65+ return 0 ;
66+ } ;
67+ entriesA . sort ( cmp ) ;
68+ entriesB . sort ( cmp ) ;
69+
70+ if ( entriesA . length !== entriesB . length ) {
71+ return false ;
72+ }
73+ for ( let i = 0 ; i < entriesA . length ; i ++ ) {
74+ if ( entriesA [ i ] [ 0 ] !== entriesB [ i ] [ 0 ] || entriesA [ i ] [ 1 ] ?. toString ( ) !== entriesB [ i ] [ 1 ] ?. toString ( ) ) {
75+ return false ;
76+ }
77+ }
78+ return true ;
79+ }
80+
81+ export { isRegex , isFunction , isPlainObject , isUndefined , isSameCondition } ;
0 commit comments