Created
May 11, 2011 15:21
-
-
Save mafintosh/966657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sanitizer = function(valids, unsettable) { | |
var compile = function(valid, value) { | |
var fn = Object.prototype.toString.call(value) === '[object RegExp]' ? | |
function(query, set, unset) { | |
if (query[valid] && value.test(query[valid])) { | |
set[valid] = query[valid]; | |
} | |
} : | |
function(query, set, unset) { | |
if (valid in query) { | |
set[valid] = query[valid]; | |
} | |
}; | |
if (unsettable) { | |
return function(query, set, unset) { | |
if (query[valid] in {'undefined':1, 'null':1}) { | |
unset[valid] = 1; | |
} else { | |
fn(query, set, unset); | |
} | |
}; | |
} | |
return fn; | |
} | |
var fns = []; | |
for (var i in valids) { | |
fns.push(compile(i, valids[i])); | |
} | |
return function(query) { | |
var set = {}; | |
var unset = {}; | |
fns.forEach(function(fn) { | |
fn(query, set, unset); | |
}); | |
return {$set:set, $unset:unset}; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment