informaticage

Final revision on deep equal with locked test

Jan 26th, 2021 (edited)
1,154
0
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 (content)
  4.       if(key === 'content') {
  5.          if(obj1['content']['blocks']) {
  6.             for(let i = 0; i < obj1['content']['blocks'].length; i++) {
  7.                console.log(obj1['content']['blocks']);
  8.                console.log(obj2['content']['blocks']);
  9.                if(!deepEqualForContent(obj1['content']['blocks'][i], obj2['content']['blocks'][i])) {
  10.                   console.log('Failing on ', obj1['content']['blocks'][i], 'vs', obj2['content']['blocks'][i]);
  11.                   return false;
  12.                }
  13.             }
  14.          } else {
  15.             return false;
  16.          }
  17.       }
  18.       // O compariamo primitivi (!= content)
  19.       console.log("Checking l1", key, obj1[key], 'vs', obj2[key])
  20.       if(key !== 'content' && obj1[key] !== obj2[key]) {
  21.          console.log('missmatch esterno', key, obj1[key], obj2[key]);
  22.          return false;
  23.       }
  24.    }
  25.  
  26.    return true;
  27. };
  28.  
  29. const deepEqualForContent = (obj1, obj2) => {
  30.    const [keys1, keys2] = [Object.keys(obj1), Object.keys(obj2)];
  31.  
  32.    // Do not replace with !obj1.isLocked
  33.    let checkThisLevel = true;
  34.    if(obj1.isLocked === false && obj2.isLocked === false) {
  35.       console.log('Skipping the below level', obj1, obj2)
  36.       checkThisLevel = false;
  37.       // return true;
  38.    }
  39.  
  40.    for(const key of keys1) {
  41.       if(isObject(obj1[key])) {
  42.          if(!isObject(obj2[key])) {
  43.                return false;
  44.          }
  45.          // Both of them are objects
  46.          console.log('CONSIDERO OGGETTO: ', key, obj1[key])
  47.          if(!deepEqualForContent(obj1[key], obj2[key])) {
  48.             return false;
  49.          }
  50.       } else {
  51.          // Primitives
  52.          console.log((checkThisLevel ? 'checking' : 'skipping'), key);
  53.          if(checkThisLevel) {
  54.             console.log("Checking", key, obj1[key], 'vs', obj2[key])
  55.             if(obj1[key] !== obj2[key]) {
  56.                return false;
  57.             }
  58.          }
  59.       }
  60.    }
  61.  
  62.    return true;
  63. }
  64.  
  65. const isObject = (item) => item && typeof item === 'object';
  66.  
  67. const t = {
  68.   isCreatedByHq: true,
  69.   sitemapId: "Home",
  70.   parentId: null,
  71.   title: "Home",
  72.   internalTitle: "Home",
  73.   locale: "it",
  74.   content: {
  75.     blocks: [
  76.       {
  77.         type: "DWSCoverCarousel",
  78.         isLocked: true,
  79.         isPinned: true,
  80.         scrollpointer: false,
  81.         items: [
  82.           {
  83.             type: "DwsCover",
  84.             isLocked: true,
  85.             isPinned: false,
  86.             owner: "hidden",
  87.             palette: "dark",
  88.             gradient: false,
  89.             title: "Titolo Slide 1",
  90.             preTitle: "Pre Titolo Slide 1",
  91.             chapter: {
  92.               name: "Chapter slide 1",
  93.               nav: false,
  94.             },
  95.             scrollIndicator: false,
  96.             link: {
  97.               content: "",
  98.               title: "",
  99.               url: "",
  100.               target: "_blank",
  101.             },
  102.             cta2: {
  103.               content: "Testfff",
  104.               url: "http://localhost:4200/back-office/cms/dws-pages/new",
  105.               target: "_blank",
  106.             },
  107.             cta3: null,
  108.             background: null,
  109.           },
  110.           {
  111.             type: "DwsCover",
  112.             isLocked: true,
  113.             isPinned: false,
  114.             owner: "hidden",
  115.             palette: "",
  116.             gradient: false,
  117.             title: "Titolo Slide 2",
  118.             preTitle: "Pre Titolo Slide 2",
  119.             chapter: {
  120.               name: "",
  121.               nav: false,
  122.             },
  123.             scrollIndicator: false,
  124.             link: {
  125.               content: "",
  126.               title: "",
  127.               url: "",
  128.               target: "_blank",
  129.             },
  130.             cta2: {
  131.               content: "Content cta2",
  132.               url: "",
  133.               target: "_blank",
  134.             },
  135.             cta3: null,
  136.             background: null,
  137.           },
  138.         ],
  139.       },
  140.       {
  141.         type: "DWSCoverCarousel",
  142.         isLocked: true,
  143.         isPinned: true,
  144.         scrollpointer: false,
  145.         items: [
  146.           {
  147.             type: "DwsCover",
  148.             isLocked: false,
  149.             isPinned: false,
  150.             owner: "hidden",
  151.             palette: "dark",
  152.             gradient: false,
  153.             title: "Titolo Slide 2",
  154.             preTitle: "Pre Titolo Slide 1",
  155.             chapter: {
  156.               name: "Chapter slide 1",
  157.               nav: false,
  158.             },
  159.             scrollIndicator: false,
  160.             link: {
  161.               content: "",
  162.               title: "",
  163.               url: "",
  164.               target: "_blank",
  165.             },
  166.             cta2: {
  167.               content: "Testfff",
  168.               url: "http://localhost:4200/back-office/cms/dws-pages/new",
  169.               target: "_blank",
  170.             },
  171.             cta3: null,
  172.             background: null,
  173.           },
  174.           {
  175.             type: "DwsCover",
  176.             isLocked: true,
  177.             isPinned: false,
  178.             owner: "hidden",
  179.             palette: "",
  180.             gradient: false,
  181.             title: "Titolo Slide 2",
  182.             preTitle: "Pre Titolo Slide 2",
  183.             chapter: {
  184.               name: "",
  185.               nav: false,
  186.             },
  187.             scrollIndicator: false,
  188.             link: {
  189.               content: "",
  190.               title: "",
  191.               url: "",
  192.               target: "_blank",
  193.             },
  194.             cta2: {
  195.               content: "Content cta2",
  196.               url: "",
  197.               target: "_blank",
  198.             },
  199.             cta3: null,
  200.             background: null,
  201.           },
  202.         ],
  203.       },
  204.     ],
  205.   },
  206.   slug: "home",
  207.   status: "draft",
  208.   id: "6010301d70155b5998348291",
  209.   original: "600ffc5f70155b59983480ef",
  210.   createdAt: "2021-01-26T15:07:09.244Z",
  211.   updatedAt: null,
  212.   revision: 0,
  213.   active: false,
  214. };
  215.  
  216. const t2 = {
  217.    isCreatedByHq: true,
  218.    sitemapId: "Home",
  219.    parentId: null,
  220.    title: "Home",
  221.    internalTitle: "Home",
  222.    locale: "it",
  223.    content: {
  224.       blocks: [
  225.          {
  226.            type: "DWSCoverCarousel",
  227.            isLocked: true,
  228.            isPinned: true,
  229.            scrollpointer: false,
  230.            items: [
  231.              {
  232.                type: "DwsCover",
  233.                isLocked: true,
  234.                isPinned: false,
  235.                owner: "hidden",
  236.                palette: "dark",
  237.                gradient: false,
  238.                title: "Titolo Slide 1",
  239.                preTitle: "Pre Titolo Slide 1",
  240.                chapter: {
  241.                  name: "Chapter slide 1",
  242.                  nav: false,
  243.                },
  244.                scrollIndicator: false,
  245.                link: {
  246.                  content: "",
  247.                  title: "",
  248.                  url: "",
  249.                  target: "_blank",
  250.                },
  251.                cta2: {
  252.                  content: "Testfff",
  253.                  url: "http://localhost:4200/back-office/cms/dws-pages/new",
  254.                  target: "_blank",
  255.                },
  256.                cta3: null,
  257.                background: null,
  258.              },
  259.              {
  260.                type: "DwsCover",
  261.                isLocked: true,
  262.                isPinned: false,
  263.                owner: "hidden",
  264.                palette: "",
  265.                gradient: false,
  266.                title: "Titolo Slide 2",
  267.                preTitle: "Pre Titolo Slide 2",
  268.                chapter: {
  269.                  name: "",
  270.                  nav: false,
  271.                },
  272.                scrollIndicator: false,
  273.                link: {
  274.                  content: "",
  275.                  title: "",
  276.                  url: "",
  277.                  target: "_blank",
  278.                },
  279.                cta2: {
  280.                  content: "Content cta2",
  281.                  url: "",
  282.                  target: "_blank",
  283.                },
  284.                cta3: null,
  285.                background: null,
  286.              },
  287.            ],
  288.          },
  289.          {
  290.            type: "DWSCoverCarousel",
  291.            isLocked: true,
  292.            isPinned: true,
  293.            scrollpointer: false,
  294.            items: [
  295.              {
  296.                type: "DwsCover",
  297.                isLocked: false,
  298.                isPinned: false,
  299.                owner: "hidden",
  300.                palette: "dark",
  301.                gradient: false,
  302.                title: "Titolo Slide 1",
  303.                preTitle: "Pre Titolo Slide 1",
  304.                chapter: {
  305.                  name: "Chapter slide 1",
  306.                  nav: false,
  307.                },
  308.                scrollIndicator: false,
  309.                link: {
  310.                  content: "",
  311.                  title: "",
  312.                  url: "",
  313.                  target: "_blank",
  314.                },
  315.                cta2: {
  316.                  content: "Testfff",
  317.                  url: "http://localhost:4200/back-office/cms/dws-pages/new",
  318.                  target: "_blank",
  319.                },
  320.                cta3: null,
  321.                background: null,
  322.              },
  323.              {
  324.                type: "DwsCover",
  325.                isLocked: true,
  326.                isPinned: false,
  327.                owner: "hidden",
  328.                palette: "",
  329.                gradient: false,
  330.                title: "Titolo Slide 2",
  331.                preTitle: "Pre Titolo Slide 2",
  332.                chapter: {
  333.                  name: "",
  334.                  nav: false,
  335.                },
  336.                scrollIndicator: false,
  337.                link: {
  338.                  content: "",
  339.                  title: "",
  340.                  url: "",
  341.                  target: "_blank",
  342.                },
  343.                cta2: {
  344.                  content: "Content cta2",
  345.                  url: "",
  346.                  target: "_blank",
  347.                },
  348.                cta3: null,
  349.                background: null,
  350.              },
  351.            ],
  352.          },
  353.        ],
  354.    },
  355.    slug: "home",
  356.    status: "draft",
  357.    id: "6010301d70155b5998348291",
  358.    original: "600ffc5f70155b59983480ef",
  359.    createdAt: "2021-01-26T15:07:09.244Z",
  360.    updatedAt: null,
  361.    revision: 0,
  362.    active: false,
  363. };
  364.  
  365.  
  366.  
Add Comment
Please, Sign In to add comment