Skip to content

Commit

Permalink
cancel rewrite findchangeKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
jparez committed Jul 26, 2024
1 parent e099e07 commit 62e7c79
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,39 @@ const createStore = (instance, reducer) => {
});
};

const arraysAreEqual = (firstArray, secondArray) => {
if (firstArray.length !== secondArray.length) {
return true;
}
for (let i = 0; i < firstArray.length; i++) {
if (firstArray[i] !== secondArray[i]) {
return true;
}
}
return false;
};
const findChangedKeys = (newState, oldState, path = []) => {
let changedKeys = [];

const isObject = (obj) => obj && typeof obj === 'object';

for (const key in newState) {
const fullPath = [...path, key].join('.');

if (!Object.prototype.hasOwnProperty.call(oldState, key)) {
changedKeys.push(fullPath);
} else if (isObject(newState[key]) && isObject(oldState[key])) {
if (
Array.isArray(newState[key]) &&
Array.isArray(oldState[key]) &&
!arraysAreEqual(newState[key], oldState[key])
) {
changedKeys.push(fullPath);
if (Array.isArray(newState[key]) && Array.isArray(oldState[key])) {
if (newState[key].length !== oldState[key].length) {
changedKeys.push(fullPath);
} else {
let arrayHasChanged = false;
for (let i = 0; i < newState[key].length; i++) {
if (newState[key][i] !== oldState[key][i]) {
arrayHasChanged = true;
}
}
if (arrayHasChanged) {
changedKeys.push(fullPath);
}
}
} else {
changedKeys = changedKeys.concat(findChangedKeys(newState[key], oldState[key], [...path, key]));
}
} else if (newState[key] !== oldState[key]) {
changedKeys.push(fullPath);
}
}

return changedKeys;
};

Expand Down

0 comments on commit 62e7c79

Please sign in to comment.