2019-01-24 02:39:05 +03:00
|
|
|
# Builtin Functions
|
|
|
|
|
2019-04-14 23:14:44 +03:00
|
|
|
## format
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Returns a formatted string. The first argument must be a String object. See
|
|
|
|
[this](https://github.com/d5/tengo/blob/master/docs/formatting.md) for more
|
|
|
|
details on formatting.
|
2019-04-14 23:14:44 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
a := [1, 2, 3]
|
|
|
|
s := format("Foo: %v", a) // s == "Foo: [1, 2, 3]"
|
|
|
|
```
|
|
|
|
|
2019-01-24 02:39:05 +03:00
|
|
|
## len
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Returns the number of elements if the given variable is array, string, map, or
|
|
|
|
module map.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := [1, 2, 3]
|
|
|
|
l := len(v) // l == 3
|
|
|
|
```
|
|
|
|
|
|
|
|
## copy
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Creates a copy of the given variable. `copy` function calls `Object.Copy`
|
|
|
|
interface method, which is expected to return a deep-copy of the value it holds.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v1 := [1, 2, 3]
|
|
|
|
v2 := v1
|
|
|
|
v3 := copy(v1)
|
|
|
|
v1[1] = 0
|
|
|
|
print(v2[1]) // "0"; 'v1' and 'v2' referencing the same array
|
|
|
|
print(v3[1]) // "2"; 'v3' not affected by 'v1'
|
|
|
|
```
|
|
|
|
|
|
|
|
## append
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Appends object(s) to an array (first argument) and returns a new array object.
|
|
|
|
(Like Go's `append` builtin.) Currently, this function takes array type only.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := [1]
|
|
|
|
v = append(v, 2, 3) // v == [1, 2, 3]
|
|
|
|
```
|
|
|
|
|
2020-02-19 17:02:31 +03:00
|
|
|
## delete
|
|
|
|
|
|
|
|
Deletes the element with the specified key from the map type.
|
|
|
|
First argument must be a map type and second argument must be a string type.
|
|
|
|
(Like Go's `delete` builtin except keys are always string).
|
|
|
|
`delete` returns `undefined` value if successful and it mutates given map.
|
|
|
|
|
|
|
|
```golang
|
|
|
|
v := {key: "value"}
|
|
|
|
delete(v, "key") // v == {}
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
v := {key: "value"}
|
|
|
|
delete(v, "missing") // v == {"key": "value"}
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
delete({}) // runtime error, second argument is missing
|
|
|
|
delete({}, 1) // runtime error, second argument must be a string type
|
|
|
|
```
|
|
|
|
|
2020-03-04 09:16:15 +03:00
|
|
|
## splice
|
|
|
|
|
|
|
|
Deletes and/or changes the contents of a given array and returns
|
|
|
|
deleted items as a new array. `splice` is similar to
|
|
|
|
JS `Array.prototype.splice()` except splice is a builtin function and
|
|
|
|
first argument must an array. First argument must be an array, and
|
|
|
|
if second and third arguments are provided those must be integers
|
|
|
|
otherwise runtime error is returned.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
`deleted_items := splice(array[, start[, delete_count[, item1[, item2[, ...]]]])`
|
|
|
|
|
|
|
|
```golang
|
|
|
|
v := [1, 2, 3]
|
|
|
|
items := splice(v, 0) // items == [1, 2, 3], v == []
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
v := [1, 2, 3]
|
|
|
|
items := splice(v, 1) // items == [2, 3], v == [1]
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
v := [1, 2, 3]
|
|
|
|
items := splice(v, 0, 1) // items == [1], v == [2, 3]
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
// deleting
|
|
|
|
v := ["a", "b", "c"]
|
|
|
|
items := splice(v, 1, 2) // items == ["b", "c"], v == ["a"]
|
|
|
|
// splice(v, 1, 3) or splice(v, 1, 99) has same effect for this example
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
// appending
|
|
|
|
v := ["a", "b", "c"]
|
|
|
|
items := splice(v, 3, 0, "d", "e") // items == [], v == ["a", "b", "c", "d", "e"]
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
// replacing
|
|
|
|
v := ["a", "b", "c"]
|
|
|
|
items := splice(v, 2, 1, "d") // items == ["c"], v == ["a", "b", "d"]
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
// inserting
|
|
|
|
v := ["a", "b", "c"]
|
|
|
|
items := splice(v, 0, 0, "d", "e") // items == [], v == ["d", "e", "a", "b", "c"]
|
|
|
|
```
|
|
|
|
|
|
|
|
```golang
|
|
|
|
// deleting and inserting
|
|
|
|
v := ["a", "b", "c"]
|
|
|
|
items := splice(v, 1, 1, "d", "e") // items == ["b"], v == ["a", "d", "e", "c"]
|
|
|
|
```
|
|
|
|
|
2019-01-28 05:39:45 +03:00
|
|
|
## type_name
|
|
|
|
|
|
|
|
Returns the type_name of an object.
|
|
|
|
|
|
|
|
```golang
|
|
|
|
type_name(1) // int
|
|
|
|
type_name("str") // string
|
|
|
|
type_name([1, 2, 3]) // array
|
|
|
|
```
|
|
|
|
|
2019-10-24 11:18:38 +03:00
|
|
|
## string
|
2019-01-28 05:39:45 +03:00
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Tries to convert an object to string object. See
|
|
|
|
[Runtime Types](https://github.com/d5/tengo/blob/master/docs/runtime-types.md)
|
|
|
|
for more details on type conversion.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
2019-10-24 11:18:38 +03:00
|
|
|
x := string(123) // x == "123"
|
2019-01-24 02:39:05 +03:00
|
|
|
```
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Optionally it can take the second argument, which will be returned if the first
|
|
|
|
argument cannot be converted to string. Note that the second argument does not
|
|
|
|
have to be string.
|
2019-03-22 19:27:34 +03:00
|
|
|
|
|
|
|
```golang
|
2019-10-24 11:18:38 +03:00
|
|
|
v = string(undefined, "foo") // v == "foo"
|
2020-03-04 17:53:38 +03:00
|
|
|
v = string(undefined, false) // v == false
|
2019-03-22 19:27:34 +03:00
|
|
|
```
|
|
|
|
|
2019-01-24 02:39:05 +03:00
|
|
|
## int
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Tries to convert an object to int object. See
|
|
|
|
[this](https://github.com/d5/tengo/blob/master/docs/runtime-types.md)
|
|
|
|
for more details on type conversion.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := int("123") // v == 123
|
|
|
|
```
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Optionally it can take the second argument, which will be returned if the first
|
|
|
|
argument cannot be converted to int. Note that the second argument does not have
|
|
|
|
to be int.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v = int(undefined, 10) // v == 10
|
2020-03-04 17:53:38 +03:00
|
|
|
v = int(undefined, false) // v == false
|
2019-01-24 02:39:05 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## bool
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Tries to convert an object to bool object. See
|
|
|
|
[this](https://github.com/d5/tengo/blob/master/docs/runtime-types.md) for more
|
|
|
|
details on type conversion.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := bool(1) // v == true
|
|
|
|
```
|
|
|
|
|
|
|
|
## float
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Tries to convert an object to float object. See
|
|
|
|
[this](https://github.com/d5/tengo/blob/master/docs/runtime-types.md) for more
|
|
|
|
details on type conversion.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := float("19.84") // v == 19.84
|
|
|
|
```
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Optionally it can take the second argument, which will be returned if the first
|
|
|
|
argument cannot be converted to float. Note that the second argument does not
|
|
|
|
have to be float.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v = float(undefined, 19.84) // v == 19.84
|
2020-03-04 17:53:38 +03:00
|
|
|
v = float(undefined, false) // v == false
|
2019-01-24 02:39:05 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## char
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Tries to convert an object to char object. See
|
|
|
|
[this](https://github.com/d5/tengo/blob/master/docs/runtime-types.md) for more
|
|
|
|
details on type conversion.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := char(89) // v == 'Y'
|
|
|
|
```
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Optionally it can take the second argument, which will be returned if the first
|
|
|
|
argument cannot be converted to float. Note that the second argument does not
|
|
|
|
have to be float.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v = char(undefined, 'X') // v == 'X'
|
2020-03-04 17:53:38 +03:00
|
|
|
v = char(undefined, false) // v == false
|
2019-01-24 02:39:05 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## bytes
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Tries to convert an object to bytes object. See
|
|
|
|
[this](https://github.com/d5/tengo/blob/master/docs/runtime-types.md) for more
|
|
|
|
details on type conversion.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := bytes("foo") // v == [102 111 111]
|
|
|
|
```
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Optionally it can take the second argument, which will be returned if the first
|
|
|
|
argument cannot be converted to float. Note that the second argument does not
|
|
|
|
have to be float.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v = bytes(undefined, bytes("foo")) // v == bytes("foo")
|
2020-03-04 17:53:38 +03:00
|
|
|
v = bytes(undefined, false) // v == false
|
2019-01-24 02:39:05 +03:00
|
|
|
```
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
If you pass an int to `bytes()` function, it will create a new byte object with
|
|
|
|
the given size.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
```golang
|
|
|
|
v := bytes(100)
|
|
|
|
```
|
|
|
|
|
2019-03-22 19:27:34 +03:00
|
|
|
## time
|
|
|
|
|
|
|
|
Tries to convert an object to time value.
|
|
|
|
|
|
|
|
```golang
|
|
|
|
v := time(1257894000) // 2009-11-10 23:00:00 +0000 UTC
|
|
|
|
```
|
|
|
|
|
2019-01-24 02:39:05 +03:00
|
|
|
## is_string
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is string. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_int
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is int. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_bool
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is bool. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_float
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is float. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_char
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is char. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_bytes
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is bytes. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_error
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is error. Or it returns `false`.
|
2019-01-24 02:39:05 +03:00
|
|
|
|
|
|
|
## is_undefined
|
|
|
|
|
2019-02-04 02:50:40 +03:00
|
|
|
Returns `true` if the object's type is undefined. Or it returns `false`.
|
|
|
|
|
|
|
|
## is_function
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Returns `true` if the object's type is function or closure. Or it returns
|
|
|
|
`false`. Note that `is_function` returns `false` for builtin functions and
|
2020-03-04 17:53:38 +03:00
|
|
|
user-provided callable objects.
|
2019-02-04 02:50:40 +03:00
|
|
|
|
|
|
|
## is_callable
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Returns `true` if the object is callable (e.g. function, closure, builtin
|
|
|
|
function, or user-provided callable objects). Or it returns `false`.
|
2019-03-22 19:27:34 +03:00
|
|
|
|
|
|
|
## is_array
|
|
|
|
|
|
|
|
Returns `true` if the object's type is array. Or it returns `false`.
|
|
|
|
|
|
|
|
## is_immutable_array
|
|
|
|
|
|
|
|
Returns `true` if the object's type is immutable array. Or it returns `false`.
|
|
|
|
|
|
|
|
## is_map
|
|
|
|
|
|
|
|
Returns `true` if the object's type is map. Or it returns `false`.
|
|
|
|
|
|
|
|
## is_immutable_map
|
|
|
|
|
|
|
|
Returns `true` if the object's type is immutable map. Or it returns `false`.
|
|
|
|
|
2019-03-27 11:27:59 +03:00
|
|
|
## is_iterable
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
Returns `true` if the object's type is iterable: array, immutable array, map,
|
|
|
|
immutable map, string, and bytes are iterable types in Tengo.
|
2019-03-27 11:27:59 +03:00
|
|
|
|
2019-03-22 19:27:34 +03:00
|
|
|
## is_time
|
|
|
|
|
|
|
|
Returns `true` if the object's type is time. Or it returns `false`.
|