flitbit / diff

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
MIT License
2.99k stars 213 forks source link

Change Addition #88

Closed suni-masuno closed 7 years ago

suni-masuno commented 7 years ago

Feature Request: It seems reasonable (and fits my case) to take an array of changes and sum them into one final change. With some moderate effort I could (and might) write this myself, but it seems like a natural fit for the project itself. My inclination is something like sumChanges([change1,change2,change3...changen])~~>totalChange so that I can look back and say what all has happened over a span of time.

Feature Request 2: It also seems desirable to extend apply change into applyChanges([...]) which takes an array of changes to apply to the origin (an ordered array that is).

Do these have downsides I'm missing? Beyond the fact that I'm a random person on the internet asking for things ^_^

Thanks for he great tool by the way, if you do or do not take the request.

flitbit commented 7 years ago

1) The default is an array of changes or undefined if there are none. Doesn't that already provide the total changes?

2) Code below: you're not alone on this one - I do it all the time:

function applyChanges(target, changes) {
  return changes.reduce(
    (acc, change) => {
      diff.applyChange(acc, true, change);
      return acc;
    },
    target
  );
}
flitbit commented 7 years ago

Take a look at example/issue-88.js - let me know if you still really need it as part of the package.