-
-
Save Eugeny/724db3c288c6047504e761246908fa42 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
// ----------- | |
// 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