Skip to content

Instantly share code, notes, and snippets.

@jmbauguess
Created September 14, 2015 17:53
Show Gist options
  • Save jmbauguess/c19bb5a9cc14ed77932f to your computer and use it in GitHub Desktop.
Save jmbauguess/c19bb5a9cc14ed77932f to your computer and use it in GitHub Desktop.
A script include to reverse the conditions of an encoded query
/**
* @description Reverses an encoded query to provide the oppposite of what it would normally provide
* @namespace
* @type {Class}
*/
var EncodedQueryReversal = Class.create();
EncodedQueryReversal.prototype = {
/**
* @description Contains objects of conditions and what to replace them with
* @type {Array}
*/
CONDITIONS: [
{'condition' : '!=', 'replacement' : '='},
{'condition' : '=', 'replacement' : '!='},
{'condition' : 'NOT LIKE', 'replacement' : 'LIKE'},
{'condition' : 'LIKE', 'replacement' : 'NOT LIKE'},
{'condition' : 'ISNOTEMPTY', 'replacement' : 'ISEMPTY'},
{'condition' : 'ISEMPTY', 'replacement' : 'ISNOTEMPTY'},
{'condition' : '<=', 'replacement' : '>='},
{'condition' : '>=', 'replacement' : '<='},
{'condition' : '<', 'replacement' : '>'},
{'condition' : '>', 'replacement' : '<'},
{'condition' : 'NOT IN', 'replacement' : 'IN'},
{'condition' : 'IN', 'replacement' : 'NOT IN'},
{'condition' : 'NSAMEAS', 'replacement' : 'SAMEAS'},
{'condition' : 'SAMEAS', 'replacement' : 'NSAMEAS'},
{'condition' : 'NOTON', 'replacement' : 'ON'},
{'condition' : 'ON', 'replacement' : 'NOTON'},
{'condition' : 'RELATIVEGE', 'replacement' : 'RELATIVELE'},
{'condition' : 'RELATIVELE', 'replacement' : 'RELATIVEGE'},
{'condition' : 'RELATIVEGT', 'replacement' : 'RELATIVELT'},
{'condition' : 'RELATIVELT', 'replacement' : 'RELATIVEGT'},
{'condition' : 'RELATIVEEE', 'replacement' : 'special'},
{'condition' : 'BETWEEN', 'replacement': 'special'}
],
/**
* @description Accepts an encoded query string and converts it into an array
* @param {String} string An encoded query string
* @return {Array} An array with each query piece as an array member
*/
splitCondition: function(string) {
var array = string.split('^');
return array;
},
/**
* @description Main method - Reverses the conditions in an encoded query string
* @param {String} string An encoded query string
* @return {String} The opposite of the encoded query string
*/
process: function(string) {
var array = this.splitCondition(string),
item;
for (item in array) {
if (array.hasOwnProperty(item)) {
array[item] = this.swapConditions(array[item]);
}
}
return array.join("^");
},
/**
* @description Runs through a map of conditions and replaces a condition with its opposite, if found
* @param {String} string A piece of an encoded query
* @return {String} The opposite of that query
* @example
* swapConditions('short_descriptionLIKESomething'); //returns 'short_descriptionNOT LIKESomething'
*/
swapConditions: function(string) {
var map = this.CONDITIONS,
item;
for (item in map) {
if (map.hasOwnProperty(item)) {
if (string.search(map[item].condition) > -1) {
return this.handleSwap(string, map[item]);
}
}
}
return string;
},
/**
* @description Delegates the swap to different functions based on the condition
* @param {String} string An encoded query condition
* @param {Object} item An object containing information about the condition
* @return {String} The opposite of the query condition
*/
handleSwap: function(string, item) {
if (item.condition === 'BETWEEN') {
return this.handleBetween(string);
} else if (item.condition === 'RELATIVEEE') {
return this.handleRelativeOn(string);
} else {
return string.replace(item.condition, item.replacement);
}
},
/**
* @description Handles the case for between encoded queries
* @param {String} string A piece of an encoded query
* @return {String} The opposite of that query - The opposite of BETWEEN is after the first parameter and before the second
* @example
* var eqr = new EncodedQueryReversal();
* var str = 'sys_created_onBETWEENjavascript:gs.daysAgoStart(1)@javascript:gs.daysAgoEnd(0)';
* eqr.process(str); // returns 'sys_created_on>javascript:gs.daysAgoStart(1)^ORsys_created_on<javascript:gs.daysAgoEnd(0)'
*/
handleBetween: function(string) {
var fieldName = string.match(/(.*)BETWEEN/)[1],
array = string.split('@');
array[0] = array[0].replace(/BETWEEN/, '>');
array[1] = "OR" + fieldName + "<" + array[1];
return array.join("^");
},
/**
* @description Handles the case for relative on encoded queries
* @param {String} string A piece of an encoded query
* @return {String} The opposite of that query - The opposite of RELATIVE ON depends on the final condition - ago or from now/ahead
* @example
* var eqr = new EncodedQueryReversal();
* var str = 'sys_created_onRELATIVEEE@hour@ahead@1';
* eqr.process(str); // returns 'sys_created_onRELATIVEEE@hour@ago@1'
*/
handleRelativeOn: function(string) {
if (string.indexOf('ago') > -1) {
return string.replace('ago', 'ahead');
} else {
return string.replace('ahead', 'ago');
}
},
'type' : 'EncodedQueryReversal'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment