UNPKG

react-form-controlled

Version:

Intuitive react forms for building powerful applications

53 lines (43 loc) 1.32 kB
const { hasOwnProperty } = Object.prototype; /** * inlined Object.is polyfill to avoid requiring consumers ship their own * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is */ function is(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7-10 // Steps 6.b-6.e: +0 != -0 return x !== 0 || 1 / x === 1 / y; } return x !== x && y !== y; } /** * Performs equality by iterating through keys on an object and returning false * when any key has values which are not strictly equal between the arguments. * Returns true when the values of all keys are strictly equal. */ export default function shallowEqual(objA, objB, ignore = []) { if (is(objA, objB)) { return true; } if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { return false; } const keysA = Object.keys(objA); const keysB = Object.keys(objB); if (keysA.length !== keysB.length) { return false; } // Test for A's keys different from B. for (let i = 0; i < keysA.length; i++) { const propName = keysA[i]; if (ignore.indexOf(propName) !== -1) { continue; } if (!hasOwnProperty.call(objB, propName) || !is(objA[propName], objB[propName])) { return false; } } return true; }