Last active
August 29, 2015 14:05
-
-
Save AutoSponge/7f08954d7f319fec652f to your computer and use it in GitHub Desktop.
dsl for copying values from one object to another
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
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(); | |
}; |
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
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