JSFiddle - React, Tailwind, and code Playground
by kentaromiura
JavaScript
String.implement('toJSONCPL', function() {
var statement = this.split('AND');
var translate = {
'<': '$lt',
'>': '$gt',
'=': '$eq'
};
for (var i = 0, max = statement.length; i < max; i++) {
console.dir(statement[i])
statement[i] = statement[i].replace(/(.+)([<=>])(.+)/g, function() {
//console.dir(arguments)
return ['"',arguments[1].replace(/ /g,''),'":{"', translate[arguments[2]], '":',arguments[3],"}"].join('');
});
}
return ['{', statement.join(','), '}'].join('');
});
//"somekey < 10 AND another key > 5" =>
//{"somekey":{"$lt":10},"anotherkey":{"$gt":5}}
alert("somekey < 10 AND anotherkey > 5 AND jiggly = puff".toJSONCPL());