1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/eventDiffer.js
2020-02-20 08:30:21 +01:00

74 lines
1.8 KiB
JavaScript

var eventType = require('./eventType');
var diff = require('deep-diff').diff;
var strategyTypes = [
eventType.strategyCreated,
eventType.strategyDeleted
];
var featureTypes = [
eventType.featureCreated,
eventType.featureUpdated,
eventType.featureArchived,
eventType.featureRevived
];
function baseTypeFor(event) {
if (featureTypes.indexOf(event.type) !== -1) {
return 'features';
} else if (strategyTypes.indexOf(event.type) !== -1) {
return 'strategies';
} else {
throw new Error('unknown event type: ' + JSON.stringify(event));
}
}
function groupByBaseTypeAndName(events) {
var groups = {};
events.forEach(function (event) {
var baseType = baseTypeFor(event);
groups[baseType] = groups[baseType] || {};
groups[baseType][event.data.name] = groups[baseType][event.data.name] || [];
groups[baseType][event.data.name].push(event);
});
return groups;
}
function eachConsecutiveEvent(events, callback) {
var groups = groupByBaseTypeAndName(events);
Object.keys(groups).forEach(function (baseType) {
var group = groups[baseType];
Object.keys(group).forEach(function (name) {
var events = group[name];
var left, right, i, l;
for (i = 0, l = events.length; i < l; i++) {
left = events[i];
right = events[i + 1];
callback(left, right);
}
});
});
}
function addDiffs(events) {
eachConsecutiveEvent(events, function (left, right) {
if (right) {
left.diffs = diff(right.data, left.data);
left.diffs = left.diffs || [];
} else {
left.diffs = null;
}
});
}
module.exports = {
addDiffs: addDiffs
};