Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getRandomValues = require("get-random-values");
- const validateContent = function (
- contentMaster,
- contentSlave,
- validateCurrentLevel = false,
- failures = []
- ) {
- if (contentMaster === null) {
- const outcome = !validateCurrentLevel || contentSlave === null;
- if (outcome === false) {
- console.log("Failing on null mutation");
- }
- return outcome;
- }
- for ([key, value] of Object.entries(contentMaster)) {
- // Le primitive del master
- const haveToValidate =
- contentMaster.isLocked !== undefined
- ? contentMaster.isLocked
- : validateCurrentLevel;
- // console.log(
- // key,
- // "is primitive: ",
- // !Array.isArray(value) && !(typeof value === "object")
- // );
- if (!Array.isArray(value) && !(typeof value === "object")) {
- if (haveToValidate) {
- if (
- contentSlave === undefined ||
- contentMaster[key] !== contentSlave[key]
- ) {
- console.log("Failing on", key);
- console.log(!!contentMaster[key] && !!contentMaster[key].isLocked);
- failures.push(!!contentMaster[key]);
- continue;
- }
- }
- }
- if (Array.isArray(value) || typeof value === "object") {
- // console.log("REC", key, contentMaster[key], 'vs', contentSlave[key]);
- if (
- (contentSlave === undefined) ||
- (contentSlave[key] === undefined && haveToValidate)
- ) {
- console.log("Failing on deleted", key);
- failures.push(!!contentMaster[key] && !!contentMaster[key].isLocked);
- continue;
- }
- if (
- !validateContent(
- contentMaster[key],
- contentSlave[key],
- haveToValidate,
- failures
- )
- ) {
- failures.push(!!contentMaster[key] && !!contentMaster[key].isLocked);
- continue;
- }
- }
- }
- return [
- failures,
- failures.length === 0 || failures.some((failure) => failure === false),
- ];
- };
- const generateRandomCmsBlockUID = function () {
- return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
- (c ^ (getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
- );
- };
- const getOwnerTypeByReferenceType = function (referenceType) {
- if (!referenceType || referenceType === "hq") {
- return "hq";
- }
- if (referenceType === "group") {
- return "group";
- }
- if (referenceType === "dealer") {
- return "dealer";
- }
- };
- const mapContentBlocksByCmsBlockUID = function (cmsBlocks) {
- const mapByCmsBlockUID = {};
- // console.log();
- cmsBlocks.forEach((cmsItem, index) => {
- // Assigning owner
- // console.log("SONO NELL'iterazione: ", cmsItem);
- cmsItem["cmsCustomBlockPosition"] = index;
- if (!cmsItem.cmsBlockOwner) {
- cmsItem.cmsBlockOwner = getOwnerTypeByReferenceType(
- cmsItem.referenceType
- );
- }
- if (cmsItem.cmsBlockUID) {
- mapByCmsBlockUID[cmsItem.cmsBlockUID] = cmsItem;
- } else {
- cmsItem.cmsBlockUID = generateRandomCmsBlockUID();
- mapByCmsBlockUID[cmsItem.cmsBlockUID] = cmsItem;
- }
- Object.keys(cmsItem).forEach((cmsItemKey) => {
- if (Array.isArray(cmsItem[cmsItemKey])) {
- cmsItem[cmsItemKey] = mapContentBlocksByCmsBlockUID(
- cmsItem[cmsItemKey]
- );
- }
- });
- });
- return mapByCmsBlockUID;
- };
- const map1 = {
- "2be4f7c3-1eed-443b-a6d6-0ca67de96bee": {
- type: "BoxGrid",
- cmsBlockUID: "2be4f7c3-1eed-443b-a6d6-0ca67de96bee",
- cmsBlockOwner: "hq",
- isLocked: false,
- isPinned: false,
- palette: "",
- title: "Pagina test merge 4",
- filters: null,
- slug: "Pagina test merge 4",
- chapter: { name: "Pagina test merge 4", nav: false },
- share: null,
- items: {
- "c3d81502-3641-4666-881c-3a681c7c1239": {
- type: "BoxGridRow",
- cmsBlockUID: "c3d81502-3641-4666-881c-3a681c7c1239",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- items: {
- "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a": {
- cmsBlockUID: "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- preTitle: "Pre Title",
- title: "Pre Title ",
- subTitle: "Pagina test merge 4",
- description: "Pagina test merge 4 Subtitle",
- width: "50%",
- media: {
- landscapeBig: { id: "603794de41c6d945e01235d3" },
- portraitMedium: { id: "603794de41c6d945e01235d3" },
- },
- thronPlaylistId: "",
- cta: null,
- ctaType: "standard",
- filterValues: { filter1: "", filter2: "", filter3: "" },
- },
- "cb393c42-7e78-4568-8073-26b331239b0f": {
- cmsBlockUID: "cb393c42-7e78-4568-8073-26b331239b0f",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- preTitle: "Pre Title i2",
- title: "Pre Title i2",
- subTitle: "Pre Title i2",
- description: "Pre Title i2",
- width: "100%",
- media: {
- landscapeBig: { id: "6037778241c6d945e0123271" },
- portraitMedium: { id: "6037778241c6d945e0123271" },
- },
- thronPlaylistId: "",
- cta: null,
- ctaType: "standard",
- filterValues: {
- filter1: "Filter 1",
- filter2: "Filter 2",
- filter3: "Filter 3",
- },
- },
- },
- },
- },
- },
- };
- const map2 = {
- "2be4f7c3-1eed-443b-a6d6-0ca67de96bee": {
- type: "BoxGrid",
- cmsBlockUID: "2be4f7c3-1eed-443b-a6d6-0ca67de96bee",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- palette: "",
- title: "Pagina test merge 114",
- filters: null,
- slug: "Pagina test merge 4",
- chapter: { name: "Pagina test merge 4", nav: false },
- share: null,
- items: {
- "c3d81502-3641-4666-881c-3a681c7c1239": {
- type: "BoxGridRow",
- cmsBlockUID: "c3d81502-3641-4666-881c-3a681c7c1239",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- items: {
- "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a": {
- cmsBlockUID: "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- preTitle: "Pre Title",
- title: "Pre Title ",
- subTitle: "Pagina test merge 4",
- description: "Pagina test merge 5 Subtitle",
- width: "50%",
- media: {
- landscapeBig: { id: "603794de41c6d945e01235d3" },
- portraitMedium: { id: "603794de41c6d945e01235d3" },
- },
- thronPlaylistId: "",
- cta: null,
- ctaType: "standard",
- filterValues: { filter1: "", filter2: "", filter3: "" },
- },
- "cb393c42-7e78-4568-8073-26b331239b0f": {
- cmsBlockUID: "cb393c42-7e78-4568-8073-26b331239b0f",
- cmsBlockOwner: "hq",
- isLocked: true,
- isPinned: false,
- preTitle: "Pre Title i2",
- title: "Pre Title i2",
- subTitle: "Pre Title i2",
- description: "Pre Title i2",
- width: "100%",
- media: {
- landscapeBig: { id: "6037778241c6d945e0123271" },
- portraitMedium: { id: "6037778241c6d945e0123271" },
- },
- thronPlaylistId: "",
- cta: null,
- ctaType: "standard",
- filterValues: {
- filter1: "Filter 1",
- filter2: "Filter 2",
- filter3: "Filter 3",
- },
- },
- },
- },
- },
- },
- };
- console.log("FINAL RESULT", validateContent(map1, map2));
Add Comment
Please, Sign In to add comment