{"size": 2032, "ext": "js", "max_stars_repo_path": "src/algorithm/certify.js", "max_stars_repo_name": "Team-e-motion/CMPE-GMV-2020", "max_stars_repo_stars_event_min_datetime": "2020-12-25T19:22:48.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-25T19:22:48.000Z", "max_issues_repo_path": "src/algorithm/certify.js", "max_issues_repo_name": "Team-e-motion/CMPE-GMV-2020", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/algorithm/certify.js", "max_forks_repo_name": "Team-e-motion/CMPE-GMV-2020", "max_forks_repo_forks_event_min_datetime": "2020-12-25T19:22:40.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-25T19:22:40.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 33.3114754098, "max_line_length": 161, "alphanum_fraction": 0.7012795276}
import {
    alphabetChars,
    charFrequencyInNumber,
    countFrequencyLetters,
    numberToWord,
    messageFirstPart,
    messageSecondPart,
    kConstantFirstMillion,
    messageSecondPartSingle,
    noPostscriptAvailable
} from '../util/utilities';

const postscriptConstantFrequencies = countFrequencyLetters(messageFirstPart + messageSecondPart);

export const certifyMessage = (message, certifyingChar) => {
    const messageFrequencies = countFrequencyLetters(message);
    return certifyingChar === 'all'
        ? certifyMessageAllChars(messageFrequencies)
        : certifyMessageSingleChar(messageFrequencies, certifyingChar);
};

const certifyMessageSingleChar = (messageFrequencies, certifyingChar) => {
    const messageCharFrequency =
        certifyingChar in messageFrequencies ? messageFrequencies[certifyingChar] : 0;
    const postscriptConstantFrequency =
        certifyingChar in postscriptConstantFrequencies
            ? postscriptConstantFrequencies[certifyingChar]
            : 0;
    const messageCharFreq = messageCharFrequency + postscriptConstantFrequency + 1; // Note that it always contains at least the letter at the end of the message

    const bound = kConstantFirstMillion + messageCharFreq;

    let minValidFreq = messageCharFreq;
    for (
        ;
        minValidFreq < bound &&
        charFrequencyInNumber(certifyingChar, minValidFreq) + messageCharFreq !== minValidFreq;
        minValidFreq++
    );

    if (minValidFreq < bound) {
        return (
            messageFirstPart +
            numberToWord(minValidFreq) +
            (minValidFreq > 1 ? messageSecondPart : messageSecondPartSingle) +
            certifyingChar
        );
    } else {
        return noPostscriptAvailable;
    }
};

// TODO: Implement official certification (all chars)
// eslint-disable-next-line
const certifyMessageAllChars = (messageFrequencies) => {
    return (
        messageFirstPart +
        alphabetChars.map((char) => ' ' + char.toUpperCase() + messageSecondPart + char)
    );
};
