@graphprotocol/graph-cli
Version: 
CLI for building for and deploying to The Graph
39 lines (38 loc) • 1.71 kB
JavaScript
/**
 * Sorts an array with specified elements given priority.
 * Use a predicate function, or provide an array to prioritise elements.
 * If no compare function is provided, default JS sorting (ascending) behaviour prevails.
 *
 * @param {T[]} array - The array to be sorted.
 * @param {((element: T) => boolean) | T[]} prioritySpecifier - A function that returns true if an element should be prioritized, or an array of elements to be prioritized.
 * @param {(a: T, b: T) => number} [compareFunction] - An optional comparison function to sort the elements of the array. If omitted, the array is sorted in default order.
 * @returns {T[]} The sorted array with priority elements first.
 *
 * @example
 * const numbers = [5, 3, 9, 1, 4];
 * sortWithPriority(numbers, x => x > 5); // [9, 1, 3, 4, 5]
 * sortWithPriority(numbers, [9, 1]); // [1, 9, 3, 4, 5]
 */
function sortWithPriority(array, prioritySpecifier, compareFunction) {
    // prioritySpecifier can be an array or a function so handle each case
    let isPriorityElement;
    if (Array.isArray(prioritySpecifier) || !prioritySpecifier) {
        const prioritySet = new Set(prioritySpecifier ?? []);
        isPriorityElement = (element) => prioritySet.has(element);
    }
    else {
        isPriorityElement = prioritySpecifier;
    }
    const priorityArray = array.filter(isPriorityElement);
    const regularArray = array.filter(item => !isPriorityElement(item));
    if (compareFunction) {
        priorityArray.sort(compareFunction);
        regularArray.sort(compareFunction);
    }
    else {
        priorityArray.sort();
        regularArray.sort();
    }
    return priorityArray.concat(regularArray);
}
export { sortWithPriority };