File size: 1,119 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
import { includes, isEqual } from 'lodash';

export default ( options = {} ) => {
	const { ignore, deep, shallow } = options;
	return ( prevProps, nextProps ) => {
		for ( const propName in prevProps ) {
			// Skip ignored properties
			if ( ignore && includes( ignore, propName ) ) {
				continue;
			}

			// Some properties want to be compared deeply
			if ( deep && includes( deep, propName ) ) {
				if ( ! isEqual( prevProps[ propName ], nextProps[ propName ] ) ) {
					return false;
				}

				continue;
			}

			// Compare all other props (or a selected subset) shallowly
			if ( ! shallow || includes( shallow, propName ) ) {
				if ( prevProps[ propName ] !== nextProps[ propName ] ) {
					return false;
				}
			}
		}

		// Find properties that are only in `nextProps` and are not ignored.
		// Presence of such properties means that the objects are not equal.
		for ( const propName in nextProps ) {
			if (
				! ( propName in prevProps ) &&
				! ( ignore && includes( ignore, propName ) ) &&
				! ( shallow && ! includes( shallow, propName ) )
			) {
				return false;
			}
		}

		return true;
	};
};