Created
November 2, 2017 18:06
-
-
Save swashcap/a0e8ad766cc1359cdb94c67b187f47be 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
const _ = require('lodash') | |
const data = require('./wat.json') | |
const targets = [ | |
'drydock', | |
'handlebars', | |
'minimist', | |
'node-uuid', | |
'open', | |
'prettyjson' | |
] | |
const mapReduce = (collection, map, reduce) => _.reduce( | |
collection, | |
(result, value, key) => { | |
if (reduce(value, key)) { | |
result[key] = map(value, key) | |
} | |
return result | |
}, | |
Array.isArray(collection) ? [] : {} | |
) | |
const recursivePick = (value, key) => { | |
if (typeof value === 'object') { | |
const newValue = key === 'dependencies' ? | |
value : // dependencies leaf | |
_.pick(value, ['dependencies', 'version']) // single dependency leaf | |
return _.mapValues(newValue, recursivePick) | |
} | |
return value | |
} | |
console.log(JSON.stringify( | |
mapReduce( | |
data.dependencies, | |
recursivePick, | |
(value, key) => ~targets.indexOf(key) | |
), | |
undefined, | |
2 | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment