Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Created June 11, 2020 14:00
Show Gist options
  • Save G33kDude/b12aae376d3f9dbdd857e1d92b166dc5 to your computer and use it in GitHub Desktop.
Save G33kDude/b12aae376d3f9dbdd857e1d92b166dc5 to your computer and use it in GitHub Desktop.
Print(obj, quote:=False, end:="`n")
{
static cout:=FileOpen("*", "w")
, escapes := [["``", "``" "``"], ["""", """"""], ["`b", "``b"]
, ["`f", "``f"], ["`r", "``r"], ["`n", "``n"], ["`t", "``t"]]
if IsObject(obj)
{
for k in obj
is_array := k == A_Index
until !is_array
cout.Write(is_array ? "[" : "{")
for k, v in obj
{
cout.Write(A_Index > 1 ? ", " : "")
is_array ? _ : Print(k, 1, "") cout.Write(": ")
Print(v, 1, "")
}
return cout.Write(( is_array ? "]" : "}") end), end ? cout.__Handle : _
}
if (!quote || ObjGetCapacity([obj], 1) == "")
return cout.Write(obj . end), end ? cout.__Handle : _
for k, v in escapes
obj := StrReplace(obj, v[1], v[2])
while RegExMatch(obj, "O)[^\\x20-\\x7e]", m)
obj := StrReplace(obj, m[0], Format("\\u{:04X}", Ord(m[0])))
return cout.Write("""" obj """" . end), end ? cout.__Handle : _
}
@a-raccoon
Copy link

Added to my Libs. 👍

; Print() by GeekDude 2020
; Explodes the contents of an object array into a printable string.
; https://gist.github.com/G33kDude/b12aae376d3f9dbdd857e1d92b166dc5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment