Skip to content

Instantly share code, notes, and snippets.

@jellyninjadev
Created May 23, 2020 12:24
Show Gist options
  • Save jellyninjadev/af1eb7bd53246dfd10b599ef57fd1ca9 to your computer and use it in GitHub Desktop.
Save jellyninjadev/af1eb7bd53246dfd10b599ef57fd1ca9 to your computer and use it in GitHub Desktop.
// can run in https://www.typescriptlang.org/play
type Payload = {
results?: {
aggregation?: {
pie_aggregation?: {
items?: {
buckets: [{
key: string
filtered: {
amount_stats: {sum: number}
}}]
}
}
}
}
}
const experiences = [{id: '1', name: 'name'}]
const payload: Payload = {}
const payload2: Payload = {
results: {
aggregation: {
pie_aggregation: {
items: {
buckets: [{key: '1', filtered: {amount_stats: {sum: 3}}}]
}
}
}
}
}
const toPie = (payload: Payload) => payload.results?.aggregation?.pie_aggregation?.items?.buckets.map(bucket => {
const experience = experiences.find(experience => experience.id === bucket.key)
if (!experience) return null
return {
name: experience.name,
y: bucket?.filtered?.amount_stats.sum ?? 0
}
})
const result = toPie(payload)
const result2 = toPie(payload2)
console.log('Undefined', result)
console.log('Defined', result2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment