Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active August 29, 2015 14:05
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 AutoSponge/7f08954d7f319fec652f to your computer and use it in GitHub Desktop.
Save AutoSponge/7f08954d7f319fec652f to your computer and use it in GitHub Desktop.
dsl for copying values from one object to another
function copy( prop ) {
var receiver, target;
function bindFrom( obj ) {
target = obj;
return copyProp();
}
function bindTo( obj ) {
receiver = obj;
return copyProp();
}
function copyProp() {
if ( target && receiver ) {
if ( prop in target ) {
receiver[prop] = target[prop];
}
return receiver;
}
return {
from: bindFrom,
to: bindTo
};
}
return copyProp();
};
var result = {id:1, foo: 'bar', baz: null};
var myObj = ['foo', 'baz']
.map( copy )
.reduce( function ( obj, copyProp ) {
return copyProp.from( result ).to( obj );
}, {} );
//=> {foo: "bar", baz: null}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment