-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj2str.ahk
61 lines (50 loc) · 1.62 KB
/
obj2str.ahk
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
/* ___ _ _ ____ __ _
/___\ |__ (_)___ \/ _\ |_ _ __
// // '_ \| | __) \ \| __| '__|
/ \_//| |_) | |/ __/_\ \ |_| |
\___/ |_.__// |_____\__/\__|_|
4/17/17 |__/ by errorseven
What this does:
Takes an Object/Array and turns it into a string. Maintains exact format,
can easily transfer to other scripts, useful for debugging.
Example:
OurObject := {arr:[1, 2, 3]
, text:"Hello, World"
, obj:{1:1, 2:2, 3:3, 5:5}}
String := Obj2Str(OurObject)
MsgBox % String
; String -> {arr:[1, 2, 3], obj:{1:1, 2:2, 3:3, 5:5}, text:"Hello, World"}
*/
Obj2Str(obj) {
Linear := True
While (A_Index != obj.MaxIndex()) {
if !(obj.hasKey(A_Index)) {
Linear := False
break
}
}
For e, v in obj {
if (Linear == False) {
if (IsObject(v))
r .= e ":" Obj2Str(v) ", "
else {
r .= e ":"
if v is number
r .= v ", "
else
r .= """" v """, "
}
} else {
if (IsObject(v))
r .= Obj2Str(v) ", "
else {
if v is number
r .= v ", "
else
r .= """" v """, "
}
}
}
return Linear ? "[" trim(r, ", ") "]"
: "{" trim(r, ", ") "}"
}