Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // iterariamo sulle [blockKey, values(chapter,items)]
- // se la value è un oggetto
- // e la value.cmsBlockUID === key
- const isBuffer = function (obj) {
- return (
- obj &&
- obj.constructor &&
- typeof obj.constructor.isBuffer === "function" &&
- obj.constructor.isBuffer(obj)
- );
- };
- const keyIdentity = function (key) {
- return key;
- };
- const flatten = function (target, opts) {
- opts = opts || {};
- const delimiter = opts.delimiter || ".";
- const maxDepth = opts.maxDepth;
- const transformKey = opts.transformKey || keyIdentity;
- const output = {};
- function step(object, prev, currentDepth) {
- currentDepth = currentDepth || 1;
- Object.keys(object).forEach(function (key) {
- const value = object[key];
- const isarray = opts.safe && Array.isArray(value);
- const type = Object.prototype.toString.call(value);
- const isbuffer =
- value &&
- value.constructor &&
- typeof value.constructor.isBuffer === "function" &&
- value.constructor.isBuffer(value);
- const isobject = type === "[object Object]" || type === "[object Array]";
- const newKey = prev
- ? prev + delimiter + transformKey(key)
- : transformKey(key);
- if (
- !isarray &&
- !isbuffer &&
- isobject &&
- Object.keys(value).length &&
- (!opts.maxDepth || currentDepth < maxDepth)
- ) {
- return step(value, newKey, currentDepth + 1);
- }
- output[newKey] = value;
- });
- }
- step(target);
- return output;
- };
- const unflatten = function (target, opts) {
- opts = opts || {};
- const delimiter = opts.delimiter || ".";
- const overwrite = opts.overwrite || false;
- const transformKey = opts.transformKey || keyIdentity;
- const result = {};
- const isbuffer = isBuffer(target);
- if (
- isbuffer ||
- Object.prototype.toString.call(target) !== "[object Object]"
- ) {
- return target;
- }
- // safely ensure that the key is
- // an integer.
- function getkey(key) {
- const parsedKey = Number(key);
- return isNaN(parsedKey) || key.indexOf(".") !== -1 || opts.object
- ? key
- : parsedKey;
- }
- function addKeys(keyPrefix, recipient, target) {
- return Object.keys(target).reduce(function (result, key) {
- result[keyPrefix + delimiter + key] = target[key];
- return result;
- }, recipient);
- }
- function isEmpty(val) {
- const type = Object.prototype.toString.call(val);
- const isArray = type === "[object Array]";
- const isObject = type === "[object Object]";
- if (!val) {
- return true;
- } else if (isArray) {
- return !val.length;
- } else if (isObject) {
- return !Object.keys(val).length;
- }
- }
- target = Object.keys(target).reduce(function (result, key) {
- const type = Object.prototype.toString.call(target[key]);
- const isObject = type === "[object Object]" || type === "[object Array]";
- if (!isObject || isEmpty(target[key])) {
- result[key] = target[key];
- return result;
- } else {
- return addKeys(key, result, flatten(target[key], opts));
- }
- }, {});
- Object.keys(target).forEach(function (key) {
- const split = key.split(delimiter).map(transformKey);
- let key1 = getkey(split.shift());
- let key2 = getkey(split[0]);
- let recipient = result;
- while (key2 !== undefined) {
- if (key1 === "__proto__") {
- return;
- }
- const type = Object.prototype.toString.call(recipient[key1]);
- const isobject = type === "[object Object]" || type === "[object Array]";
- // do not write over falsey, non-undefined values if overwrite is false
- if (!overwrite && !isobject && typeof recipient[key1] !== "undefined") {
- return;
- }
- if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
- recipient[key1] = typeof key2 === "number" && !opts.object ? [] : {};
- }
- recipient = recipient[key1];
- if (split.length > 0) {
- key1 = getkey(split.shift());
- key2 = getkey(split[0]);
- }
- }
- // unflatten again for 'messy objects'
- recipient[key1] = unflatten(target[key], opts);
- });
- return result;
- };
- const flatDiff = function (obj1, obj2) {
- const obj1Keys = Object.keys(obj1);
- const obj2Keys = Object.keys(obj2);
- const updated = new Set();
- const added = new Set();
- const removed = new Set();
- for (const key of obj1Keys) {
- if (Array.isArray(obj1[key]) || Array.isArray(obj1[key])) {
- continue;
- }
- if (obj1[key] && obj2[key] === undefined) {
- removed.add(key);
- continue;
- }
- if (obj1[key] === undefined && obj2[key]) {
- added.add(key);
- continue;
- }
- if (obj1[key] !== obj2[key]) {
- updated.add(key);
- }
- }
- for (const key of obj2Keys) {
- if (Array.isArray(obj1[key]) || Array.isArray(obj1[key])) {
- continue;
- }
- if (obj1[key] && obj2[key] === undefined) {
- removed.add(key);
- continue;
- }
- if (obj1[key] === undefined && obj2[key]) {
- added.add(key);
- continue;
- }
- if (obj1[key] !== obj2[key]) {
- updated.add(key);
- }
- }
- return {
- added: [...added],
- removed: [...removed],
- updated: [...updated],
- };
- };
- const objDiff = function (obj1, obj2) {
- return flatDiff(flatten(obj1), flatten(obj2));
- };
- const unmapContentBlocksByCmsBlockUID = function (cmsMappedBlocks) {
- const result = Object.values(cmsMappedBlocks);
- for (const [key, value] of Object.entries(result)) {
- console.log(value);
- if (value && typeof value === "object") {
- if (value.cmsBlockUID) {
- cmsMappedBlocks[
- value.cmsCustomBlockPosition
- ] = unmapContentBlocksByCmsBlockUID(value);
- } else {
- cmsMappedBlocks[key] = unmapContentBlocksByCmsBlockUID(value);
- }
- }
- }
- return cmsMappedBlocks;
- };
- const unmapContentBlocksByCmsBlockUID2 = function (cmsMappedBlocks) {
- const result = Object.values(cmsMappedBlocks);
- result.forEach((item) => {
- if (item.cmsBlockUID) {
- item[item.cmsCustomBlockPosition] = Object.values(item);
- } else {
- if (item && typeof item === "object") {
- item = unmapContentBlocksByCmsBlockUID2(item);
- }
- }
- });
- return result;
- };
- const dfsVisitObject = function (obj) {
- if (obj)
- Object.keys(obj).forEach((key) => {
- if (obj[key] && typeof obj[key] === "object") {
- if (obj[key].cmsBlockUID) {
- obj[obj[key].cmsCustomBlockPosition] = obj[key];
- dfsVisitObject(obj[key]);
- delete obj[key];
- } else {
- dfsVisitObject(obj[key]);
- }
- }
- });
- };
- let test = {
- "09ea55b0-14cf-4e02-bf68-33750991f571": {
- type: "BoxGridRow",
- cmsBlockUID: "09ea55b0-14cf-4e02-bf68-33750991f571",
- cmsBlockOwner: "hq",
- isLocked: false,
- isPinned: false,
- items: {
- "5846ac58-9c8f-4094-8ff8-5a80295dbda2": {
- cmsBlockUID: "5846ac58-9c8f-4094-8ff8-5a80295dbda2",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- preTitle: "test-new-validate",
- title: "test-new-validate",
- subTitle: "test-new-validate",
- description: "test-new-validate",
- width: "33%",
- media: {
- landscapeBig: {
- id: "603794de41c6d945e01235d3",
- },
- portraitMedium: {
- id: "603794de41c6d945e01235d3",
- },
- },
- thronPlaylistId: "603794de41c6d945e01235d3",
- cta: null,
- ctaType: "standard",
- filterValues: {
- filter1: "",
- filter2: "",
- filter3: "",
- },
- cmsCustomBlockPosition: 0,
- },
- "8156c2e3-8657-42e3-abb7-e98c8cf38280": {
- cmsBlockUID: "8156c2e3-8657-42e3-abb7-e98c8cf38280",
- cmsBlockOwner: "hq",
- isLocked: false,
- isPinned: false,
- preTitle: "test-new-validate",
- title: "test-new-validate",
- subTitle: "test-new-validate",
- description: "test-new-validate",
- width: "33%",
- media: {
- landscapeBig: {
- id: "603794de41c6d945e01235d3",
- },
- portraitMedium: {
- id: "603794de41c6d945e01235d3",
- },
- },
- thronPlaylistId: "603794de41c6d945e01235d3",
- cta: null,
- ctaType: "standard",
- filterValues: {
- filter1: "",
- filter2: "",
- filter3: "",
- },
- cmsCustomBlockPosition: 1,
- },
- },
- cmsCustomBlockPosition: 0,
- },
- };
- dfsVisitObject(test);
- test = unflatten(flatten(test));
- console.log(JSON.stringify(Object.values(test), null, 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement