Skip to content

Instantly share code, notes, and snippets.

@o0101
Created September 17, 2017 11:22
Show Gist options
  • Save o0101/aadd4132212a563082d595133ce89c36 to your computer and use it in GitHub Desktop.
Save o0101/aadd4132212a563082d595133ce89c36 to your computer and use it in GitHub Desktop.
// 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