refacto(export-v2): Move merge function to objects lib

This commit is contained in:
Baptiste Studer 2022-10-02 13:23:45 +02:00
parent 753996ad08
commit 885d49e6f1
2 changed files with 22 additions and 20 deletions

View File

@ -1,3 +1,5 @@
const deepmerge = require('deepmerge');
class ObjectBuilder {
_obj = {};
@ -26,6 +28,19 @@ const isObjectEmpty = (obj) => {
return true;
};
const mergeObjects = (x, y) => {
return deepmerge(x, y, {
arrayMerge: (target, source) => {
source.forEach((item) => {
if (target.indexOf(item) === -1) {
target.push(item);
}
});
return target;
},
});
};
const logObj = (obj) => JSON.stringify(obj, null, ' ');
module.exports = {
@ -33,4 +48,5 @@ module.exports = {
logObj,
isObjectSafe,
isObjectEmpty,
mergeObjects,
};

View File

@ -1,11 +1,10 @@
const deepmerge = require('deepmerge');
const cloneDeep = require('lodash/cloneDeep');
const flattenDeep = require('lodash/flattenDeep');
const { isEmpty, merge } = require('lodash/fp');
const qs = require('qs');
const { isArraySafe, toArray } = require('../../../libs/arrays');
const { ObjectBuilder, isObjectSafe } = require('../../../libs/objects');
const { ObjectBuilder, isObjectSafe, mergeObjects } = require('../../../libs/objects');
const { CustomSlugToSlug, CustomSlugs } = require('../../config/constants');
const { getModelAttributes } = require('../../utils/models');
const { convertToJson } = require('./converters-v2');
@ -102,7 +101,7 @@ const findEntriesForHierarchy = async (slug, hierarchy, deepness, { search, ids
entriesFlatten = entriesFlatten.map((entry) => flattenEntry(entry, slug));
})();
storedData = ownMerge({ [slug]: Object.fromEntries(entriesFlatten.map((entry) => [entry.id, entry])) }, storedData);
storedData = mergeObjects({ [slug]: Object.fromEntries(entriesFlatten.map((entry) => [entry.id, entry])) }, storedData);
// Skip admin::user slug.
(() => {
@ -134,7 +133,7 @@ const findEntriesForHierarchy = async (slug, hierarchy, deepness, { search, ids
);
const subStore = await findEntriesForHierarchy(hierarchy[attribute.name].__slug, hierarchy[attribute.name], deepness - 1, { ids });
storedData = ownMerge(subStore, storedData);
storedData = mergeObjects(subStore, storedData);
}
})();
@ -159,7 +158,7 @@ const findEntriesForHierarchy = async (slug, hierarchy, deepness, { search, ids
const ids = componentEntries.map((entry) => entry.id);
const subStore = await findEntriesForHierarchy(componentHierarchy.__slug, componentHierarchy, deepness - 1, { ids });
storedData = ownMerge(subStore, storedData);
storedData = mergeObjects(subStore, storedData);
}
}
})();
@ -180,7 +179,7 @@ const findEntriesForHierarchy = async (slug, hierarchy, deepness, { search, ids
);
const subStore = await findEntriesForHierarchy(hierarchy[attribute.name].__slug, hierarchy[attribute.name], deepness - 1, { ids });
storedData = ownMerge(subStore, storedData);
storedData = mergeObjects(subStore, storedData);
}
})();
@ -200,26 +199,13 @@ const findEntriesForHierarchy = async (slug, hierarchy, deepness, { search, ids
);
const subStore = await findEntriesForHierarchy(hierarchy[attribute.name].__slug, hierarchy[attribute.name], deepness - 1, { ids });
storedData = ownMerge(subStore, storedData);
storedData = mergeObjects(subStore, storedData);
}
})();
return storedData;
};
const ownMerge = (x, y) => {
return deepmerge(x, y, {
arrayMerge: (target, source) => {
source.forEach((item) => {
if (target.indexOf(item) === -1) {
target.push(item);
}
});
return target;
},
});
};
const findEntries = async (slug, deepness, { search, ids }) => {
try {
const queryBuilder = new ObjectBuilder();