source: node_modules/recharts/lib/util/propsAreEqual.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[a762898]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.propsAreEqual = propsAreEqual;
7var _reactRedux = require("react-redux");
8var propsToShallowCompare = new Set(['axisLine', 'tickLine', 'activeBar', 'activeDot', 'activeLabel', 'activeShape', 'allowEscapeViewBox', 'background', 'cursor', 'dot', 'label', 'line', 'margin', 'padding', 'position', 'shape', 'style', 'tick', 'wrapperStyle',
9// radius can be an array of 4 numbers, easy to compare shallowly
10'radius']);
11
12/**
13 * When comparing two values, returns true if they are the same value or
14 * are both NaN.
15 *
16 * If we used just a simple triple equals, we would get false negatives for two NaNs
17 * which could cause extra re-renders so let's have this instead.
18 *
19 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness#same-value-zero_equality
20 *
21 * @param x first value to compare
22 * @param y second value to compare
23 * return true if the same, false if different
24 */
25function sameValueZero(x, y) {
26 if (x == null && y == null) {
27 /*
28 * treat null and undefined as equal. Internally in Recharts we make no difference between these two
29 * so there is no need to re-render.
30 */
31 return true;
32 }
33 if (typeof x === 'number' && typeof y === 'number') {
34 // x and y are equal (this is true for -0 and 0) or they are both NaN
35 // eslint-disable-next-line no-self-compare
36 return x === y || x !== x && y !== y;
37 }
38 return x === y;
39}
40
41/**
42 * So usually React would compare only the first level of props using Object.is.
43 * However, in our case many props are objects or arrays, and our own docs recommend to do that!
44 * Therefore, we need a custom comparison function that does a shallow comparison of each prop value.
45 *
46 * Because charts can and do receive large props (typically the data array),
47 * we only limit this to a subset of known props that are likely to be objects/arrays.
48 *
49 * @param prevProps
50 * @param nextProps
51 */
52function propsAreEqual(prevProps, nextProps) {
53 var allKeys = new Set([...Object.keys(prevProps), ...Object.keys(nextProps)]);
54 for (var key of allKeys) {
55 /*
56 * If a key is on a special allowlist, go one level deeper
57 * and do a shallow comparison of the values.
58 */
59 if (propsToShallowCompare.has(key)) {
60 if (prevProps[key] == null && nextProps[key] == null) {
61 /*
62 * treat null and undefined as equal. Internally in Recharts we make no difference between these two
63 * so there is no need to re-render.
64 */
65 continue;
66 }
67 if (!(0, _reactRedux.shallowEqual)(prevProps[key], nextProps[key])) {
68 return false;
69 }
70 /*
71 * Otherwise do a simple same-value comparison (with NaN support).
72 */
73 } else if (!sameValueZero(prevProps[key], nextProps[key])) {
74 return false;
75 }
76 }
77 return true;
78}
Note: See TracBrowser for help on using the repository browser.