<commit_before>import {on, is, isNot, undefinedOrEmpty, to, keys, filter, equal, values, Pair} from 'tsfun';
import {Query} from 'idai-components-2';
import {IndexItem, SimpleIndexItem} from '../index/index-item';
import {SortUtil} from '../../util/sort-util';
import {Name, ResourceId} from '../../constants';
import {isUndefinedOrEmpty} from 'tsfun/src/predicate';
import {pairWith} from '../../util/utils';

// @author <NAME>
// @author <NAME>

/**
 * @param indexItems // TODO review typing: must be Array<IndexItem> if exactMatchFirst
 * @param query
 */
export function getSortedIds(indexItems: Array<SimpleIndexItem>,
                             query: Query): Array<ResourceId> {

    indexItems = generateOrderedResultList(indexItems);
    if (shouldRankTypes(query)) {
        indexItems = handleTypesForName(indexItems, query.rankOptions['matchType']);
    }
    handleExactMatch(indexItems, query);
    return indexItems.map(to('id'));
}


function shouldRankTypes(query: Query) {

    return equal(query.types)(['Type']) && query.rankOptions && query.rankOptions['matchType'];
}


function handleTypesForName(indexItems: Array<SimpleIndexItem>,
                            rankTypesFor: Name) {

    const pairs = calcPercentages(indexItems, rankTypesFor);
    pairs.sort(comparePercentages);
    return pairs.map(to('[0]'));
}


function comparePercentages(a: any, b: any) {<commit_msg>Replace first with to<commit_after>import {on, is, isNot, undefinedOrEmpty, to, first, keys, filter, equal, values, Pair} from 'tsfun';
import {Query} from 'idai-components-2';
import {IndexItem, SimpleIndexItem} from '../index/index-item';
import {SortUtil} from '../../util/sort-util';
import {Name, ResourceId} from '../../constants';
import {isUndefinedOrEmpty} from 'tsfun/src/predicate';
import {pairWith} from '../../util/utils';

// @author <NAME>
// @author <NAME>

/**
 * @param indexItems // TODO review typing: must be Array<IndexItem> if exactMatchFirst
 * @param query
 */
export function getSortedIds(indexItems: Array<SimpleIndexItem>,
                             query: Query): Array<ResourceId> {

    indexItems = generateOrderedResultList(indexItems);
    if (shouldRankTypes(query)) {
        indexItems = handleTypesForName(indexItems, query.rankOptions['matchType']);
    }
    handleExactMatch(indexItems, query);
    return indexItems.map(to('id'));
}


function shouldRankTypes(query: Query) {

    return equal(query.types)(['Type']) && query.rankOptions && query.rankOptions['matchType'];
}


function handleTypesForName(indexItems: Array<SimpleIndexItem>,
                            rankTypesFor: Name) {

    const pairs = calcPercentages(indexItems, rankTypesFor);
    pairs.sort(comparePercentages);
    return pairs.map(first) as Array<SimpleIndexItem>;
}


function comparePercentages(a: any, b: any) {
{'fraction_non_alphanumeric': 0.14406481155336387, 'fraction_numerical': 0.0010567101091933778, 'mean_word_length': 4.062388591800357, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1550051', 'n_tokens_mistral': 829, 'n_tokens_neox': 824, 'n_words': 200}