informaticage

Check deep equal objects based on isLocked

Jan 26th, 2021
837
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const deepEqualForSelectedProperties = (obj1, obj2, propertiesToCheck) => {
  2.     for(const key of propertiesToCheck) {
  3.         // O compariamo oggetti
  4.         if(key === 'content' && !deepEqualForContent(obj1['content']['blocks'][0], obj2['content']['blocks'][0])) {
  5.             return false;
  6.         }
  7.  
  8.         // O compariamo primitivi
  9.         if(obj1[key] !== obj2[key]) {
  10.             return false;
  11.         }
  12.     }
  13.  
  14.     return true;
  15. };
  16.  
  17. const deepEqualForContent = (obj1, obj2) => {
  18.     const [keys1, keys2] = [Object.keys(obj1), Object.keys(obj2)];
  19.  
  20.     // Do not replace with !obj1.isLocked
  21.     if(obj1.isLocked === false) {
  22.         console.log("quitto qua");
  23.         return true;
  24.     }
  25.  
  26.     for(const key of keys1) {
  27.         if(isObject(obj1[key])) {
  28.             if(!isObject(obj2[key])) {
  29.                 return false;
  30.             }
  31.             // Both of them are objects
  32.             return deepEqualForContent(obj1[key], obj2[key]);
  33.         }
  34.         // Primitives
  35.         if(obj1[key] !== obj2[key]) {
  36.             return false;
  37.         }
  38.     }
  39.  
  40.     return true;
  41. }
  42.  
  43. const isObject = (item) => item && typeof item === 'object';
RAW Paste Data