Parsing a string with specific pattern into json
I could have criticized the most lines of the initial approach in terms of naming, relations, structuring and performance, but I believe it should be completely rewritten.



The optimized approach:



const input = "status:all,applied_date:2019-04-15--to--2019-04-15,screen_status:SR|NS";
const dateSep = /--to--/;

/**
 * Parse input string to build JSON representing 
   a rule-set of AND clauses for SQL query.
 * The return value is JSON output.
 * @param {string} str - input string
 * @param {string} [entry_sep] - entry separator, defaults to ","
 */
function strToAndClause(str, entry_sep = ",") {
    let entries = str.split(entry_sep);
    let output = { and: {} },
        and_clause = output.and;
        
    entries.forEach((entry) =&gt; {
        let [key, val] = entry.split(":"), rule = {};
        key = key.replace("_", ".");
        and_clause[key] = rule;
        
        if (dateSep.test(val)) {
            rule.between = val.split(dateSep);
        } else if (val.indexOf("|") !== -1) {
            rule.inq = val.split("|");
        } else {
            rule.eq = val;
        }
    });
    return JSON.stringify(output);
}

console.log(strToAndClause(input));