-
Notifications
You must be signed in to change notification settings - Fork 2
/
deep_print.js
74 lines (67 loc) · 1.4 KB
/
deep_print.js
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
function deep_print(object) {
// Copyright (c) 2019, Leonid Titov, Mentions Highly Appreciated.
var level = 1;
var id_cnt = 1;
var all_old_objects = {};
print_obj(object);
for (var id in all_old_objects) {
delete all_old_objects[id].__temp_prn_id;
}
return;
//
function print_obj(o) {
if (o.__temp_prn_id === undefined) {
o.__temp_prn_id = id_cnt;
all_old_objects[id_cnt] = o;
id_cnt ++;
for (var prop in o) {
pr_lev(prop);
level ++;
if (o[prop] instanceof Array) {
print_array(o[prop]);
}
else if (o[prop] instanceof Object) {
print_obj(o[prop]);
}
else {
pr_lev(o[prop]);
}
level --;
}
}
else {
pr_lev(`recursive link to id=${o.__temp_prn_id}`);
}
}
function print_array(a) {
if (a.__temp_prn_id === undefined) {
a.__temp_prn_id = id_cnt;
all_old_objects[id_cnt] = a;
id_cnt ++;
pr_lev(`array, id=${id_cnt - 1}:`);
level ++;
a.forEach( v => {
if (v instanceof Array) {
print_array(v);
}
else if (v instanceof Object) {
print_object(v);
}
else {
pr_lev(v);
}
});
level --;
}
else {
pr_lev(`recursive link to id=${a.__temp_prn_id}`);
}
}
function pr_lev (text) {
var lev = level;
for (; lev > 0; lev--) {
printarea.innerHTML += "\t";
}
printarea.innerHTML += text + "\n";
}
}