Advertisement
informaticage

Reconstruct from object

Mar 25th, 2021
1,250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // iterariamo sulle [blockKey, values(chapter,items)]
  2. // se la value è un oggetto
  3. // e la value.cmsBlockUID === key
  4.  
  5. const isBuffer = function (obj) {
  6.   return (
  7.     obj &&
  8.     obj.constructor &&
  9.     typeof obj.constructor.isBuffer === "function" &&
  10.     obj.constructor.isBuffer(obj)
  11.   );
  12. };
  13.  
  14. const keyIdentity = function (key) {
  15.   return key;
  16. };
  17.  
  18. const flatten = function (target, opts) {
  19.   opts = opts || {};
  20.  
  21.   const delimiter = opts.delimiter || ".";
  22.   const maxDepth = opts.maxDepth;
  23.   const transformKey = opts.transformKey || keyIdentity;
  24.   const output = {};
  25.  
  26.   function step(object, prev, currentDepth) {
  27.     currentDepth = currentDepth || 1;
  28.     Object.keys(object).forEach(function (key) {
  29.       const value = object[key];
  30.       const isarray = opts.safe && Array.isArray(value);
  31.       const type = Object.prototype.toString.call(value);
  32.       const isbuffer =
  33.         value &&
  34.         value.constructor &&
  35.         typeof value.constructor.isBuffer === "function" &&
  36.         value.constructor.isBuffer(value);
  37.       const isobject = type === "[object Object]" || type === "[object Array]";
  38.  
  39.       const newKey = prev
  40.         ? prev + delimiter + transformKey(key)
  41.         : transformKey(key);
  42.  
  43.       if (
  44.         !isarray &&
  45.         !isbuffer &&
  46.         isobject &&
  47.         Object.keys(value).length &&
  48.         (!opts.maxDepth || currentDepth < maxDepth)
  49.       ) {
  50.         return step(value, newKey, currentDepth + 1);
  51.       }
  52.  
  53.       output[newKey] = value;
  54.     });
  55.   }
  56.  
  57.   step(target);
  58.  
  59.   return output;
  60. };
  61.  
  62. const unflatten = function (target, opts) {
  63.   opts = opts || {};
  64.  
  65.   const delimiter = opts.delimiter || ".";
  66.   const overwrite = opts.overwrite || false;
  67.   const transformKey = opts.transformKey || keyIdentity;
  68.   const result = {};
  69.  
  70.   const isbuffer = isBuffer(target);
  71.   if (
  72.     isbuffer ||
  73.     Object.prototype.toString.call(target) !== "[object Object]"
  74.   ) {
  75.     return target;
  76.   }
  77.  
  78.   // safely ensure that the key is
  79.   // an integer.
  80.   function getkey(key) {
  81.     const parsedKey = Number(key);
  82.  
  83.     return isNaN(parsedKey) || key.indexOf(".") !== -1 || opts.object
  84.       ? key
  85.       : parsedKey;
  86.   }
  87.  
  88.   function addKeys(keyPrefix, recipient, target) {
  89.     return Object.keys(target).reduce(function (result, key) {
  90.       result[keyPrefix + delimiter + key] = target[key];
  91.  
  92.       return result;
  93.     }, recipient);
  94.   }
  95.  
  96.   function isEmpty(val) {
  97.     const type = Object.prototype.toString.call(val);
  98.     const isArray = type === "[object Array]";
  99.     const isObject = type === "[object Object]";
  100.  
  101.     if (!val) {
  102.       return true;
  103.     } else if (isArray) {
  104.       return !val.length;
  105.     } else if (isObject) {
  106.       return !Object.keys(val).length;
  107.     }
  108.   }
  109.  
  110.   target = Object.keys(target).reduce(function (result, key) {
  111.     const type = Object.prototype.toString.call(target[key]);
  112.     const isObject = type === "[object Object]" || type === "[object Array]";
  113.     if (!isObject || isEmpty(target[key])) {
  114.       result[key] = target[key];
  115.       return result;
  116.     } else {
  117.       return addKeys(key, result, flatten(target[key], opts));
  118.     }
  119.   }, {});
  120.  
  121.   Object.keys(target).forEach(function (key) {
  122.     const split = key.split(delimiter).map(transformKey);
  123.     let key1 = getkey(split.shift());
  124.     let key2 = getkey(split[0]);
  125.     let recipient = result;
  126.  
  127.     while (key2 !== undefined) {
  128.       if (key1 === "__proto__") {
  129.         return;
  130.       }
  131.  
  132.       const type = Object.prototype.toString.call(recipient[key1]);
  133.       const isobject = type === "[object Object]" || type === "[object Array]";
  134.  
  135.       // do not write over falsey, non-undefined values if overwrite is false
  136.       if (!overwrite && !isobject && typeof recipient[key1] !== "undefined") {
  137.         return;
  138.       }
  139.  
  140.       if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
  141.         recipient[key1] = typeof key2 === "number" && !opts.object ? [] : {};
  142.       }
  143.  
  144.       recipient = recipient[key1];
  145.       if (split.length > 0) {
  146.         key1 = getkey(split.shift());
  147.         key2 = getkey(split[0]);
  148.       }
  149.     }
  150.  
  151.     // unflatten again for 'messy objects'
  152.     recipient[key1] = unflatten(target[key], opts);
  153.   });
  154.  
  155.   return result;
  156. };
  157.  
  158. const flatDiff = function (obj1, obj2) {
  159.   const obj1Keys = Object.keys(obj1);
  160.   const obj2Keys = Object.keys(obj2);
  161.  
  162.   const updated = new Set();
  163.   const added = new Set();
  164.   const removed = new Set();
  165.  
  166.   for (const key of obj1Keys) {
  167.     if (Array.isArray(obj1[key]) || Array.isArray(obj1[key])) {
  168.       continue;
  169.     }
  170.  
  171.     if (obj1[key] && obj2[key] === undefined) {
  172.       removed.add(key);
  173.       continue;
  174.     }
  175.  
  176.     if (obj1[key] === undefined && obj2[key]) {
  177.       added.add(key);
  178.       continue;
  179.     }
  180.  
  181.     if (obj1[key] !== obj2[key]) {
  182.       updated.add(key);
  183.     }
  184.   }
  185.  
  186.   for (const key of obj2Keys) {
  187.     if (Array.isArray(obj1[key]) || Array.isArray(obj1[key])) {
  188.       continue;
  189.     }
  190.  
  191.     if (obj1[key] && obj2[key] === undefined) {
  192.       removed.add(key);
  193.       continue;
  194.     }
  195.  
  196.     if (obj1[key] === undefined && obj2[key]) {
  197.       added.add(key);
  198.       continue;
  199.     }
  200.  
  201.     if (obj1[key] !== obj2[key]) {
  202.       updated.add(key);
  203.     }
  204.   }
  205.  
  206.   return {
  207.     added: [...added],
  208.     removed: [...removed],
  209.     updated: [...updated],
  210.   };
  211. };
  212.  
  213. const objDiff = function (obj1, obj2) {
  214.   return flatDiff(flatten(obj1), flatten(obj2));
  215. };
  216.  
  217. const unmapContentBlocksByCmsBlockUID = function (cmsMappedBlocks) {
  218.   const result = Object.values(cmsMappedBlocks);
  219.  
  220.   for (const [key, value] of Object.entries(result)) {
  221.     console.log(value);
  222.     if (value && typeof value === "object") {
  223.       if (value.cmsBlockUID) {
  224.         cmsMappedBlocks[
  225.           value.cmsCustomBlockPosition
  226.         ] = unmapContentBlocksByCmsBlockUID(value);
  227.       } else {
  228.         cmsMappedBlocks[key] = unmapContentBlocksByCmsBlockUID(value);
  229.       }
  230.     }
  231.   }
  232.  
  233.   return cmsMappedBlocks;
  234. };
  235.  
  236. const unmapContentBlocksByCmsBlockUID2 = function (cmsMappedBlocks) {
  237.   const result = Object.values(cmsMappedBlocks);
  238.  
  239.   result.forEach((item) => {
  240.     if (item.cmsBlockUID) {
  241.       item[item.cmsCustomBlockPosition] = Object.values(item);
  242.     } else {
  243.       if (item && typeof item === "object") {
  244.         item = unmapContentBlocksByCmsBlockUID2(item);
  245.       }
  246.     }
  247.   });
  248.  
  249.   return result;
  250. };
  251.  
  252. const dfsVisitObject = function (obj) {
  253.   if (obj)
  254.     Object.keys(obj).forEach((key) => {
  255.       if (obj[key] && typeof obj[key] === "object") {
  256.         if (obj[key].cmsBlockUID) {
  257.           obj[obj[key].cmsCustomBlockPosition] = obj[key];
  258.           dfsVisitObject(obj[key]);
  259.  
  260.           delete obj[key];
  261.         } else {
  262.           dfsVisitObject(obj[key]);
  263.         }
  264.       }
  265.     });
  266. };
  267.  
  268. let test = {
  269.   "09ea55b0-14cf-4e02-bf68-33750991f571": {
  270.     type: "BoxGridRow",
  271.     cmsBlockUID: "09ea55b0-14cf-4e02-bf68-33750991f571",
  272.     cmsBlockOwner: "hq",
  273.     isLocked: false,
  274.     isPinned: false,
  275.     items: {
  276.       "5846ac58-9c8f-4094-8ff8-5a80295dbda2": {
  277.         cmsBlockUID: "5846ac58-9c8f-4094-8ff8-5a80295dbda2",
  278.         cmsBlockOwner: "hq",
  279.         isLocked: true,
  280.         isPinned: false,
  281.         preTitle: "test-new-validate",
  282.         title: "test-new-validate",
  283.         subTitle: "test-new-validate",
  284.         description: "test-new-validate",
  285.         width: "33%",
  286.         media: {
  287.           landscapeBig: {
  288.             id: "603794de41c6d945e01235d3",
  289.           },
  290.           portraitMedium: {
  291.             id: "603794de41c6d945e01235d3",
  292.           },
  293.         },
  294.         thronPlaylistId: "603794de41c6d945e01235d3",
  295.         cta: null,
  296.         ctaType: "standard",
  297.         filterValues: {
  298.           filter1: "",
  299.           filter2: "",
  300.           filter3: "",
  301.         },
  302.         cmsCustomBlockPosition: 0,
  303.       },
  304.       "8156c2e3-8657-42e3-abb7-e98c8cf38280": {
  305.         cmsBlockUID: "8156c2e3-8657-42e3-abb7-e98c8cf38280",
  306.         cmsBlockOwner: "hq",
  307.         isLocked: false,
  308.         isPinned: false,
  309.         preTitle: "test-new-validate",
  310.         title: "test-new-validate",
  311.         subTitle: "test-new-validate",
  312.         description: "test-new-validate",
  313.         width: "33%",
  314.         media: {
  315.           landscapeBig: {
  316.             id: "603794de41c6d945e01235d3",
  317.           },
  318.           portraitMedium: {
  319.             id: "603794de41c6d945e01235d3",
  320.           },
  321.         },
  322.         thronPlaylistId: "603794de41c6d945e01235d3",
  323.         cta: null,
  324.         ctaType: "standard",
  325.         filterValues: {
  326.           filter1: "",
  327.           filter2: "",
  328.           filter3: "",
  329.         },
  330.         cmsCustomBlockPosition: 1,
  331.       },
  332.     },
  333.     cmsCustomBlockPosition: 0,
  334.   },
  335. };
  336.  
  337. dfsVisitObject(test);
  338. test = unflatten(flatten(test));
  339.  
  340. console.log(JSON.stringify(Object.values(test), null, 2));
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement