js_tools.anubis
1.53 KB
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
/*
* Created by PyramIDE.
* User: フランスのトトロ
* Date: 18/04/2015
* Time: 23:21
* © David RENÉ
*/
read tools/basis.anubis
read xlib/web/CXM_json.anubis
transmit xlib/extensions/json.anubis
define List(Word8)
make_JS_String
(
List(Word8) current,
List(Word8) so_far
)
=
if current is
{
[ ] then reverse(so_far),
[h . t] then
make_JS_String(t,
if h = '\'' then [ h, '\\' . so_far] //single quote
else if h = '\"' then [ h, '\\' . so_far]
else if h = '\r' then ['r', '\\' . so_far] //CR
else if h = '\n' then ['n', '\\' . so_far] //LF
else if h = '\t' then ['t', '\\' . so_far] //tab
else if h = 0x08 then ['b', '\\' . so_far] //Backspace
else
[h . so_far]
)
}.
/**
* Convert a string into certified javascript string (I hope)
*/
public define String
make_JS_String
(
String str
)
=
implode(make_JS_String(explode(str), [])).
public define String
to_JS_String
(
String str
)
=
//try to use adl version of json escape string
if load_library("json") is
{
error(err) then
"'" + make_JS_String(str) + "'"
ok(json_lib) then
to_JS_String(json_lib, str)
}.
public define String
format_js_function_call
(
String name,
List(String) args
) =
name + "(" + concat(args, ",") + ");".