alm
Version:
The best IDE for TypeScript
50 lines (41 loc) • 1.21 kB
text/typescript
/**
* Mutablity helpers
*/
/**
* Shallow Equal and Shallow Compare from
* https://github.com/gaearon/react-pure-render/blob/master/src/shallowEqual.js
*
*/
export function shouldComponentUpdate(nextProps:any, nextState:any) {
return !shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState);
}
/**
* Performs equality by iterating through keys on an object and returning
* false when any key has values which are not strictly equal between
* objA and objB. Returns true when the values of all keys are strictly equal.
*
* @return {boolean}
*/
export function shallowEqual(objA, objB) {
if (objA === objB) {
return true;
}
if (typeof objA !== 'object' || objA === null ||
typeof objB !== 'object' || objB === null) {
return false;
}
var keysA = Object.keys(objA);
var keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
// Test for A's keys different from B.
var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
for (var i = 0; i < keysA.length; i++) {
if (!bHasOwnProperty(keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) {
return false;
}
}
return true;
}