Standard Library
Kite provides a rich standard library of built-in functions for common operations.
Output Functions
print(value)
Output a value without a newline.
print("Hello")print(" World")// Output: Hello Worldprintln(value)
Output a value followed by a newline.
println("Hello")println("World")// Output:// Hello// WorldType Casting Functions
int(value)
Convert a value to an integer.
var x = int("42") // 42var y = int(3.7) // 3var z = int(true) // Error: boolean can't be convertednumber(value)
Convert a value to a number (floating-point).
var x = number("3.14") // 3.14var y = number(42) // 42.0decimal(value)
Convert a value to a decimal (high precision).
var price = decimal("19.99")var amount = decimal(100)string(value)
Convert any value to its string representation.
var s = string(42) // "42"var t = string(true) // "true"var u = string([1,2,3]) // "[1, 2, 3]"boolean(value)
Convert a value to a boolean.
var b = boolean(1) // truevar c = boolean(0) // falsevar d = boolean("") // falsevar e = boolean("text") // trueString Functions
length(value)
Get the length of a string or array.
var len = length("Hello") // 5var size = length([1,2,3]) // 3charAt(string, index)
Get the character at a specific index.
var ch = charAt("Hello", 0) // "H"var last = charAt("Hello", 4) // "o"indexOf(string, search)
Find the index of a substring.
var pos = indexOf("Hello World", "World") // 6var notFound = indexOf("Hello", "xyz") // -1substring(string, start, end?)
Extract a portion of a string.
var sub = substring("Hello World", 0, 5) // "Hello"var rest = substring("Hello World", 6) // "World"toLowerCase(string)
Convert string to lowercase.
var lower = toLowerCase("HELLO") // "hello"toUpperCase(string)
Convert string to uppercase.
var upper = toUpperCase("hello") // "HELLO"trim(string)
Remove whitespace from both ends.
var cleaned = trim(" hello ") // "hello"startsWith(string, prefix)
Check if string starts with prefix.
var result = startsWith("Hello World", "Hello") // trueendsWith(string, suffix)
Check if string ends with suffix.
var result = endsWith("file.txt", ".txt") // truesplit(string, delimiter)
Split string into array.
var parts = split("a,b,c", ",") // ["a", "b", "c"]replace(string, search, replacement)
Replace occurrences in string.
var result = replace("Hello World", "World", "Kite")// "Hello Kite"format(template, ...args)
Format string with arguments.
var msg = format("Hello, {0}! You have {1} messages.", "Alice", 5)// "Hello, Alice! You have 5 messages."Collection Functions
range(end) / range(start, end) / range(start, end, step)
Generate a range of numbers.
var r1 = range(5) // [0, 1, 2, 3, 4]var r2 = range(1, 5) // [1, 2, 3, 4]var r3 = range(0, 10, 2) // [0, 2, 4, 6, 8]var r4 = range(5, 0, -1) // [5, 4, 3, 2, 1]first(array)
Get the first element.
var f = first([1, 2, 3]) // 1last(array)
Get the last element.
var l = last([1, 2, 3]) // 3isEmpty(array)
Check if array is empty.
var empty = isEmpty([]) // truevar notEmpty = isEmpty([1]) // falsecontains(array, element)
Check if array contains element.
var has = contains([1, 2, 3], 2) // truevar no = contains([1, 2, 3], 5) // falsepush(array, element)
Add element to end of array (returns new array).
var newArr = push([1, 2], 3) // [1, 2, 3]slice(array, start, end?)
Extract portion of array.
var part = slice([1, 2, 3, 4, 5], 1, 4) // [2, 3, 4]var rest = slice([1, 2, 3, 4, 5], 2) // [3, 4, 5]reverse(array)
Reverse array order.
var rev = reverse([1, 2, 3]) // [3, 2, 1]sort(array)
Sort array in ascending order.
var sorted = sort([3, 1, 2]) // [1, 2, 3]var names = sort(["c", "a", "b"]) // ["a", "b", "c"]distinct(array)
Remove duplicate elements.
var unique = distinct([1, 2, 2, 3, 1]) // [1, 2, 3]flatten(array)
Flatten nested arrays.
var flat = flatten([[1, 2], [3, 4]]) // [1, 2, 3, 4]join(array, separator)
Join array elements into string.
var str = join(["a", "b", "c"], "-") // "a-b-c"sum(array)
Sum numeric array elements.
var total = sum([1, 2, 3, 4, 5]) // 15average(array)
Calculate average of numeric array.
var avg = average([10, 20, 30]) // 20.0find(array, predicate)
Find first element matching predicate.
var found = find([1, 2, 3, 4], (x) -> x > 2) // 3zip(array1, array2)
Combine two arrays into pairs.
var pairs = zip([1, 2, 3], ["a", "b", "c"])// [[1, "a"], [2, "b"], [3, "c"]]Numeric Functions
abs(number)
Get absolute value.
var a = abs(-5) // 5var b = abs(5) // 5floor(number)
Round down to nearest integer.
var f = floor(3.7) // 3var g = floor(-3.2) // -4ceil(number)
Round up to nearest integer.
var c = ceil(3.2) // 4var d = ceil(-3.7) // -3round(number)
Round to nearest integer.
var r = round(3.5) // 4var s = round(3.4) // 3min(a, b)
Get minimum value.
var m = min(5, 3) // 3max(a, b)
Get maximum value.
var m = max(5, 3) // 5clamp(value, min, max)
Clamp value within range.
var c = clamp(15, 0, 10) // 10var d = clamp(-5, 0, 10) // 0var e = clamp(5, 0, 10) // 5pow(base, exponent)
Calculate power.
var p = pow(2, 3) // 8var q = pow(10, 2) // 100sqrt(number)
Calculate square root.
var s = sqrt(16) // 4.0var t = sqrt(2) // 1.4142...Object Functions
keys(object)
Get array of object keys.
var obj = { name: "Alice", age: 30 }var k = keys(obj) // ["name", "age"]values(object)
Get array of object values.
var obj = { name: "Alice", age: 30 }var v = values(obj) // ["Alice", 30]entries(object)
Get array of [key, value] pairs.
var obj = { a: 1, b: 2 }var e = entries(obj) // [["a", 1], ["b", 2]]hasKey(object, key)
Check if object has key.
var obj = { name: "Alice" }var has = hasKey(obj, "name") // truevar no = hasKey(obj, "email") // falseget(object, key, default?)
Get value with optional default.
var obj = { name: "Alice" }var name = get(obj, "name") // "Alice"var age = get(obj, "age", 0) // 0 (default)var email = get(obj, "email", null) // nullmerge(obj1, obj2, ...objN)
Merge multiple objects.
var a = { x: 1 }var b = { y: 2 }var c = { z: 3 }var merged = merge(a, b, c) // { x: 1, y: 2, z: 3 }// Later objects override earlier onesvar override = merge({ a: 1 }, { a: 2 }) // { a: 2 }Type Checking Functions
isString("hello") // trueisNumber(42) // trueisBoolean(true) // trueisArray([1, 2, 3]) // trueisObject({ a: 1 }) // trueisNull(null) // trueDate/Time Functions
now()
Get current date/time as ISO string.
var current = now() // "2024-01-15T10:30:00.000"date(year, month, day)
Create a date.
var d = date(2024, 1, 15) // "2024-01-15"timestamp()
Get current Unix timestamp (milliseconds).
var ts = timestamp() // e.g., 1705312200000addDays(date, days)
Add days to date.
var later = addDays("2024-01-15", 10) // "2024-01-25"var earlier = addDays("2024-01-15", -5) // "2024-01-10"diffDays(date1, date2)
Calculate difference in days between dates.
var diff = diffDays("2024-01-01", "2024-01-15") // 14formatDate(date, pattern)
Format date using pattern.
var formatted = formatDate("2024-01-15", "MM/dd/yyyy")// "01/15/2024"Utility Functions
uuid()
Generate a random UUID.
var id = uuid() // "550e8400-e29b-41d4-a716-446655440000"hash(value, algorithm?)
Calculate hash of a value. Default algorithm is SHA-256.
var h = hash("hello") // SHA-256 hashvar md5 = hash("hello", "MD5") // MD5 hashvar sha1 = hash("hello", "SHA-1") // SHA-1 hashbase64Encode(string)
Encode string to Base64.
var encoded = base64Encode("Hello, World!")// "SGVsbG8sIFdvcmxkIQ =="base64Decode(string)
Decode Base64 to string.
var decoded = base64Decode("SGVsbG8sIFdvcmxkIQ==")// "Hello, World!"toJson(value)
Convert value to JSON string.
var json = toJson({ name: "Alice", age: 30 })// '{"name":"Alice","age":30}'fromJson(string)
Parse JSON string to value.
var obj = fromJson('{"name":"Alice","age":30}')// { name: "Alice", age: 30 }env(name, default?)
Get environment variable.
var home = env("HOME")var port = env("PORT", "8080") // default if not setQuick Reference
| Category | Functions |
|---|---|
| Output | print, println |
| Cast | int, number, decimal, string, boolean, any |
| String | length, charAt, indexOf, substring, toLowerCase, toUpperCase, trim, startsWith, endsWith, split, replace, format |
| Collection | range, first, last, isEmpty, contains, push, slice, reverse, sort, distinct, flatten, join, sum, average, find, zip |
| Numeric | abs, floor, ceil, round, min, max, clamp, pow, sqrt |
| Object | keys, values, entries, hasKey, get, merge |
| Types | isString, isNumber, isBoolean, isArray, isObject, isNull |
| DateTime | now, date, timestamp, addDays, diffDays, formatDate |
| Utility | uuid, hash, base64Encode, base64Decode, toJson, fromJson, env |
Explore more: Return to Syntax Basics or check the Documentation Home.