Created
December 4, 2020 02:24
-
-
Save dgtlmonk/5d0063d49925a56d89f5feca58563a8d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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