Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created November 2, 2017 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/a0e8ad766cc1359cdb94c67b187f47be to your computer and use it in GitHub Desktop.
Save swashcap/a0e8ad766cc1359cdb94c67b187f47be to your computer and use it in GitHub Desktop.
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