example1.js 905 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var util = require('util')
  2. , deep = require('..')
  3. ;
  4. var lhs = {
  5. name: 'my object',
  6. description: 'it\'s an object!',
  7. details: {
  8. it: 'has',
  9. an: 'array',
  10. with: ['a', 'few', 'elements']
  11. }
  12. };
  13. var rhs = {
  14. name: 'updated object',
  15. description: 'it\'s an object!',
  16. details: {
  17. it: 'has',
  18. an: 'array',
  19. with: ['a', 'few', 'more', 'elements', { than: 'before' }]
  20. }
  21. };
  22. var differences = deep.diff(lhs, rhs);
  23. // Print the differences to the console...
  24. util.log(util.inspect(differences, false, 99));
  25. deep.observableDiff(lhs, rhs, function (d) {
  26. // Apply all changes except those to the 'name' property...
  27. if (d.path.length !== 1 || d.path.join('.') !== 'name') {
  28. deep.applyChange(lhs, rhs, d);
  29. }
  30. }, function (path, key) {
  31. var p = (path && path.length) ? path.join('/') : '<no-path>'
  32. util.log('prefilter: path = ' + p + ' key = ' + key);
  33. }
  34. );
  35. console.log(util.inspect(lhs, false, 99));