Skip to content

Commit be61690

Browse files
committed
Update documentation
1 parent 5002d98 commit be61690

File tree

1 file changed

+199
-61
lines changed

1 file changed

+199
-61
lines changed

docs/Language-Definition.md

Lines changed: 199 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
</tr>
5454
</table>
5555

56-
5756
## Operators
5857

5958
<table>
@@ -126,8 +125,8 @@ foo matches "^[A-Z].*"
126125
### Membership Operator
127126

128127
Fields of structs and items of maps can be accessed with `.` operator
129-
or `[]` operator. Elements of arrays and slices can be accessed with
130-
`[]` operator. Negative indices are supported with `-1` being
128+
or `[]` operator. Elements of arrays and slices can be accessed with
129+
`[]` operator. Negative indices are supported with `-1` being
131130
the last element.
132131

133132
The `in` operator can be used to check if an item is in an array or a map.
@@ -169,7 +168,6 @@ array[3:] == [4, 5]
169168
array[:] == array
170169
```
171170

172-
173171
## Built-in Functions
174172

175173
### all(array, predicate)
@@ -238,122 +236,262 @@ int("123") == 123
238236

239237
Returns the float value of a number or a string.
240238

241-
### string()
239+
### string(v)
242240

243-
todo
241+
Converts the given value `v` into a string representation.
244242

245-
### trim()
243+
```expr
244+
string(123) == "123"
245+
```
246246

247-
todo
247+
### trim(v[, chars])
248248

249-
### trimPrefix()
249+
Removes white spaces from both ends of a string `v`.
250+
If the optional `chars` argument is given, it is a string specifying the set of characters to be removed.
250251

251-
todo
252+
```expr
253+
trim(" Hello ") == "Hello"
254+
trim("__Hello__", "_") == "Hello"
255+
```
252256

253-
### trimSuffix()
257+
### trimPrefix(v, prefix)
254258

255-
todo
259+
Removes the specified prefix from the string `v` if it starts with that prefix.
256260

257-
### upper()
261+
```expr
262+
trimPrefix("HelloWorld", "Hello") == "World"
263+
```
258264

259-
todo
265+
### trimSuffix(v, suffix)
260266

261-
### lower()
267+
Removes the specified suffix from the string `v` if it ends with that suffix.
262268

263-
todo
269+
```expr
270+
trimSuffix("HelloWorld", "World") == "Hello"
271+
```
264272

265-
### split()
273+
### upper(v)
266274

267-
todo
275+
Converts all the characters in string `v` to uppercase.
268276

269-
### splitN()
277+
```expr
278+
upper("hello") == "HELLO"
279+
```
270280

271-
todo
281+
### lower(v)
272282

273-
### splitAfter()
283+
Converts all the characters in string `v` to lowercase.
274284

275-
todo
285+
```expr
286+
lower("HELLO") == "hello"
287+
```
276288

277-
### splitAfterN()
289+
### split(v, delimiter)
278290

279-
todo
291+
Splits the string `v` at each instance of the delimiter and returns an array of substrings.
280292

281-
### replace()
293+
```expr
294+
split("apple,orange,grape", ",") == ["apple", "orange", "grape"]
295+
```
282296

283-
todo
297+
### splitN(v, delimiter, n)
284298

285-
### repeat()
299+
Splits the string `v` at each instance of the delimiter but limits the result to `n` substrings.
286300

287-
todo
301+
```expr
302+
splitN("apple,orange,grape", ",", 2) == ["apple", "orange,grape"]
303+
```
288304

289-
### join()
305+
### splitAfter(v, delimiter)
290306

291-
todo
307+
Splits the string `v` after each instance of the delimiter.
292308

293-
### indexOf()
309+
```expr
310+
splitAfter("apple,orange,grape", ",") == ["apple,", "orange,", "grape"]
311+
```
294312

295-
todo
313+
### splitAfterN(v, delimiter, n)
296314

297-
### lastIndexOf()
315+
Splits the string `v` after each instance of the delimiter but limits the result to `n` substrings.
298316

299-
todo
317+
```expr
318+
splitAfterN("apple,orange,grape", ",", 2) == ["apple,", "orange,grape"]
319+
```
300320

301-
### hasPrefix()
321+
### replace(v, old, new)
302322

303-
todo
323+
Replaces all occurrences of `old` in string `v` with `new`.
304324

305-
### hasSuffix()
325+
```expr
326+
replace("Hello World", "World", "Universe") == "Hello Universe"
327+
```
306328

307-
todo
329+
### repeat(v, n)
308330

309-
### max()
331+
Repeats the string `v` `n` times.
310332

311-
todo
333+
```expr
334+
repeat("Hi", 3) == "HiHiHi"
335+
```
336+
337+
### join(v, delimiter)
338+
339+
Joins an array of strings `v` into a single string with the given delimiter.
340+
341+
```expr
342+
join(["apple", "orange", "grape"], ",") == "apple,orange,grape"
343+
```
312344

313-
### min()
345+
### indexOf(v, substring)
314346

315-
todo
347+
Returns the index of the first occurrence of the substring in string `v` or -1 if not found.
316348

317-
### toJSON()
349+
```expr
350+
indexOf("apple pie", "pie") == 6
351+
```
318352

319-
todo
353+
### lastIndexOf(v, substring)
320354

321-
### fromJSON()
355+
Returns the index of the last occurrence of the substring in string `v` or -1 if not found.
322356

323-
todo
357+
```expr
358+
lastIndexOf("apple pie apple", "apple") == 10
359+
```
324360

325-
### toBase64()
361+
### hasPrefix(v, prefix)
326362

327-
todo
363+
Returns `true` if string `v` starts with the given prefix.
328364

329-
### fromBase64()
365+
```expr
366+
hasPrefix("HelloWorld", "Hello") == true
367+
```
368+
369+
### hasSuffix(v, suffix)
370+
371+
Returns `true` if string `v` ends with the given suffix.
372+
373+
```expr
374+
hasSuffix("HelloWorld", "World") == true
375+
```
376+
377+
### max(v1, v2)
378+
379+
Returns the maximum of the two values `v1` and `v2`.
380+
381+
```expr
382+
max(5, 7) == 7
383+
```
330384

331-
todo
385+
### min(v1, v2)
386+
387+
Returns the minimum of the two values `v1` and `v2`.
388+
389+
```expr
390+
min(5, 7) == 5
391+
```
392+
393+
### toJSON(v)
394+
395+
Converts the given value `v` to its JSON string representation.
396+
397+
```expr
398+
toJSON({"name": "John", "age": 30})
399+
```
400+
401+
### fromJSON(v)
402+
403+
Parses the given JSON string `v` and returns the corresponding value.
404+
405+
```expr
406+
fromJSON('{"name": "John", "age": 30}')
407+
```
408+
409+
### toBase64(v)
410+
411+
Encodes the string `v` into Base64 format.
412+
413+
```expr
414+
toBase64("Hello World") == "SGVsbG8gV29ybGQ="
415+
```
416+
417+
### fromBase64(v)
418+
419+
Decodes the Base64 encoded string `v` back to its original form.
420+
421+
```expr
422+
fromBase64("SGVsbG8gV29ybGQ=") == "Hello World"
423+
```
332424

333425
### now()
334426

335-
todo
427+
Returns the current date and time.
428+
429+
```expr
430+
createdAt > now() - duration(1h)
431+
```
432+
433+
### duration(v)
434+
435+
Returns [time.Duration](https://pkg.go.dev/time#Duration) value of the given string `v`.
336436

337-
### duration()
437+
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
338438

339-
todo
439+
```expr
440+
duration("1h").Seconds() == 3600
441+
```
442+
443+
### date(v[, format, timezone])
340444

341-
### date()
445+
Converts the given value `v` into a date representation.
342446

343-
todo
447+
If the optional `format` argument is given, it is a string specifying the format of the date.
448+
The format string uses the same formatting rules as the standard
449+
Go [time package](https://pkg.go.dev/time#pkg-constants).
344450

345-
### first()
451+
If the optional `timezone` argument is given, it is a string specifying the timezone of the date.
346452

347-
todo
453+
If the `format` argument is not given, the `v` argument must be in one of the following formats:
348454

349-
### last()
455+
- 2006-01-02
456+
- 15:04:05
457+
- 2006-01-02 15:04:05
458+
- RFC3339
459+
- RFC822,
460+
- RFC850,
461+
- RFC1123,
350462

351-
todo
463+
```expr
464+
date("2023-08-14")
465+
date("15:04:05")
466+
date("2023-08-14T00:00:00Z")
467+
date("2023-08-14 00:00:00", "2006-01-02 15:04:05", "Europe/Zurich")
468+
```
352469

353-
### get()
470+
### first(v)
354471

355-
todo
472+
Returns the first element from an array `v`. If the array is empty, returns `nil`.
356473

474+
```expr
475+
first([1, 2, 3]) == 1
476+
```
477+
478+
### last(v)
479+
480+
Returns the last element from an array `v`. If the array is empty, returns `nil`.
481+
482+
```expr
483+
last([1, 2, 3]) == 3
484+
```
485+
486+
### get(v, index)
487+
488+
Retrieves the element at the specified index from an array or map `v`. If the index is out of range, returns `nil`.
489+
Or the key does not exist, returns `nil`.
490+
491+
```expr
492+
get([1, 2, 3], 1) == 2
493+
get({"name": "John", "age": 30}, "name") == "John"
494+
```
357495

358496
## Predicate
359497

0 commit comments

Comments
 (0)