Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Created May 11, 2011 15:21
Show Gist options
  • Save mafintosh/966657 to your computer and use it in GitHub Desktop.
Save mafintosh/966657 to your computer and use it in GitHub Desktop.
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