davethecoder
davethecoder

Reputation: 3932

how to use ramda to extract changed properties from an object, when compared against original

Hopefully using ramda, I would like to take the changes made to config, and use them to update a users config, where the property/ies changed have the same old value.

After discussion. changes. I will keep a changes object and just R.merge(original, user). However I need to be able to extract a changed property and its value, so that i can merge that object, into the user, when on user config.

Example:

var original = { name:'test', setting1:1, setting2:2 };
var toChange = R.clone(original);
var userConfig {};
/// edit toChange
toChange.setting1 = 2;
/// extract setting1 and value from toChange

R.difference(original,toChange) => should be { setting1:2 };
/// but this does not work,  no matter what way round i put original 
/// and toChange, need to extract that change / diff

/// so i can then
userConfig = R.merge(userConfig, R.difference(original,toChange)); // for example

Upvotes: 0

Views: 660

Answers (1)

davethecoder
davethecoder

Reputation: 3932

Found a way to solve incase someone wants to do the same

var orig = {a: 1, b: 2, c: 3};
var edited = R.merge(R.clone(orig),{b:7});
var user = { d: 4 };

//check if left value == right value
//if not, return null
//on merge with

//wrap around reject if null

//wrap around a merge into user

user = R.merge(user, R.reject(R.isNil, 
R.mergeWith(function(l,r){
  return (R.equals(l,r)) ? null : l;
},edited,orig)));

console.log(user);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>

Upvotes: 1

Related Questions