zhangnaisong
2023-08-05 24d66c8d82b628a06e93dbb1abfea2049b3d45ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
 
var test = require('tape');
var traverse = require('../');
var deepEqual = require('./lib/deep_equal');
var util = require('util');
 
test('circular', function (t) {
    t.plan(1);
 
    var obj = { x: 3 };
    obj.y = obj;
    traverse(obj).forEach(function () {
        if (this.path.join('') === 'y') {
            t.equal(
                util.inspect(this.circular.node),
                util.inspect(obj)
            );
        }
    });
});
 
test('deepCirc', function (t) {
    t.plan(2);
    var obj = { x: [1, 2, 3], y: [4, 5] };
    obj.y[2] = obj;
 
    traverse(obj).forEach(function () {
        if (this.circular) {
            t.deepEqual(this.circular.path, []);
            t.deepEqual(this.path, ['y', '2']);
        }
    });
});
 
test('doubleCirc', function (t) {
    var obj = { x: [1, 2, 3], y: [4, 5] };
    obj.y[2] = obj;
    obj.x.push(obj.y);
 
    var circs = [];
    traverse(obj).forEach(function (x) {
        if (this.circular) {
            circs.push({ circ: this.circular, self: this, node: x });
        }
    });
 
    t.deepEqual(circs[0].self.path, ['x', '3', '2']);
    t.deepEqual(circs[0].circ.path, []);
 
    t.deepEqual(circs[1].self.path, ['y', '2']);
    t.deepEqual(circs[1].circ.path, []);
 
    t.deepEqual(circs.length, 2);
    t.end();
});
 
test('circDubForEach', function (t) {
    var obj = { x: [1, 2, 3], y: [4, 5] };
    obj.y[2] = obj;
    obj.x.push(obj.y);
 
    traverse(obj).forEach(function () {
        if (this.circular) { this.update('...'); }
    });
 
    t.same(obj, { x: [1, 2, 3, [4, 5, '...']], y: [4, 5, '...'] });
    t.end();
});
 
test('circDubMap', function (t) {
    var obj = { x: [1, 2, 3], y: [4, 5] };
    obj.y[2] = obj;
    obj.x.push(obj.y);
 
    var c = traverse(obj).map(function () {
        if (this.circular) {
            this.update('...');
        }
    });
 
    t.same(c, { x: [1, 2, 3, [4, 5, '...']], y: [4, 5, '...'] });
    t.end();
});
 
test('circClone', function (t) {
    var obj = { x: [1, 2, 3], y: [4, 5] };
    obj.y[2] = obj;
    obj.x.push(obj.y);
 
    var clone = traverse.clone(obj);
    t.ok(obj !== clone);
 
    t.ok(clone.y[2] === clone);
    t.ok(clone.y[2] !== obj);
    t.ok(clone.x[3][2] === clone);
    t.ok(clone.x[3][2] !== obj);
    t.same(clone.x.slice(0, 3), [1, 2, 3]);
    t.same(clone.y.slice(0, 2), [4, 5]);
    t.end();
});
 
test('circMapScrub', function (t) {
    var obj = { a: 1, b: 2 };
    obj.c = obj;
 
    var scrubbed = traverse(obj).map(function () {
        if (this.circular) { this.remove(); }
    });
    t.same(
        Object.keys(scrubbed).sort(),
        ['a', 'b']
    );
    t.ok(deepEqual(scrubbed, { a: 1, b: 2 }));
 
    t.equal(obj.c, obj);
    t.end();
});