rebase.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. var nil = {}
  3. var owns = ({}).hasOwnProperty
  4. function rebase(result, parent, delta) {
  5. var key, current, previous, update
  6. for (key in parent) {
  7. if (owns.call(parent, key)) {
  8. previous = parent[key]
  9. update = owns.call(delta, key) ? delta[key] : nil
  10. if (previous === null) continue
  11. else if (previous === void(0)) continue
  12. else if (update === null) continue
  13. else if (update === void(0)) continue
  14. else result[key] = previous
  15. }
  16. }
  17. for (key in delta) {
  18. if (owns.call(delta, key)) {
  19. update = delta[key]
  20. current = owns.call(result, key) ? result[key] : nil
  21. if (current === update) continue
  22. else if (update === null) continue
  23. else if (update === void(0)) continue
  24. else if (current === nil) result[key] = update
  25. else if (typeof(update) !== "object") result[key] = update
  26. else if (typeof(current) !== "object") result[key] = update
  27. else result[key]= rebase({}, current, update)
  28. }
  29. }
  30. return result
  31. }
  32. module.exports = rebase