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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
The utpl script language supports declaring objects (dictionaries) using
either JSON or JavaScript notation.
-- Expect stdout --
{ }
{ "name": "Bob", "age": 31, "email": { "work": "bob@example.com", "private": "bob@example.org" } }
{ "banana": "yellow", "tomato": "red", "broccoli": "green" }
{ "foo": "bar", "complex key": "qrx" }
{ "foo": { "bar": true } }
-- End --
-- Testcase --
{%
// An empty object can be declared using a pair of curly brackets
empty_obj = { };
// It is also possible to use JSON notation to declare an object
json_obj = {
"name": "Bob",
"age": 31,
"email": {
"work": "bob@example.com",
"private": "bob@example.org"
}
};
// Declaring an object in JavaScript notation is supported as well
another_obj = {
banana: "yellow",
tomato: "red",
broccoli: "green"
};
// Mixing styles is allowed too
third_obj = {
foo: "bar",
"complex key": "qrx"
};
// Important caveat: when nesting objects, ensure that curly brackets
// are separated by space or newline to avoid interpretation as
// expression block tag!
nested_obj = { foo: { bar: true } }; // <-- mind the space in "} }"
// Printing (or stringifying) objects will return their JSON representation
print(empty_obj, "\n");
print(json_obj, "\n");
print(another_obj, "\n");
print(third_obj, "\n");
print(nested_obj, "\n");
%}
-- End --
Additionally, utpl implements ES6-like spread operators to allow shallow copying
of object properties into other objects.
-- Expect stdout --
{ "foo": true, "bar": false }
{ "foo": true, "bar": false, "baz": 123, "qrx": 456 }
{ "foo": false, "bar": true, "baz": 123, "qrx": 456 }
{ "foo": true, "bar": false }
{ "foo": true, "bar": false, "level2": { "baz": 123, "qrx": 456 } }
{ "foo": true, "bar": false, "0": 7, "1": 8, "2": 9 }
-- End --
-- Testcase --
{%
o1 = { foo: true, bar: false };
o2 = { baz: 123, qrx: 456 };
arr = [7, 8, 9];
print(join("\n", [
// copying one object into another
{ ...o1 },
// combining two objects
{ ...o1, ...o2 },
// copying object and override properties
{ ...o1, ...o2, foo: false, bar: true },
// default properties overwritten by spread operator
{ foo: 123, bar: 456, ...o1 },
// nested spread operators
{ ...o1, level2: { ...o2 } },
// merging array into objects
{ ...o1, ...arr }
]), "\n");
%}
-- End --
ES2015 short hand property notation is supported as well.
-- Expect stdout --
{ "a": 123, "b": true, "c": "test" }
-- End --
-- Testcase --
{%
a = 123;
b = true;
c = "test";
o = { a, b, c };
print(o, "\n");
%}
-- End --
-- Expect stderr --
Syntax error: Unexpected token
Expecting ':'
In line 2, byte 14:
` o = { "foo" };`
Near here ------^
-- End --
-- Testcase --
{%
o = { "foo" };
%}
-- End --
-- Expect stderr --
Syntax error: Invalid identifier
In line 2, byte 8:
` o = { function };`
^-- Near here
-- End --
-- Testcase --
{%
o = { function };
%}
-- End --
ES2015 computed property names are supported.
-- Expect stdout --
{ "test": true, "hello": false, "ABC": 123 }
-- End --
-- Testcase --
{%
s = "test";
o = {
[s]: true,
["he" + "llo"]: false,
[uc("abc")]: 123
};
print(o, "\n");
%}
-- End --
-- Expect stderr --
Syntax error: Expecting expression
In line 2, byte 10:
` o1 = { []: true };`
Near here --^
Syntax error: Unexpected token
Expecting ']'
In line 3, byte 14:
` o2 = { [true, false]: 123 };`
Near here ------^
-- End --
-- Testcase --
{%
o1 = { []: true };
o2 = { [true, false]: 123 };
%}
-- End --
|