Skip to content

Instantly share code, notes, and snippets.

@dgtlmonk
Created December 4, 2020 02:24
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 dgtlmonk/5d0063d49925a56d89f5feca58563a8d to your computer and use it in GitHub Desktop.
Save dgtlmonk/5d0063d49925a56d89f5feca58563a8d to your computer and use it in GitHub Desktop.
type Booking = {
id: number,
name: string
}
type BookingAndShit = {
selected?:boolean
}
type TNode = {
node: any
}
type TEdge = {
edges: TNode[]
}
interface INormalizeDataFromEdge<M> {
source: TEdge
metaData?: M
}
const data: TEdge = {
edges: [
{
node: {
id: 100
},
},
{
node: {
id: 101
},
},
{
node: {
id: 102
},
}
]
}
function normalizeDataFromEdge<T,M = unknown | null>({source, metaData}: INormalizeDataFromEdge<M>):T[] {
if (!source.edges) {
return [] as T[]
}
const nodes = source.edges.map(edge => {
return { ...edge.node, ...metaData}
})
return nodes as T[]
}
console.log(normalizeDataFromEdge<Booking>(
{
source: { edges: [{node: {a:1}}]}
}))
console.log(normalizeDataFromEdge<Booking,BookingAndShit>({ source: data, metaData: {selected: true}} ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment