{"size": 1805, "ext": "js", "max_stars_repo_path": "src/format-filter.js", "max_stars_repo_name": "JosephClay/oquery", "max_stars_repo_stars_event_min_datetime": "2016-05-03T00:57:50.000Z", "max_stars_repo_stars_event_max_datetime": "2019-05-05T11:33:50.000Z", "max_issues_repo_path": "src/format-filter.js", "max_issues_repo_name": "JosephClay/oquery", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/format-filter.js", "max_forks_repo_name": "JosephClay/oquery", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.1206896552, "max_line_length": 78, "alphanum_fraction": 0.4975069252}
const startsWith = require('lodash/startsWith');
const endsWith = require('lodash/endsWith');
const isNumber = require('lodash/isNumber');
const operations = require('./operations.json');
const comparison = [
    '==',
    '===',
    '!=',
    '!==',
    '>',
    '>=',
    '<',
    '<='
].reduce((map, key) => {
    map[key] = 1;
    return map;
}, {});

module.exports = function(str) {
    const pieces = str.split(' ');

    return pieces.map(value => value.trim())
        .map(value => operations[value] || value)
        .map((value, idx) => {
            const previousPiece = pieces[idx - 1];
            const wasComparison = comparison[previousPiece];
            if (!wasComparison) { return value; }

            // if we ran into a comparison, this value should be formatted
            // with '' if it's not already

            // assume the fast path...that the user already wrapped
            // in single quotes
            if (startsWith(value, `'`)) { return value; }
            // or the vale is an alias
            if (startsWith(value, `@`)) { return value; }

            // the value could be in a group
            let val = value;
            let end = '';
            if (endsWith(value, `)`)) {
                val = val.substr(0, val.length - 1);
                end = ')';
            }

            // it was a number, this was a noop
            const numVal = +val;
            if (isNumber(numVal) && !isNaN(numVal)) { return value; }

            // trim double quotes if they're used
            if (startsWith(value, '"')) { val = val.substr(1, val.length); }
            if (endsWith(value, '"')) { val = val.substr(0, val.length - 1); }

            // return the correctly formatted value
            return `'${val}'${end}`;
        })
        .join(' ');
};