Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Find regular middleware entries
const middleware = instances.filter(function(m) {
return !m.isPhasePlaceHolder;
});
// Sort the entries to keep the order
stableSortInPlace(phases, compareByOrder);
// Build a map for phase orders (phaseName --> phaseOrder)
const phaseOrders = {};
phases.forEach(function(p) {
phaseOrders[p.phase] = p.order;
});
stableSortInPlace(middleware, function(m1, m2) {
// First by phase
let delta = phaseOrders[m1.phase] - phaseOrders[m2.phase];
if (delta !== 0) return (delta > 0 ? 1 : -1);
// by subPhase
delta = compareBySubPhase(m1, m2);
if (delta !== 0) return (delta > 0 ? 1 : -1);
// By order
return compareByOrder(m1, m2);
});
return {
phases: phases,
middleware: middleware,
};
}
const playerIds = [
this.parser.player.guid,
...this.parser.player.pets.map(pet => pet.guid),
].join(',')
filter = `(${filter}) and source.id not in (${playerIds})`
// Request the new events
const newEvents = await getFflogsEvents(
this.parser.report.code,
this.parser.fight,
{filter},
)
// Add them onto the end, then sort. Using stable to ensure order is kept, as it can be sensitive sometimes.
events.push(...newEvents)
stable.inplace(events, (a, b) => {
if (a.timestamp === b.timestamp) {
const aTypeOrder = EVENT_TYPE_ORDER[a.type] || EVENT_TYPE_ORDER.default
const bTypeOrder = EVENT_TYPE_ORDER[b.type] || EVENT_TYPE_ORDER.default
return aTypeOrder - bTypeOrder
}
return a.timestamp - b.timestamp
})
return events
}
}
proto._sortLayersByPhase = function() {
if (this._skipLayerSorting) return;
const phaseOrder = {};
this._requestHandlingPhases.forEach(function(name, ix) {
phaseOrder[name + ':before'] = ix * 3;
phaseOrder[name] = ix * 3 + 1;
phaseOrder[name + ':after'] = ix * 3 + 2;
});
const router = this._router;
stableSortInPlace(router.stack, compareLayers);
function compareLayers(left, right) {
const leftPhase = left.phase;
const rightPhase = right.phase;
if (leftPhase === rightPhase) return 0;
// Builtin middleware is always first
if (leftPhase === BUILTIN_MIDDLEWARE) return -1;
if (rightPhase === BUILTIN_MIDDLEWARE) return 1;
// Layers registered via app.use and app.route
// are executed as the first items in `routes` phase
if (leftPhase === undefined) {
if (rightPhase === 'routes')
return -1;
function middleware(req, res, next) {
// bail early if req/res don't pass conditions for execution or there's no data to sort
if (!should_execute(req, res) || _.isEmpty(res.data)) {
return next();
}
// capture the pre-sort order
const presort_order = res.data.map(_.property('_id'));
// stable operates on array in place
stable.inplace(res.data, comparator(req.clean));
// capture the post-sort order
const postsort_order = res.data.map(_.property('_id'));
// log it for debugging purposes
logger.debug([
`req.clean: ${JSON.stringify(req.clean)}`,
`pre-sort: [${presort_order}]`,
`post-sort: [${postsort_order}]`
].join(', '));
next();
}
// eslint-disable-next-line no-param-reassign
currentPost.categoryInCommon =
currentPost.category_id === post.category_id;
});
const ranking = Object.keys(articles).filter(
currentSlug =>
articles[currentSlug].issue_id === latestIssueId &&
currentSlug !== post.slug,
);
if (ranking.length < 3) {
throw new Error(
'Less than three articles to qualify as related articles',
);
}
stable.inplace(ranking, (slugA, slugB) => {
const a = articles[slugA];
const b = articles[slugB];
if (a.tagsInCommon !== b.tagsInCommon) {
// This puts a before b if a has more tags in common
return b.tagsInCommon - a.tagsInCommon;
}
if (a.categoryInCommon !== b.categoryInCommon) {
if (a.categoryInCommon) {
return -1;
}
return 1;
}
return 0;
});
// since the sort is stable we know we should always get the
// same values, also if there are no articles with related tags
export function mergeEvents(existing: Fflogs.Event[], incoming: Fflogs.Event[]) {
// Create a new array with the incoming events at the end
const events = existing.concat(incoming)
// Sort them in place
stable.inplace(events, (a, b) => {
if (a.timestamp === b.timestamp) {
const aTypeOrder = EVENT_TYPE_ORDER[a.type as string] || EVENT_TYPE_ORDER.default
const bTypeOrder = EVENT_TYPE_ORDER[b.type as string] || EVENT_TYPE_ORDER.default
return aTypeOrder - bTypeOrder
}
return a.timestamp - b.timestamp
})
return events
}
function sortMiddleware(instances) {
// Find all phases
const phases = instances.filter(function(m) {
return m.isPhasePlaceHolder;
});
// Find regular middleware entries
const middleware = instances.filter(function(m) {
return !m.isPhasePlaceHolder;
});
// Sort the entries to keep the order
stableSortInPlace(phases, compareByOrder);
// Build a map for phase orders (phaseName --> phaseOrder)
const phaseOrders = {};
phases.forEach(function(p) {
phaseOrders[p.phase] = p.order;
});
stableSortInPlace(middleware, function(m1, m2) {
// First by phase
let delta = phaseOrders[m1.phase] - phaseOrders[m2.phase];
if (delta !== 0) return (delta > 0 ? 1 : -1);
// by subPhase
delta = compareBySubPhase(m1, m2);
if (delta !== 0) return (delta > 0 ? 1 : -1);
// By order
return compareByOrder(m1, m2);
return function sort(inPlace, iterable, selector, comparator = defaultComparator) {
let array = ensureArray(iterable);
const wrappedComparator = selector
? (a, b) => comparator(selector(itemValue(a)), selector(itemValue(b)))
: (a, b) => comparator(itemValue(a), itemValue(b));
if (inPlace) {
stable.inplace(array, wrappedComparator);
} else {
array = stable(array, wrappedComparator);
}
if (collectionType === 'Sequence' || collectionSubtype === 'Indexed') {
return array;
}
return new NativeConstructor(array);
};
}
}
}
for (const [file, replacements] of replacementsByFile) {
const document = await loader(file);
let contents = document.contents;
/**
* This is the important bit. We know that none of the replacements overlap,
* so in order for their source ranges in the file to all be valid at the
* time we apply them, we simply need to apply them starting from the end
* of the document and working backwards to the beginning.
*
* To preserve ordering of insertions to the same position, we use a stable
* sort.
*/
stable.inplace(replacements, (a: Replacement, b: Replacement) => {
const leftEdgeComp =
comparePositionAndRange(b.range.start, a.range, true);
if (leftEdgeComp !== 0) {
return leftEdgeComp;
}
return comparePositionAndRange(b.range.end, a.range, false);
});
for (const replacement of replacements) {
const offsets = document.sourceRangeToOffsets(replacement.range);
contents = contents.slice(0, offsets[0]) + replacement.replacementText +
contents.slice(offsets[1]);
}
result.editedFiles.set(file, contents);
}
return result;