forked from code4fukui/DNCL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobj2s.js
35 lines (35 loc) · 924 Bytes
/
obj2s.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
export const obj2s = (o) => {
const objs = [];
const f = (o, nest = 0) => {
if (o === null) {
return "null";
} else if (o === undefined) {
return "undefined";
} else if (typeof o == "string") {
return nest == 0 ? o : `"${o}"`;
} else if (typeof o == "number" || typeof o == "boolean") {
return o.toString();
} else {
const idx = objs.indexOf(o);
if (idx == -1) {
objs.push(o);
} else {
return "[object " + idx + "]";
}
if (Array.isArray(o)) {
const res = [];
for (const i of o) {
res.push(f(i, nest + 1));
}
return "[" + res.join(", ") + "]";
} else if (typeof o == "object") {
const res = [];
for (const name in o) {
res.push(name + ": " + f(o[name], nest + 1));
}
return "{ " + res.join(", ") + " }";
}
}
};
return f(o);
};