<commit_before>import { createSelector } from 'reselect'
import get from 'lodash/fp/get'

import * as constants from './constants'

/**
 * Given an augmented page doc, attempts to calculate its approx. freeze dry page size in MB.
 * @param {any} pageDoc The augmented page doc from search results.
 * @returns {string} Approx. size in MB of freeze-dry attachment. 0 if not found.
 */
function calcFreezeDrySize(pageDoc) {
    const pageSize = get(['_attachments', 'frozen-page.html', 'length'])(pageDoc)
    const sizeInMB = pageSize !== undefined
        ? Math.round(pageSize / 1024**2 * 10) / 10
        : 0

    return sizeInMB.toString()
}

/**
 * Either set display title to be the top-level title field, else look in content. Fallback is the URL.
 * @param {any} pageDoc The augmented page doc from search results.
 * @returns {string} Title string to display from page title, or URL in case of bad data..
 */
function decideTitle(pageDoc) {
    if (pageDoc.title) return pageDoc.title

    return (pageDoc.content && pageDoc.content.title) ? pageDoc.content.title : pageDoc.url
}

export const overview = state => state.overview
const searchResult = createSelector(overview, state => state.searchResult)
const resultDocs = createSelector(searchResult, results => results.docs)
export const currentQueryParams = createSelector(overview, state => state.currentQueryParams)
export const isLoading = createSelector(overview, state => state.isLoading)
export const noResults = createSelector(resultDocs, isLoading, (docs, isLoading) => docs.length === 0 && !isLoading)
export const currentPage = createSelector(overview, state => state.currentPage)
export const resultsSkip = createSelector(currentPage, page => page * constants.PAGE_SIZE)
export const deleteConfirmProps = createSelector(overview, state => state.deleteConfirmProps)
export const isDeleteConfShown = createSelector(deleteConfirmProps, state => state.isShown)
export const urlToDelete = createSelector(deleteConfirmProps, state => state.url)

export const results = createSelector(
    resultDocs,
    docs => docs.map(pageDoc => ({
        ...pageDoc,
        freezeDrySize: calcFreezeDrySize(pageDoc),
        title: decideTitle(pageDoc),
    })),<commit_msg>Move skip state derivation to results state length<commit_after>import { createSelector } from 'reselect'
import get from 'lodash/fp/get'

import * as constants from './constants'

/**
 * Given an augmented page doc, attempts to calculate its approx. freeze dry page size in MB.
 * @param {any} pageDoc The augmented page doc from search results.
 * @returns {string} Approx. size in MB of freeze-dry attachment. 0 if not found.
 */
function calcFreezeDrySize(pageDoc) {
    const pageSize = get(['_attachments', 'frozen-page.html', 'length'])(pageDoc)
    const sizeInMB = pageSize !== undefined
        ? Math.round(pageSize / 1024**2 * 10) / 10
        : 0

    return sizeInMB.toString()
}

/**
 * Either set display title to be the top-level title field, else look in content. Fallback is the URL.
 * @param {any} pageDoc The augmented page doc from search results.
 * @returns {string} Title string to display from page title, or URL in case of bad data..
 */
function decideTitle(pageDoc) {
    if (pageDoc.title) return pageDoc.title

    return (pageDoc.content && pageDoc.content.title) ? pageDoc.content.title : pageDoc.url
}

export const overview = state => state.overview
const searchResult = createSelector(overview, state => state.searchResult)
const resultDocs = createSelector(searchResult, results => results.docs)
export const currentQueryParams = createSelector(overview, state => state.currentQueryParams)
export const isLoading = createSelector(overview, state => state.isLoading)
export const noResults = createSelector(resultDocs, isLoading, (docs, isLoading) => docs.length === 0 && !isLoading)
export const currentPage = createSelector(overview, state => state.currentPage)
export const resultsSkip = createSelector(resultDocs, results => results.length)
export const deleteConfirmProps = createSelector(overview, state => state.deleteConfirmProps)
export const isDeleteConfShown = createSelector(deleteConfirmProps, state => state.isShown)
export const urlToDelete = createSelector(deleteConfirmProps, state => state.url)

export const results = createSelector(
    resultDocs,
    docs => docs.map(pageDoc => ({
        ...pageDoc,
        freezeDrySize: calcFreezeDrySize(pageDoc),
        title: decideTitle(pageDoc),
    })),
{'fraction_non_alphanumeric': 0.10229552039224427, 'fraction_numerical': 0.005348785379986628, 'mean_word_length': 4.582089552238806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '2234894', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1210, 'n_words': 416}