graphbrainz/src/util.js

36 lines
934 B
JavaScript
Raw Normal View History

2016-08-20 05:59:32 +00:00
import util from 'util'
2016-08-08 07:54:06 +00:00
export function getFields (info) {
2016-08-20 05:59:32 +00:00
if (info.kind !== 'Field') {
info = info.fieldASTs[0]
}
const selections = info.selectionSet.selections
2016-08-08 07:54:06 +00:00
return selections.reduce((fields, selection) => {
fields[selection.name.value] = selection
return fields
}, {})
}
2016-08-20 05:59:32 +00:00
export function prettyPrint (obj, { depth = 5,
colors = true,
breakLength = 120 } = {}) {
console.log(util.inspect(obj, { depth, colors, breakLength }))
}
export function toFilteredArray (obj) {
return (Array.isArray(obj) ? obj : [obj]).filter(x => x)
}
export function extendIncludes (includes, moreIncludes) {
includes = toFilteredArray(includes)
moreIncludes = toFilteredArray(moreIncludes)
const seen = {}
return includes.concat(moreIncludes).filter(x => {
if (seen[x]) {
return false
}
seen[x] = true
return true
})
}