1 |
|
2 | var util = require('util')
|
3 | , deep = require('..')
|
4 | ;
|
5 |
|
6 | function duckWalk() {
|
7 | util.log('right step, left-step, waddle');
|
8 | }
|
9 |
|
10 | function quadrapedWalk() {
|
11 | util.log('right hind-step, right fore-step, left hind-step, left fore-step');
|
12 | }
|
13 |
|
14 | var duck = {
|
15 | legs: 2,
|
16 | walk: duckWalk
|
17 | };
|
18 |
|
19 | var dog = {
|
20 | legs: 4,
|
21 | walk: quadrapedWalk
|
22 | };
|
23 |
|
24 | var diff = deep.diff(duck, dog);
|
25 |
|
26 |
|
27 | util.log('Differences:\r\n' + util.inspect(diff, false, 9));
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var observed = [];
|
33 | deep.observableDiff(duck, dog, function (d) {
|
34 | if (d && d.lhs && typeof d.lhs !== 'function') {
|
35 | observed.push(d);
|
36 | }
|
37 | });
|
38 |
|
39 | util.log('Observed without recording functions:\r\n' + util.inspect(observed, false, 9));
|
40 |
|
41 | util.log(util.inspect(dog, false, 9) + ' walking: ');
|
42 | dog.walk();
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | deep.observableDiff(dog, duck, function (d) {
|
49 | deep.applyChange(dog, duck, d);
|
50 | });
|
51 |
|
52 | util.log(util.inspect(dog, false, 9) + ' walking: ');
|
53 | dog.walk();
|
54 |
|
55 |
|
56 | if (deep.diff(duck, dog)) {
|
57 | util.log("Ooops, that prior statement seems to be wrong! (but it won't be)");
|
58 | }
|
59 |
|
60 |
|
61 | dog.walk = function duckWalk() {
|
62 | util.log('right step, left-step, waddle');
|
63 | };
|
64 |
|
65 | if (diff = deep.diff(duck, dog)) {
|
66 |
|
67 | util.log('Hrmm, the dog walks differently: ' + util.inspect(diff, false, 9));
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 | observed = [];
|
73 | deep.observableDiff(duck, dog, function (d) {
|
74 |
|
75 | if (d && typeof d.lhs === 'function' && typeof d.rhs === 'function') {
|
76 | var leftFnText = d.lhs.toString();
|
77 | var rightFnText = d.rhs.toString();
|
78 | if (leftFnText !== rightFnText) {
|
79 | observed.push(d);
|
80 | }
|
81 | } else {
|
82 | observed.push(d);
|
83 | }
|
84 | });
|
85 |
|
86 | if (observed.length === 0) {
|
87 | util.log('Yay!, we detected that the walk functions are equivalent');
|
88 | } |
\ | No newline at end of file |