TelegramPWA/src/util/arePropsShallowEqual.ts
2021-08-03 19:02:59 +03:00

45 lines
1.1 KiB
TypeScript

export default function arePropsShallowEqual(currentProps: AnyLiteral, newProps: AnyLiteral) {
if (currentProps === newProps) {
return true;
}
const currentKeys = Object.keys(currentProps);
const currentKeysLength = currentKeys.length;
const newKeysLength = Object.keys(newProps).length;
if (currentKeysLength !== newKeysLength) {
return false;
}
if (currentKeysLength === 0) {
return true;
}
for (let i = 0; i < currentKeysLength; i++) {
const prop = currentKeys[i];
if (currentProps[prop] !== newProps[prop]) {
return false;
}
}
return true;
}
export function getUnequalProps(currentProps: AnyLiteral, newProps: AnyLiteral) {
const currentKeys = Object.keys(currentProps);
const currentKeysLength = currentKeys.length;
const newKeysLength = Object.keys(newProps).length;
if (currentKeysLength !== newKeysLength) {
return ['%LENGTH%'];
}
return currentKeys.reduce((res, prop) => {
if (currentProps[prop] !== newProps[prop]) {
res.push(`${prop}: ${currentProps[prop]} => ${newProps[prop]}`);
}
return res;
}, [] as string[]);
}