Skip to content

Commit

Permalink
inlining react-fast-compare to get around rollup configuration issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gwyneplaine committed Jul 23, 2018
1 parent 24f85f8 commit f85fe6d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -16,7 +16,6 @@
"memoize-one": "^4.0.0",
"prop-types": "^15.6.0",
"raf": "^3.4.0",
"react-fast-compare": "^2.0.1",
"react-input-autosize": "^2.2.1",
"react-transition-group": "^2.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Select.js
Expand Up @@ -3,7 +3,7 @@
import React, { Component, type ElementRef, type Node } from 'react';

import memoizeOne from 'memoize-one';
import isEqual from 'react-fast-compare';
import isEqual from './internal/react-fast-compare';

import { createFilter } from './filters';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/animated/index.js
@@ -1,6 +1,6 @@
// @flow
import memoize from 'memoize-one';
import isEqual from 'react-fast-compare';
import isEqual from '../internal/react-fast-compare';
import { type SelectComponents, defaultComponents } from '../components/index';
import { default as AnimatedInput } from './Input';
import { default as AnimatedMultiValue } from './MultiValue';
Expand Down
90 changes: 90 additions & 0 deletions src/internal/react-fast-compare.js
@@ -0,0 +1,90 @@
'use strict';

var isArray = Array.isArray;
var keyList = Object.keys;
var hasProp = Object.prototype.hasOwnProperty;

function equal(a, b) {
// fast-deep-equal index.js 2.0.1
if (a === b) return true;

if (a && b && typeof a == 'object' && typeof b == 'object') {
var arrA = isArray(a)
, arrB = isArray(b)
, i
, length
, key;

if (arrA && arrB) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;) {
if (!equal(a[i], b[i])) return false;
}
return true;
}

if (arrA != arrB) return false;

var dateA = a instanceof Date
, dateB = b instanceof Date;
if (dateA != dateB) return false;
if (dateA && dateB) return a.getTime() == b.getTime();

var regexpA = a instanceof RegExp
, regexpB = b instanceof RegExp;
if (regexpA != regexpB) return false;
if (regexpA && regexpB) return a.toString() == b.toString();

var keys = keyList(a);
length = keys.length;

if (length !== keyList(b).length) {
return false;
}

for (i = length; i-- !== 0;) {
if (!hasProp.call(b, keys[i])) return false;
}
// end fast-deep-equal

// Custom handling for React
for (i = length; i-- !== 0;) {
key = keys[i];
if (key === '_owner' && a.$$typeof) {
// React-specific: avoid traversing React elements' _owner.
// _owner contains circular references
// and is not needed when comparing the actual elements (and not their owners)
// .$$typeof and ._store on just reasonable markers of a react element
continue;
} else {
// all other properties should be traversed as usual
if (!equal(a[key], b[key])) return false;
}
}

// fast-deep-equal index.js 2.0.1
return true;
}

return a!==a && b!==b;
}
// end fast-deep-equal

export default function exportedEqual(a, b) {
try {
return equal(a, b);
} catch (error) {
if (error.message && error.message.match(/stack|recursion/i)) {
// warn on circular references, don't crash
// browsers give this different errors name and messages:
// chrome/safari: "RangeError", "Maximum call stack size exceeded"
// firefox: "InternalError", too much recursion"
// edge: "Error", "Out of stack space"
console.warn('Warning: react-fast-compare does not handle circular references.', error.name, error.message);
return false;
}
// some other error. we should definitely know about these
throw error;
}
};

0 comments on commit f85fe6d

Please sign in to comment.