Skip to content

Instantly share code, notes, and snippets.

@mohamed-ali
Last active December 25, 2015 08:09
Show Gist options
  • Save mohamed-ali/6944876 to your computer and use it in GitHub Desktop.
Save mohamed-ali/6944876 to your computer and use it in GitHub Desktop.
OData filter preparator: simple, compact and easy to extend created to resolve the issue of preparing the OData filter presented in these dicussions: - http://stackoverflow.com/questions/19251441/how-to-cast-with-eq-in-odata-filtering - http://stackoverflow.com/questions/19319170/how-to-escape-single-quote-in-odata-filter-uri - http://stackoverf…
/*
* OData filter preparator:
* simple, compact and easy to extend
* copyright (c) Mohamed Ali Jamaoui 2013
*
* can be used on the fly to prepare the OData filter
* can easly be extended to handle new Edm types
* > the list of Edm types can be found here:
* > http://www.odata.org/documentation/odata-v2-documentation/overview/#6_Primitive_Data_Types
* > check the detailed description here: http://intelligea.wordpress.com/2013/10/12/how-to-format-odata-filters-values-on-the-fly-using-their-edm-types/
*/
var odataHandler = {};
odataHandler.odataFilter = function module(){
var filterList = {};
//private function for escape special characters in a string
//currently escaping in order: single quote, ampersand
function escapeString(string){
string = string.replace(/'/g,'\'\'');
string = string.replace(/&/g,'%26');
return string;
}
function exports(){}
exports.prepareFilter = function(_EdmType, _value){
return filterList[_EdmType] ? filterList[_EdmType](_value) : "Edm Type unhandled yet";
};
//add a new filter or overwrites an existing one
exports.addFilterHandler = function(_EdmType, _callbackFunction){
filterList[_EdmType] = _callbackFunction;
//allow method chaining
return this;
};
//return the list of the supported EDM types
exports.getSupportedEdmTypes = function(){
return Object.keys(filterList);
};
//adding default filter handlers;
exports.addFilterHandler("Edm.DateTime", function(_value){return "datetime'"+_value+"'";});
exports.addFilterHandler("Edm.Decimal", function(_value){return _value+"M";});
exports.addFilterHandler("Edm.Single", function(_value){return _value+"f";});
exports.addFilterHandler("Edm.Int64", function(_value){return _value+"L";});
exports.addFilterHandler("Edm.Int32", function(_value){return _value;});
exports.addFilterHandler("Edm.Int16", function(_value){return _value;});
//escape single quote if any, ampersand if any and return the string
exports.addFilterHandler("Edm.String", function(_value){return "'"+ escapeString(_value)+"'";});
return exports;
};
//How to use
var test = odataHandler.odataFilter();
//how to extend with new handler
test.addFilterHandler("Edm.Int16", function(_value){
return _value;
});
//how to prepare filter on the fly
console.log(test.prepareFilter("Edm.DateTime","1996-07-04T00:00:00"));
//result >> datetime'1996-07-04T00:00:00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment