<commit_before>import moment       from 'moment';
import { localize } from '../../../../../_common/localize';

const duration_maps = {
    t: { display: 'ticks',   order: 1 },
    s: { display: 'seconds', order: 2, to_second: 1 },
    m: { display: 'minutes', order: 3, to_second: 60 },
    h: { display: 'hours',   order: 4, to_second: 60 * 60 },
    d: { display: 'days',    order: 5, to_second: 60 * 60 * 24 },
};

const buildDurationConfig = (contract, durations = { min_max: {}, units_display: {} }) => {
    durations.min_max[contract.start_type]       = durations.min_max[contract.start_type] || {};
    durations.units_display[contract.start_type] = durations.units_display[contract.start_type] || [];

    const obj_min = getDurationFromString(contract.min_contract_duration);
    const obj_max = getDurationFromString(contract.max_contract_duration);

    durations.min_max[contract.start_type][contract.expiry_type] = {
        min: convertDurationUnit(obj_min.duration, obj_min.unit, 's'),
        max: convertDurationUnit(obj_max.duration, obj_max.unit, 's'),
    };

    const arr_units = [];
    durations.units_display[contract.start_type].forEach(obj => {
        arr_units.push(obj.value);
    });
    if (/^tick|daily$/.test(contract.expiry_type)) {
        if (arr_units.indexOf(obj_min.unit) === -1) {
            arr_units.push(obj_min.unit);
        }
    } else {
        Object.keys(duration_maps).forEach(u => {
            if (
                u !== 'd' && // when the expiray_type is intraday, the supported units are seconds, minutes and hours.
                arr_units.indexOf(u) === -1 &&
                duration_maps[u].order >= duration_maps[obj_min.unit].order &&
                duration_maps[u].order <= duration_maps[obj_max.unit].order) {
                arr_units.push(u);
            }
        });
    }

    durations.units_display[contract.start_type] = arr_units
        .sort((a, b) => (duration_maps[a].order > duration_maps[b].order ? 1 : -1))
        .reduce((o, c) => (
            [...o, { text: localize(duration_maps[c].display), value: c }]
        ), []);

    return durations;
};

const convertDurationUnit = (value, from_unit, to_unit) => {
    if (!value || !from_unit || !to_unit) return null;
    if (from_unit === to_unit || !('to_second' in duration_maps[from_unit])) return value;
    return (value * duration_maps[from_unit].to_second) / duration_maps[to_unit].to_second;
};

const getDurationFromString = (duration_string) => {
    const duration = duration_string.toString().match(/[a-zA-Z]+|[0-9]+/g);
    return {
        duration: duration[0],
        unit    : duration[1],
    };
};

const getExpiryType = (store) => {
    const { duration_unit, expiry_date, expiry_type } = store;
    const server_time = store.root_store.common.server_time;

    const duration_is_day       = expiry_type === 'duration' && duration_unit === 'd';
    const expiry_is_after_today = expiry_type === 'endtime' && moment.utc(expiry_date).isAfter(moment(server_time).utc(), 'day');

    let contract_expiry_type = 'daily';
    if (!duration_is_day && !expiry_is_after_today) {
        contract_expiry_type = duration_unit === 't' ? 'tick' : 'intraday';
    }

    return contract_expiry_type;
};

const convertDurationLimit = (value, unit) => {
    if (unit === 'm') {
        const minute = value / 60;
        return minute >= 1 ? Math.floor(minute) : 1;
    } else if (unit === 'h') {
        const hour = value / 3600;
        return hour >= 1 ? Math.floor(hour) : 1;
    }

    return value;
};

module.exports = {
    buildDurationConfig,
    convertDurationLimit,
    getExpiryType,
};<commit_msg>Fixed the issue of validating duraiton when the unit is day<commit_after>import moment       from 'moment';
import { localize } from '../../../../../_common/localize';

const duration_maps = {
    t: { display: 'ticks',   order: 1 },
    s: { display: 'seconds', order: 2, to_second: 1 },
    m: { display: 'minutes', order: 3, to_second: 60 },
    h: { display: 'hours',   order: 4, to_second: 60 * 60 },
    d: { display: 'days',    order: 5, to_second: 60 * 60 * 24 },
};

const buildDurationConfig = (contract, durations = { min_max: {}, units_display: {} }) => {
    durations.min_max[contract.start_type]       = durations.min_max[contract.start_type] || {};
    durations.units_display[contract.start_type] = durations.units_display[contract.start_type] || [];

    const obj_min = getDurationFromString(contract.min_contract_duration);
    const obj_max = getDurationFromString(contract.max_contract_duration);

    durations.min_max[contract.start_type][contract.expiry_type] = {
        min: convertDurationUnit(obj_min.duration, obj_min.unit, 's'),
        max: convertDurationUnit(obj_max.duration, obj_max.unit, 's'),
    };

    const arr_units = [];
    durations.units_display[contract.start_type].forEach(obj => {
        arr_units.push(obj.value);
    });
    if (/^tick|daily$/.test(contract.expiry_type)) {
        if (arr_units.indexOf(obj_min.unit) === -1) {
            arr_units.push(obj_min.unit);
        }
    } else {
        Object.keys(duration_maps).forEach(u => {
            if (
                u !== 'd' && // when the expiray_type is intraday, the supported units are seconds, minutes and hours.
                arr_units.indexOf(u) === -1 &&
                duration_maps[u].order >= duration_maps[obj_min.unit].order &&
                duration_maps[u].order <= duration_maps[obj_max.unit].order) {
                arr_units.push(u);
            }
        });
    }

    durations.units_display[contract.start_type] = arr_units
        .sort((a, b) => (duration_maps[a].order > duration_maps[b].order ? 1 : -1))
        .reduce((o, c) => (
            [...o, { text: localize(duration_maps[c].display), value: c }]
        ), []);

    return durations;
};

const convertDurationUnit = (value, from_unit, to_unit) => {
    if (!value || !from_unit || !to_unit) return null;
    if (from_unit === to_unit || !('to_second' in duration_maps[from_unit])) return value;
    return (value * duration_maps[from_unit].to_second) / duration_maps[to_unit].to_second;
};

const getDurationFromString = (duration_string) => {
    const duration = duration_string.toString().match(/[a-zA-Z]+|[0-9]+/g);
    return {
        duration: duration[0],
        unit    : duration[1],
    };
};

const getExpiryType = (store) => {
    const { duration_unit, expiry_date, expiry_type } = store;
    const server_time = store.root_store.common.server_time;

    const duration_is_day       = expiry_type === 'duration' && duration_unit === 'd';
    const expiry_is_after_today = expiry_type === 'endtime' && moment.utc(expiry_date).isAfter(moment(server_time).utc(), 'day');

    let contract_expiry_type = 'daily';
    if (!duration_is_day && !expiry_is_after_today) {
        contract_expiry_type = duration_unit === 't' ? 'tick' : 'intraday';
    }

    return contract_expiry_type;
};

const convertDurationLimit = (value, unit) => {
    if (unit === 'm') {
        const minute = value / 60;
        return minute >= 1 ? Math.floor(minute) : 1;
    } else if (unit === 'h') {
        const hour = value / (60 * 60);
        return hour >= 1 ? Math.floor(hour) : 1;
    } else if (unit === 'd') {
        const day = value / (60 * 60 * 24);
        return day >= 1 ? Math.floor(day) : 1;
    }

    return value;
};

module.exports = {
    buildDurationConfig,
    convertDurationLimit,
    getExpiryType,
};
{'fraction_non_alphanumeric': 0.15568622210343444, 'fraction_numerical': 0.010690899371909661, 'mean_word_length': 2.910135841170324, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '7390983', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2409, 'n_words': 482}