All files / react_grid_layout_modified/lib fastRGLPropsEqual.js

100% Statements 10/10
100% Branches 2/2
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 45 46 47    2x             2x 2x 2x     2x             4148x               2928x   1220x         2x 122x                  
// @preval
 
require("@babel/register");
 
// Fast way to compare RGL props in shouldComponentUpdate.
// Generates the fastest possible comparison of the type:
// function (a, b) { return a.className === b.className && a.style === b.style && ... }
// This avoids enumerating keys, avoids us keeping our own key list, and can be very easily optimized.
 
const PropTypes = require("prop-types");
const propTypes = require("./ReactGridLayoutPropTypes").default;
const keys = Object.keys(propTypes);
 
// Remove 'children' key as we don't want to compare it
keys.splice(keys.indexOf("children"), 1);
 
// Returns a code string indicating what to do here.
// In most cases we want to do a simple equality comparison,
// but we have some arrays and tuples and objects we want
// to do a shallow comparison on.
function getEqualType(key) {
  if (
    [
      PropTypes.number,
      PropTypes.bool,
      PropTypes.string,
      PropTypes.func
    ].includes(propTypes[key])
  ) {
    return `(a.${key} === b.${key})`;
  }
  return `isEqualImpl(a.${key}, b.${key})`;
}
 
// Exports a function that compares a and b. `isEqualImpl` is a required
// third prop, as we can't otherwise access it.
module.exports = () =>
  eval(`
  function fastRGLPropsEqual(a, b, isEqualImpl) {
    if (a === b) return true;
    return (
      ${keys.map(getEqualType).join(" && ")}
    );
  }
  fastRGLPropsEqual;
`);