Skip to content

Instantly share code, notes, and snippets.

@Eugeny
Last active February 14, 2023 15:28
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 Eugeny/724db3c288c6047504e761246908fa42 to your computer and use it in GitHub Desktop.
Save Eugeny/724db3c288c6047504e761246908fa42 to your computer and use it in GitHub Desktop.
// -----------
// Challenge 0
// -----------
// function F<T> (f: ???): Promise<T> {
// return new Promise(f)
// }
// -----------
// Challenge 1
// -----------
// export function extractPropertyByPath<V, T extends Record<string, T|V>> (obj: T, path: string): V|undefined {
function extractPropertyByPath(obj, path) {
return undefined
}
// -----------
// Tests
// -----------
const space = {
planets: {
saturn: { mass: 2 } ,
earth: { mass: 1 } ,
},
stars: [
{ name: 'Sun' },
],
}
console.assert(
extractPropertyByPath(space, "planets.saturn.mass") == 2,
'test 1'
)
console.assert(
extractPropertyByPath(space, "planets.earth.mass") == 1,
'test 2'
)
console.assert(
extractPropertyByPath(space, "planets.moon.mass") == undefined,
'undefined handling'
)
// console.assert(
// extractPropertyByPath(space, "planets.stars[0].name") == 'Sun',
// 'arrays'
// )
// -----------
// Challenge 2
// -----------
// function makeExtractor<V, T extends Record<string, T|V>> (path: string): T => V|undefined
function makeExtractor (path) {
return undefined
}
// -----------
// Tests
// -----------
const cat = {
name: 'Mittens',
color: {
rgb: { r: 60, g: 60, b: 60 } ,
}
}
const bird = {
name: 'Tweety',
color: {
rgb: { r: 255, g: 255, b: 128 } ,
}
}
// const extractor = makeExtractor('color.rgb')
// console.assert(
// extractor(cat) == cat.color.rgb,
// 'test1'
// )
// console.assert(
// extractor(bird) == bird.color.rgb,
// 'test2'
// )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment