<commit_before>module.exports = {
    createLicenceObject,
    createAddressObject,
    createReportingInstructionsObject,
    createConditionsObject,
    addAdditionalConditions
};
const {DATE_FIELD} = require('../services/utils/conditionsValidator');
const {formatObjectForView} = require('../services/utils/formatForView');

function createLicenceObject(object) {

    const acceptedAttributes = Object.keys(licenceModel);

    return filteredToAttributes(object, acceptedAttributes);
}

function createAddressObject(object) {
    const acceptedAttributes = Object.keys(licenceModel.dischargeAddress);

    return filteredToAttributes(object, acceptedAttributes);
}

function createReportingInstructionsObject(object) {
    const acceptedAttributes = Object.keys(licenceModel.reporting);

    return filteredToAttributes(object, acceptedAttributes);
}

const filteredToAttributes = (input, acceptedKeys) => {
    return Object.keys(input).reduce((objectBuilt, key) => {
        if(acceptedKeys.includes(key)) {
            const value = input[key] || '';
            return {...objectBuilt, ...{[key]: value}};
        }
        return objectBuilt;
    }, {});
};

function createConditionsObject(selectedConditions, formInputs) {

    return selectedConditions.reduce((conditions, condition) => {

        const conditionAttributes = condition.FIELD_POSITION.value;
        if (!conditionAttributes) {
            return {...conditions, [condition.ID.value]: {}};
        }

        return {...conditions, [condition.ID.value]: inputsFor(conditionAttributes, formInputs)};

    }, {});
}

function addAdditionalConditions(rawLicence, selectedConditions) {
    const additionalConditions = Object.keys(rawLicence.additionalConditions).map(condition => {

        const selectedCondition = selectedConditions.find(selected => selected.ID.value == condition);
        const userInputName = selectedCondition.USER_INPUT.value;

        if(userInputName) {
            const userInput = formatObjectForView(rawLicence.additionalConditions[condition], {dates: [DATE_FIELD]});
            return injectUserInputInto(selectedCondition, userInput);
        }

        return selectedCondition.TEXT.value;
    });

    return {...rawLicence, additionalConditions};
}

function injectUserInputInto(condition, userInput) {

    const fieldPositionObject = condition.FIELD_POSITION.value;
    const fieldNames = Object.keys(fieldPositionObject);
    const conditionText = condition.TEXT.value;
    const placeHolders = getPlaceholdersFrom(conditionText);

    return placeHolders.reduce((text, placeHolder, index) => {

        const fieldNameForPlaceholder = fieldNames.find(field => fieldPositionObject[field] == index);
        const inputtedData = userInput[fieldNameForPlaceholder];
        return text.replace(placeHolder, inputtedData);

    }, conditionText);
}<commit_msg>bug fix, dates being formatted multiple times<commit_after>module.exports = {
    createLicenceObject,
    createAddressObject,
    createReportingInstructionsObject,
    createConditionsObject,
    addAdditionalConditions
};

function createLicenceObject(object) {

    const acceptedAttributes = Object.keys(licenceModel);

    return filteredToAttributes(object, acceptedAttributes);
}

function createAddressObject(object) {
    const acceptedAttributes = Object.keys(licenceModel.dischargeAddress);

    return filteredToAttributes(object, acceptedAttributes);
}

function createReportingInstructionsObject(object) {
    const acceptedAttributes = Object.keys(licenceModel.reporting);

    return filteredToAttributes(object, acceptedAttributes);
}

const filteredToAttributes = (input, acceptedKeys) => {
    return Object.keys(input).reduce((objectBuilt, key) => {
        if(acceptedKeys.includes(key)) {
            const value = input[key] || '';
            return {...objectBuilt, ...{[key]: value}};
        }
        return objectBuilt;
    }, {});
};

function createConditionsObject(selectedConditions, formInputs) {

    return selectedConditions.reduce((conditions, condition) => {

        const conditionAttributes = condition.FIELD_POSITION.value;
        if (!conditionAttributes) {
            return {...conditions, [condition.ID.value]: {}};
        }

        return {...conditions, [condition.ID.value]: inputsFor(conditionAttributes, formInputs)};

    }, {});
}

function addAdditionalConditions(rawLicence, selectedConditions) {
    const additionalConditions = Object.keys(rawLicence.additionalConditions).map(condition => {

        const selectedCondition = selectedConditions.find(selected => selected.ID.value == condition);
        const userInputName = selectedCondition.USER_INPUT.value;

        if(userInputName) {
            const userInput = rawLicence.additionalConditions[condition];
            return injectUserInputInto(selectedCondition, userInput);
        }

        return selectedCondition.TEXT.value;
    });

    return {...rawLicence, additionalConditions};
}

function injectUserInputInto(condition, userInput) {

    const fieldPositionObject = condition.FIELD_POSITION.value;
    const fieldNames = Object.keys(fieldPositionObject);
    const conditionText = condition.TEXT.value;
    const placeHolders = getPlaceholdersFrom(conditionText);

    return placeHolders.reduce((text, placeHolder, index) => {

        const fieldNameForPlaceholder = fieldNames.find(field => fieldPositionObject[field] == index);
        const inputtedData = userInput[fieldNameForPlaceholder];
        return text.replace(placeHolder, inputtedData);

    }, conditionText);
}
{'fraction_non_alphanumeric': 0.10578127796670843, 'fraction_numerical': 0.0, 'mean_word_length': 4.121906507791017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6935354', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1398, 'n_words': 260}