Created
August 3, 2015 14:51
-
-
Save indexzero/6a13d94804b54faf7557 to your computer and use it in GitHub Desktop.
Simple sanitization and filtering in JavaScript
This file contains hidden or 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 filtered = { | |
bazz: true, | |
buzz: true | |
}; | |
var maps = { | |
foo: function (value, acc) { | |
return value * 10; | |
}, | |
bar: function (value, acc) { | |
return value * 20 | |
} | |
}; | |
function sanitize(obj) { | |
return Object.keys(obj).reduce(function (acc, key) { | |
if (filtered[key]) { return acc; } | |
acc[key] = maps[key] | |
? maps[key](obj[key], acc) | |
: obj[key]; | |
return acc; | |
}, {}); | |
} | |
console.log(sanitize({ | |
bazz: 100, | |
buzz: 10, | |
fizzbuzz: 50, | |
foo: 50, | |
bar: 20 | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment