I have a function that has, as input, a ranges
object which may define from
, to
and limit
. Note that it might have one, two or all three of them.
I want the output to be a saneRanges
object, where if partial values are provided, the other ones are inferred.
For example if only limit
is provided, from
should be 0 and to
should be limit-1
.
This is the code I wrote during prototyping. I made sure I took zero shortcuts, as I didn't want smarty pants code make debugging harder.
But now... here I am. I have this lame code, and I want to find the bestest way to write it.
How would you rewrite this, so that it doesn't look quite so terrible?
sanitizeRanges: function( ranges, skipHardLimitOnQueries ){
var self = this
var saneRanges = {};
if( ! self.hardLimitOnQueries ) var hardLimitOnQueries = 0;
saneRanges.from = 0;
saneRanges.to = 0;
saneRanges.limit = 0;
if( typeof( ranges ) === 'object' && ranges !== null ){
// Copy values over to saneRanges
saneRanges.from = ranges.from || -1;
saneRanges.to = ranges.to || -1;
saneRanges.limit = ranges.limit || -1;
var sr = saneRanges;
// Sorry, no shortcuts here for now. Code will be optimised later
// (maybe)
// Case: Nothing is set
if( sr.from === -1 && sr.to === -1 && sr.limit === -1 ){
sr.from = 0;
sr.to = self.hardLimitOnQueries;
sr.limit = self.hardLimitOnQueries;
// Case: Only "limit" is set
// - Set "from" and "to"
} else if( sr.from === -1 && sr.to === -1 && sr.limit !== -1 ){
sr.from = 0;
sr.to = sr.limit - 1;
// Case: Only "from" is set
// - Set "to" and "limit"
} else if( sr.from !== -1 && sr.to === -1 && sr.limit === -1 ){
sr.limit = 0;
sr.to = 0;
// Case: Only "to" is set
// - Set "from" and "limit"
} else if( sr.from === -1 && sr.to !== -1 && sr.limit === -1 ){
sr.from = 0;
sr.limit = sr.to + 1;
// Case: Only "from" and "limit" are set
// - Set "to"
} else if( sr.from !== -1 && sr.to === -1 && sr.limit !== -1 ){
sr.to = sr.from + sr.limit - 1;
// Case: Only "from" and "to" are set
// - Set "limit"
} else if( sr.from !== -1 && sr.to !== -1 && sr.limit === -1 ){
sr.limit = sr.to - sr.from + 1;
// Case: Only "to" and "limit" are set
// - Set "from"
} else if( sr.from === -1 && sr.to !== -1 && sr.limit !== -1 ){
sr.from = 0;
}
// Make sure "limit" never goes over
if( sr.limit != 0 && sr.from + sr.limit - 1 > sr.to ){
sr.limit = sr.to - sr.from + 1;
}
}
// Apply hard limit on queries if required to do so. Driver implementations
// should only pass 'true' for non-cursor queries, to prevent huge toArray() on
// a million records
if( ! skipHardLimitOnQueries ){
if( self.hardLimitOnQueries && ( saneRanges.limit === 0 || sr.limit > self.hardLimitOnQueries ) ){
saneRanges.limit = self.hardLimitOnQueries;
}
}
return saneRanges;
},