Last active
November 12, 2018 14:25
-
-
Save antmdvs/b5cf3e99ace20bbfcaf7a2256fda1957 to your computer and use it in GitHub Desktop.
Product filtering
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
//Installed node modules: jquery underscore request express jade shelljs passport http sys lodash async mocha moment connect validator restify ejs ws co when helmet wrench brain mustache should backbone forever debug | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
var util = require('util'); | |
var input = ""; | |
process.stdin.on('data', function (text) { | |
input += text; | |
}); | |
process.stdin.on('end', function () { | |
const data = JSON.parse(input); | |
const filteredProducts = getFilteredProducts(data); | |
console.log(JSON.stringify(filteredProducts)); | |
}); | |
/// solution start -- 3. added edge case handling + code comments | |
const getFilteredProducts = data => { | |
// we default to empty arrays so we don't blow up if | |
// either of these are not present in the input file | |
const { products = [], exclusions = [] } = data; | |
// a product should be excluded if there exists some exclusion element | |
// such that its `key` and `value` values match any of the given product object's key-value pairs. | |
const isExcluded = product => | |
Object.entries(product).some(([key, value]) => | |
exclusions.some(excl => excl.key === key && excl.value === value) | |
); | |
const filteredProducts = products.filter(product => !isExcluded(product)); | |
return filteredProducts; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment