Skip to content

Instantly share code, notes, and snippets.

@johnsmith17th
Created April 30, 2013 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnsmith17th/5489033 to your computer and use it in GitHub Desktop.
Save johnsmith17th/5489033 to your computer and use it in GitHub Desktop.
Get sorting of query.
/**
* Get sorting of query.
*
* @param {HttpRequest} req
* @param {Object} options
* - {Boolean} [customizable]
* whether the sorting is customizable
* i.e. can be pass from query string with the key $sort
* e.g. $sort=id,-key
* - {String} [defaults]
* defined default sorting of query
* e.g. 'id -key'
* @returns {String} sorting part for query
*/
function __sorting(req, options) {
var result = null,
opt = options || {};
// customizable
if (opt.customizable) {
result = utils.param(req, '$sort', function(value) {
return value ? value.replace(/\,/g, ' ') : null;
});
};
// default
if (!result && opt.defaults) {
result = opt.defaults;
};
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment