Created
September 17, 2017 11:22
-
-
Save o0101/aadd4132212a563082d595133ce89c36 to your computer and use it in GitHub Desktop.
This file contains 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
// more 5 line functions | |
function project( obj, ...slots ) { | |
slots = new Set(slots); | |
const pslots = Object.keys( obj ).filter( k => slots.has(k) ); | |
return pslots.reduce( (p,k) => (k.includes('.') ? p[k] = resolve(obj,k) : p[k]=obj[k],p), {}); | |
} | |
function resolve( obj, path ) { | |
// FIXME: if key == '.' this will break. :) | |
path = path.split(/\./g); | |
while(obj !== null && obj !== undefined && path.length) { | |
obj = obj[path.shift()]; | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment