<commit_before>'use strict';
var utils = require('../lib/utils'),
    mediaquery = require('css-mediaquery');
/*
    Routes for /start/ could be any of the following:
    * `/start/`
    * `/start/?cols=6&med=48em&lrg=60em`
    * `/start/?cols=6&sm=screen and (min-device-width: 480px)`
*/
module.exports = function (req, res, next) {

    var query = utils.extend({}, req.query),
        mq = hasMQ(query);

    //if we have columns, then normalize them into an array.
    if (query.cols) {
        query.cols = normalizeCols(query.cols);
    }

    if (mq) {
        Object.keys(mq).forEach(function (key) {
            var validMqStr = isValidMQ(mq[key]);
            if (!validMqStr) {
                //remove the media query from `query` if it's not valid.
                delete query[key];
            }
            else {
                //update the media query in `query` with the legitimate one.
                query[key] = validMqStr;
            }
        });
    }

    res.expose(query, 'app.start.query');
    res.render('start');
};

// Takes in a string input for number of columns and converts it into an array.
function normalizeCols (cols) {
    //cols will always be a string, so we can convert the string to an array of 1 or more integers.
    return cols.split(",").map(function (x) {
        return parseInt(x);
    });
}


/*
Checks to see if media queries are valid, by following these steps:
    Does the query param value parse as a media query?
        If yes? return true.
        If not, then assume it's a width value, so wrap it with "screen and (min-width: " + value + ")"
    Does it now parse as a media query?
        If yes, return true.
        If not, return false.
*/
function isValidMQ (mqStr) {
    //This regex splits up a string that contains a sequences of letters or numbers ("48em", "480px") into an array of grouped letters and numbers (["48", "em"], ["480", "px"])
    var RE_SEPARATE_NUM_LETTERS = /[a-zA-Z]+|[0-9]+/g,
        captures;
    try {
        mediaquery.parse(mqStr);
    } catch (e) {
        //invalid media query, so let's check that there's some floated value in here, and if there is, we will prepend/append some strings
        captures = mqStr.match(RE_SEPARATE_NUM_LETTERS);
        if (captures.length === 2 && parseFloat(captures[0])) {
            mqStr = 'screen and (min-width: ' + mqStr + ')';
        }

        else {
            return false;
        }
        try {
            mediaquery.parse(mqStr);
        } catch (e) {
            //still not a valid media query
            return false;
        }
    }

    return mqStr;
}

function hasMQ (obj) {
    var o = utils.extend({}, obj);
    delete o.cols;
    delete o.fonts;
    delete o.prefix;
    if (Object.getOwnPropertyNames(o).length === 0) {
        return false;
    }
    return o;
}
<commit_msg>replace `hasMQ()` with `normalizeQuery()`<commit_after>'use strict';
var utils = require('../lib/utils'),
    mediaquery = require('css-mediaquery');
/*
    Routes for /start/ could be any of the following:
    * `/start/`
    * `/start/?cols=6&med=48em&lrg=60em`
    * `/start/?cols=6&sm=screen and (min-device-width: 480px)`
*/
module.exports = function (req, res, next) {

    var query = normalizeQuery(utils.extend({}, req.query));

    //if we have columns, then normalize them into an array.
    if (query.cols) {
        query.cols = normalizeCols(query.cols);
    }

    res.expose(query, 'app.start.query');
    res.render('start');
};

// Takes in a string input for number of columns and converts it into an array.
function normalizeCols (cols) {
    //cols will always be a string, so we can convert the string to an array of 1 or more integers.
    return cols.split(",").map(function (x) {
        return parseInt(x);
    });
}


/*
Checks to see if media queries are valid, by following these steps:
    Does the query param value parse as a media query?
        If yes? return true.
        If not, then assume it's a width value, so wrap it with "screen and (min-width: " + value + ")"
    Does it now parse as a media query?
        If yes, return true.
        If not, return false.
*/
function isValidMQ (mqStr) {
    //This regex splits up a string that contains a sequences of letters or numbers ("48em", "480px") into an array of grouped letters and numbers (["48", "em"], ["480", "px"])
    var RE_SEPARATE_NUM_LETTERS = /[a-zA-Z]+|[0-9]+/g,
        captures;
    try {
        mediaquery.parse(mqStr);
    } catch (e) {
        //invalid media query, so let's check that there's some floated value in here, and if there is, we will prepend/append some strings
        captures = mqStr.match(RE_SEPARATE_NUM_LETTERS);
        if (captures.length === 2 && parseFloat(captures[0])) {
            mqStr = 'screen and (min-width: ' + mqStr + ')';
        }

        else {
            return false;
        }
        try {
            mediaquery.parse(mqStr);
        } catch (e) {
            //still not a valid media query
            return false;
        }
    }

    return mqStr;
}

/*
    This function takes in a `req.query` object, validates all the media queries within it, removes the incorrect media queries, and then returns a modified `req.query` object, with only valid values within in.
*/
function normalizeQuery (obj) {
    var query = obj,
        mq = utils.extend({}, query);
    delete mq.cols;
    delete mq.fonts;
    delete mq.prefix;

    Object.keys(mq).forEach(function (key) {
        var validMqStr = isValidMQ(mq[key]);
        if (!validMqStr) {
            //remove the media query from `query` if it's not valid.
            delete query[key];
        }
        else {
            //update the media query in `query` with the legitimate one.
            query[key] = validMqStr;
        }
    });

    return query;
}

{'fraction_non_alphanumeric': 0.12321704760268087, 'fraction_numerical': 0.008420690840350575, 'mean_word_length': 2.222591362126246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4961841', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1768, 'n_words': 616}