Language GuideNative Functions

Native Functions

KodiScript provides a rich set of built-in functions that are available in both Go and Kotlin SDKs.

String Functions

FunctionSignatureDescription
toStringtoString(value)Converts any value to its string representation
toNumbertoNumber(value)Converts a value to a number
lengthlength(str)Returns the length of a string
substringsubstring(str, start, [end])Extracts a portion of a string
toUpperCasetoUpperCase(str)Converts string to uppercase
toLowerCasetoLowerCase(str)Converts string to lowercase
trimtrim(str)Removes whitespace from both ends
splitsplit(str, separator)Splits a string into an array
joinjoin(array, separator)Joins array elements into a string
replacereplace(str, old, new)Replaces all occurrences of a substring
containscontains(str, substr)Checks if string contains a substring
startsWithstartsWith(str, prefix)Checks if string starts with prefix
endsWithendsWith(str, suffix)Checks if string ends with suffix
indexOfindexOf(str, substr)Returns index of first occurrence (-1 if not found)

Examples

let text = "  Hello, World!  "
print(trim(text))           // "Hello, World!"
print(toUpperCase("hello")) // "HELLO"
print(split("a,b,c", ","))  // ["a", "b", "c"]
print(contains("hello", "ell")) // true

Math Functions

FunctionSignatureDescription
absabs(n)Returns absolute value
floorfloor(n)Rounds down to nearest integer
ceilceil(n)Rounds up to nearest integer
roundround(n)Rounds to nearest integer
minmin(a, b, ...)Returns the smallest value
maxmax(a, b, ...)Returns the largest value
powpow(base, exp)Returns base raised to the power of exp
sqrtsqrt(n)Returns square root
sinsin(n)Returns sine (n in radians)
coscos(n)Returns cosine (n in radians)
tantan(n)Returns tangent (n in radians)
loglog(n)Returns natural logarithm
log10log10(n)Returns base-10 logarithm
expexp(n)Returns e raised to the power of n

Examples

print(abs(-5))        // 5
print(floor(3.7))     // 3
print(ceil(3.2))      // 4
print(pow(2, 8))      // 256
print(min(5, 3, 8))   // 3

Random Functions

FunctionSignatureDescription
randomrandom()Returns a random number between 0 and 1
randomIntrandomInt(min, max)Returns a random integer in range [min, max]
randomUUIDrandomUUID()Returns a random UUID v4 string

Examples

print(random())           // 0.742...
print(randomInt(1, 100))  // 42
print(randomUUID())       // "550e8400-e29b-41d4-a716-446655440000"

Type Functions

FunctionSignatureDescription
typeOftypeOf(value)Returns type as string: “null”, “string”, “number”, “boolean”, “array”, “object”
isNullisNull(value)Returns true if value is null
isNumberisNumber(value)Returns true if value is a number
isStringisString(value)Returns true if value is a string
isBoolisBool(value)Returns true if value is a boolean

Examples

print(typeOf(42))       // "number"
print(typeOf("hello"))  // "string"
print(isNull(null))     // true
print(isNumber(3.14))   // true

Array Functions

FunctionSignatureDescription
sizesize(array)Returns the number of elements
firstfirst(array)Returns the first element or null
lastlast(array)Returns the last element or null
reversereverse(array)Returns a reversed copy of the array
sliceslice(array, start, [end])Returns a portion of the array
sortsort(array, ["asc"|"desc"])Returns a sorted copy (ascending by default)
sortBysortBy(array, field, ["asc"|"desc"])Sorts array of objects by a field

Examples

let nums = [3, 1, 4, 1, 5]
print(size(nums))       // 5
print(first(nums))      // 3
print(last(nums))       // 5
print(sort(nums))       // [1, 1, 3, 4, 5]
print(sort(nums, "desc")) // [5, 4, 3, 1, 1]
print(reverse(nums))    // [5, 1, 4, 1, 3]

JSON Functions

FunctionSignatureDescription
jsonParsejsonParse(str)Parses a JSON string into an object/array
jsonStringifyjsonStringify(value)Converts a value to JSON string

Examples

let obj = jsonParse('{"name": "Alice", "age": 30}')
print(obj.name)  // "Alice"
 
let arr = [1, 2, 3]
print(jsonStringify(arr))  // "[1,2,3]"

Encoding Functions

Base64

FunctionSignatureDescription
base64Encodebase64Encode(str)Encodes string to Base64
base64Decodebase64Decode(str)Decodes Base64 string

URL

FunctionSignatureDescription
urlEncodeurlEncode(str)URL-encodes a string
urlDecodeurlDecode(str)Decodes a URL-encoded string

Examples

print(base64Encode("Hello"))  // "SGVsbG8="
print(base64Decode("SGVsbG8="))  // "Hello"
 
print(urlEncode("hello world"))  // "hello+world"
print(urlDecode("hello+world"))  // "hello world"

Crypto/Hash Functions

FunctionSignatureDescription
md5md5(str)Returns MD5 hash as hex string
sha1sha1(str)Returns SHA-1 hash as hex string
sha256sha256(str)Returns SHA-256 hash as hex string

Examples

print(md5("hello"))     // "5d41402abc4b2a76b9719d911017c592"
print(sha256("hello"))  // "2cf24dba5fb0a30e26e83b2ac5b9e29e..."

Output Function

FunctionSignatureDescription
printprint(value, ...)Outputs values (captured by interpreter)

The print function is special - it doesn’t return a value but captures output that can be retrieved from the interpreter after execution.

print("Hello, World!")
print("The answer is:", 42)