@featurevisor/core
Version:
Core package of Featurevisor for Node.js usage
46 lines • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findDuplicateSegments = findDuplicateSegments;
const crypto = require("crypto");
async function findDuplicateSegments(deps, options = {}) {
const { datasource } = deps;
const segments = await datasource.listSegments();
const segmentsWithHash = [];
for (const segmentKey of segments) {
const segment = await datasource.readSegment(segmentKey);
const conditions = JSON.stringify(segment.conditions);
const hash = crypto.createHash("sha256").update(conditions).digest("hex");
segmentsWithHash.push({
segmentKey,
hash,
});
}
const groupedSegments = segmentsWithHash.reduce((acc, { segmentKey, hash }) => {
if (!acc[hash]) {
acc[hash] = [];
}
acc[hash].push(segmentKey);
return acc;
}, {});
const duplicateSegments = Object.values(groupedSegments).filter((segmentKeys) => segmentKeys.length > 1);
const result = [];
for (const segmentKeys of duplicateSegments) {
const entry = {
segments: segmentKeys,
};
if (options.authors) {
const historyEntries = [];
for (const segmentKey of segmentKeys) {
const entries = await datasource.listHistoryEntries("segment", segmentKey);
entries.forEach((entry) => {
historyEntries.push(entry);
});
}
const authors = Array.from(new Set(historyEntries.map((entry) => entry.author)));
entry.authors = authors;
}
result.push(entry);
}
return result;
}
//# sourceMappingURL=findDuplicateSegments.js.map