RubyLouvre/anu

View on GitHub
lib/shallowCompare.js

Summary

Maintainability
B
6 hrs
Test Coverage
/**
 * shallowCompares补丁 by 司徒正美 Copyright 2018-05-25
 * IE9+
 */

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    (global.shallowCompare = factory());
}(this, (function () {

var hasOwnProperty = Object.prototype.hasOwnProperty;



var hasSymbol = typeof Symbol === "function" && Symbol["for"];
var REACT_ELEMENT_TYPE = hasSymbol ? Symbol["for"]("react.element") : 0xeac7;













var __type = Object.prototype.toString;








var numberMap = {
    "[object Boolean]": 2,
    "[object Number]": 3,
    "[object String]": 4,
    "[object Function]": 5,
    "[object Symbol]": 6,
    "[object Array]": 7
};
function typeNumber(data) {
    if (data === null) {
        return 1;
    }
    if (data === void 666) {
        return 0;
    }
    var a = numberMap[__type.call(data)];
    return a || 8;
}

function shallowEqual(objA, objB) {
    if (Object.is(objA, objB)) {
        return true;
    }
    if (typeNumber(objA) < 7 || typeNumber(objB) < 7) {
        return false;
    }
    var keysA = Object.keys(objA);
    var keysB = Object.keys(objB);
    if (keysA.length !== keysB.length) {
        return false;
    }
    for (var i = 0; i < keysA.length; i++) {
        if (!hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
            return false;
        }
    }
    return true;
}

function shallowCompare(instance, nextProps, nextState) {
    var a = shallowEqual(instance.props, nextProps);
    var b = shallowEqual(instance.state, nextState);
    return !a || !b;
}

return shallowCompare;

})));