Skip to content

sdk.lua

Vadim edited this page Jun 24, 2023 · 1 revision

aes

AES encryption and decryption.

decrypt

function aes.decrypt(key: string, value: string)
  -> decrypted: string

Decrypts a string using AES.

@param key β€” The key to use for decryption.

@param value β€” The string to decrypt.

@return decrypted β€” The decrypted string.

encrypt

function aes.encrypt(key: string, value: string)
  -> encrypted: string

Encrypts a string using AES.

@param key β€” The key to use for encryption.

@param value β€” The string to encrypt.

@return encrypted β€” The encrypted string.


base64

Base64 encoding and decoding.

decode

function base64.decode(value: string, encoding?: userdata)
  -> decoded: string

Decodes a base64 string.

@param value β€” The base64 string to decode.

@param encoding β€” The encoding to use. Defaults to std_encoding.

@return decoded β€” The decoded string.

encode

function base64.encode(value: string, encoding?: userdata)
  -> encoded: string

Encodes a string to base64.

@param value β€” The string to encode.

@param encoding β€” The encoding to use. Defaults to std_encoding.

@return encoded β€” The encoded string.

raw_std_encoding

userdata

The standard raw, unpadded base64 encoding, as defined in RFC 4648.

raw_url_encoding

userdata

The alternate raw, unpadded base64 encoding defined in RFC 4648. It is typically used in URLs and file names.

std_encoding

userdata

The standard base64 encoding, as defined in RFC 4648.

url_encoding

userdata

The alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names.


base64_encoding

userdata

crypto

Various cryptographic functions.

aes

aes

AES encryption and decryption.

md5

md5

MD5 cryptographic hash function.

sha1

sha1

SHA1 cryptographic hash function.

sha256

sha256

SHA256 cryptographic hash function.

sha512

sha512

SHA-512 cryptographic hash function.


encoding

base64

base64

Base64 encoding and decoding.

json

json

Provides functions for encoding and decoding JSON.


headless

Headless browser

browser

function headless.browser()
  -> browser: headless_browser

Creates a new headless browser

@return browser β€” headless browser instance


headless_browser

page

(method) headless_browser:page(url: string)
  -> page: headless_page

Visit the page

@param url β€” url of the page to visit


headless_element

click

(method) headless_element:click()

html

(method) headless_element:html()
  -> html: string

input

(method) headless_element:input(text: string)

headless_page

element

(method) headless_page:element(selector: string)
  -> element: headless_element

Get HTML element by CSS selector

@param selector β€” CSS selector for the element

@return element β€” HTML element

html

(method) headless_page:html()
  -> html: string

navigate

(method) headless_page:navigate(url: string)

@param url β€” URL to navigate


html

This library provides functions for parsing HTML and querying it using CSS selectors. It is based on goquery.

parse

function html.parse(html: string)
  -> document: html_document

Parses the given HTML and returns a selection containing the root element.

@param html β€” The HTML to parse.

@return document β€” The document object.


html_document

Document represents an HTML document to be manipulated. Unlike jQuery, which is loaded as part of a DOM document, and thus acts upon its containing document, GoQuery doesn't know which HTML document to act upon. So it needs to be told, and that's what the document class is for. It holds the root document node to manipulate, and can make selections on this document.

find

(method) html_document:find(selector: string)
  -> selection: html_selection

Finds all elements that match the selector string. It returns a new selection object with the matched elements.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

html

(method) html_document:html()
  -> html: string

Gets the HTML contents of the first element in the set of matched elements. It includes text and comment nodes.

@return html β€” The HTML contents of the first element in the set of matched elements.

markdown

(method) html_document:markdown()
  -> markdown: string

Converts the document to Markdown. Can be used to show the contents of a document in info page

@return markdown β€” The Markdown representation of the document.

selection

(method) html_document:selection()
  -> selection: html_selection

Converts document to a selection object.

@return selection β€” A selection object.

simplified

(method) html_document:simplified()
  -> html: html_document

Gets the readable part of the document (simplified view). Similar to reader mode in browsers.

@return html β€” The simplified document


html_selection

add

(method) html_selection:add(selector: string)
  -> selection: html_selection

Add adds the selector string's matching nodes to those in the current selection and returns a new selection object. The selector string is run in the context of the document of the current selection object.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

add_back

(method) html_selection:add_back(selection: html_selection)
  -> selection: html_selection

Adds the specified Selection object's nodes to those in the current selection and returns a new Selection object.

@param selection β€” A selection object.

@return selection β€” A selection object.

add_class

(method) html_selection:add_class(class: string)

Adds the specified class(es) to each of the set of matched elements.

@param class β€” One or more class names to be added to the class attribute of each matched element.

add_selection

(method) html_selection:add_selection(selection: html_selection)
  -> selection: html_selection

Adds the specified selection object's nodes to those in the current selection and returns a new selection object.

@param selection β€” A selection object.

@return selection β€” A selection object.

attr

(method) html_selection:attr(name: string)
  -> value: string
  2. ok: boolean

Returns the value of the given attribute of the first element in the selection.

@param name β€” The name of the attribute to get.

@return value β€” The value of the attribute, or nil if the attribute is not present.

@return ok β€” Whether the attribute was present.

attr_or

(method) html_selection:attr_or(name: string, default: string)
  -> value: string

Returns the value of the given attribute of the first element in the selection, or a default value if the attribute is not present.

@param name β€” The name of the attribute to get.

@param default β€” The default value to return if the attribute is not present.

@return value β€” The value of the attribute, or the default value if the attribute is absent.

children

(method) html_selection:children()
  -> selection: html_selection

Gets all direct child elements of each element in the Selection. It returns a new selection object containing the matched elements.

@return selection β€” A selection object.

closest

(method) html_selection:closest(selector: string)
  -> selection: html_selection

Gets the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

contents

(method) html_selection:contents()
  -> selection: html_selection

Contents gets the children of each element in the selection, including text and comment nodes. It returns a new selection object containing these elements

@return selection β€” A selection object.

each

(method) html_selection:each(fn: fun(selection: html_selection, index: number))

Iterates over all elements in the selection, calling the given function for each one.

@param fn β€” The function to call for each element.

eq

(method) html_selection:eq(index: number)
  -> selection: html_selection

Reduces the set of matched elements to the one at the specified index. If a negative index is given, it counts backwards starting at the end of the set. It returns a new Selection object, and an empty Selection object if the index is invalid.

@param index β€” The index of the element to select.

@return selection β€” A selection object.

filter

(method) html_selection:filter(selector: string)
  -> selection: html_selection

Filter reduces the set of matched elements to those that match the selector string. It returns a new Selection object for this subset of matching elements.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

find

(method) html_selection:find(selector: string)
  -> selection: html_selection

Finds all elements matching the given selector.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

find_selection

(method) html_selection:find_selection(selection: html_selection)
  -> selection: html_selection

gets the descendants of each element in the current selection, filtered by a selection. It returns a new selection object containing these matched elements.

@param selection β€” A selection object.

@return selection β€” A selection object.

first

(method) html_selection:first()
  -> selection: html_selection

Reduces the set of matched elements to the first in the set. It returns a new selection object, and an empty selection object if the the selection is empty.

@return selection β€” A selection object.

has_class

(method) html_selection:has_class(class: string)
  -> ok: boolean

determines whether any of the matched elements are assigned the given class.

@param class β€” The class to check for.

@return ok β€” Whether any of the matched elements have the given class.

html

(method) html_selection:html()
  -> html: string

Returns the HTML contents of the first element in the selection.

@return html β€” Gets the HTML contents of the first element in the set of matched elements. It includes text and comment nodes.

is

(method) html_selection:is(selector: string)
  -> ok: boolean

Checks the current matched set of elements against a selector and returns true if at least one of these elements matches.

@param selector β€” The CSS selector to use to find the elements.

@return ok β€” Whether any of the matched elements match the selector.

last

(method) html_selection:last()
  -> selection: html_selection

Reduces the set of matched elements to the last in the set. It returns a new selection object, and an empty selection object if the the selection is empty.

@return selection β€” A selection object.

length

(method) html_selection:length()
  -> n: number

Returns the number of elements in the selection.

@return n β€” The number of elements in the selection.

map

(method) html_selection:map(fn: fun(selection: html_selection, index: number):any)
  -> results: any[]

Iterates over a selection, executing a function for each matched element. The function's return value is added to the returned table.

@param fn β€” The function to execute for each element. It receives the index of the element in the selection and the element as arguments.

@return results β€” A table containing the return values of the function for each element.

markdown

(method) html_selection:markdown()
  -> markdown: string

Converts the selection to Markdown. Can be used to show the contents of a selection in info page

@return markdown β€” The Markdown representation of the selection.

next

(method) html_selection:next()
  -> selection: html_selection

Gets the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector. It returns a new Selection object containing the matched elements.

@return selection β€” A selection object.

next_all

(method) html_selection:next_all()

Gets all the following siblings of each element in the Selection. It returns a new Selection object containing the matched elements.

next_until

(method) html_selection:next_until(selector: string)
  -> selection: html_selection

gets all following siblings of each element up to but not including the element matched by the selector. It returns a new Selection object containing the matched elements.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

parent

(method) html_selection:parent()
  -> selection: html_selection

Gets the parent of each element in the Selection. It returns a new Selection object containing the matched elements.

@return selection β€” A selection object.

parents

(method) html_selection:parents()
  -> selection: html_selection

Gets the ancestors of each element in the current Selection. It returns a new Selection object with the matched elements.

@return selection β€” A selection object.

parents_until

(method) html_selection:parents_until(selector: string)
  -> selection: html_selection

Gets the ancestors of each element in the current Selection, up to but not including the element matched by the selector. It returns a new Selection object with the matched elements.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

prev

(method) html_selection:prev()
  -> selection: html_selection

Gets the immediately preceding sibling of each element in the Selection. It returns a new selection object containing the matched elements.

@return selection β€” A selection object.

prev_all

(method) html_selection:prev_all()
  -> selection: html_selection

Gets all the preceding siblings of each element in the Selection. It returns a new selection object containing the matched elements.

@return selection β€” A selection object.

prev_until

(method) html_selection:prev_until(selector: string)
  -> selection: html_selection

Gets all preceding siblings of each element up to but not including the element matched by the selector. It returns a new selection object containing the matched elements.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

remove

(method) html_selection:remove(selector: string)
  -> selection: html_selection

Removes elements from the selection that match the selector string. It returns a new selection object with the matching elements removed.

@param selector β€” The CSS selector to use to find the elements.

@return selection β€” A selection object.

remove_class

(method) html_selection:remove_class(class: string)

Removes the given class(es) from each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments. If no class name is provided, all classes are removed.

@param class β€” The class to remove. If not provided, all classes are removed.

siblings

(method) html_selection:siblings()
  -> selection: html_selection

Gets all sibling elements of each element in the Selection. It returns a new selection object containing the matched elements.

@return selection β€” A selection object.

simplified

(method) html_selection:simplified()
  -> selection: html_selection

Gets the readable part of the selection (simplified view). Similar to reader mode in browsers.

@return selection β€” A selection object.

slice

(method) html_selection:slice(start: number, finish: number)
  -> selection: html_selection

Returns a selection containing a subset of the elements in the original selection. It returns a new selection object with the matched elements.

@param start β€” The index of the first element to include in the new selection.

@param finish β€” The index of the first element to exclude from the new selection.

@return selection β€” A selection object.

terminate

(method) html_selection:terminate()
  -> selection: html_selection

Ends the most recent filtering operation in the current chain and returns the set of matched elements to its previous state.

@return selection β€” A selection object.

text

(method) html_selection:text()
  -> text: string

Returns the text contents of the first element in the selection.

@return text β€” The text contents of the first element in the selection.

toggle_class

(method) html_selection:toggle_class(class: string)

Adds or removes the given class(es) for each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments.

@param class β€” The class to toggle.


http

Package http provides HTTP client implementations. Make HTTP (or HTTPS) requests

METHOD_CONNECT

string

CONNECT HTTP Method

METHOD_DELETE

string

DELETE HTTP Method

METHOD_GET

string

GET HTTP Method

METHOD_HEAD

string

HEAD HTTP Method

METHOD_PATCH

string

PATCH HTTP Method

METHOD_POST

string

POST HTTP Method

METHOD_PUT

string

PUT HTTP Method

STATUS_OK

number

Returned if the request was successful

request

function http.request(method: 'CONNECT'|'DELETE'|'GET'|'HEAD'|'PATCH'...(+2), url: string, body?: string)
  -> request: http_request

Create a new HTTP request

@param method β€” HTTP method

@param url β€” URL

@param body β€” Request body

@return request β€” Request

method:
    | 'GET'
    | 'HEAD'
    | 'POST'
    | 'PUT'
    | 'PATCH'
    | 'DELETE'
    | 'CONNECT'

http_request

HTTP Request

content_length

(method) http_request:content_length(length?: number)
  -> length: number?

Get or set request content length

@param length β€” Content length

@return length β€” Content length

cookie

(method) http_request:cookie(key: string, value?: string)
  -> value: string?

Get or set request cookie

@param key β€” Cookie key

@param value β€” Cookie value

@return value β€” Cookie value

header

(method) http_request:header(key: string, value?: string)
  -> value: string?

Get or set request header

@param key β€” Header key

@param value β€” Header value

@return value β€” Header value

send

(method) http_request:send()
  -> response: http_response

Perform request

@return response β€” Response


http_response

HTTP Response

body

(method) http_response:body()
  -> body: string

Get response body

@return body β€” Body

content_length

(method) http_response:content_length()
  -> length: number

Get response content length

@return length β€” Content length

cookies

(method) http_response:cookies()
  -> cookies: { name: string, value: string }[]

Get response cookies. Returns a list of cookies.

@return cookies β€” Cookies

header

(method) http_response:header(key: string)
  -> value: string

Get response header

@param key β€” Header key

@return value β€” Header value

status

(method) http_response:status()
  -> status: number

Get response status

@return status β€” Status


js

JavaScript execution.

vm

function js.vm()
  -> vm: js_vm

Creates a new JavaScript virtual machine.

@return vm β€” The new JavaScript virtual machine.


js_vm

A JavaScript virtual machine. This is used to execute JavaScript code.

get

(method) js_vm:get(name: string)
  -> value: js_vm_value

Gets the value of the given property on the global object.

@param name β€” The name of the property.

@return value β€” The value of the property.

run

(method) js_vm:run(code: string)
  -> value: js_vm_value

Runs the given JavaScript code.

@param code β€” The JavaScript code to run.

@return value β€” The value returned by the code.

set

(method) js_vm:set(name: string, value: any)

Sets the value of the given property on the global object. It will convert the given Lua value to a JavaScript value.

@param name β€” The name of the property.

@param value β€” The value to set.


js_vm_value

A value returned from a JavaScript VM.

export

(method) js_vm_value:export()
  -> value: any

Exports the value to a Lua value.

@return value β€” The exported value.

is_boolean

(method) js_vm_value:is_boolean()
  -> ok: boolean

Returns whether the value is a boolean.

@return ok β€” Whether the value is a boolean.

is_function

(method) js_vm_value:is_function()
  -> ok: boolean

Returns whether the value is a function.

@return ok β€” Whether the value is a function.

is_nan

(method) js_vm_value:is_nan()
  -> ok: boolean

Returns whether the value is NaN.

@return ok β€” Whether the value is NaN.

is_null

(method) js_vm_value:is_null()
  -> ok: boolean

Returns whether the value is null.

@return ok β€” Whether the value is null.

is_number

(method) js_vm_value:is_number()
  -> ok: boolean

Returns whether the value is a number.

@return ok β€” Whether the value is a number.

is_object

(method) js_vm_value:is_object()
  -> ok: boolean

Returns whether the value is an object.

@return ok β€” Whether the value is an object.

is_string

(method) js_vm_value:is_string()
  -> ok: boolean

Returns whether the value is a string.

@return ok β€” Whether the value is a string.

is_undefined

(method) js_vm_value:is_undefined()
  -> ok: boolean

Returns whether the value is undefined.

@return ok β€” Whether the value is undefined.

to_string

(method) js_vm_value:to_string()
  -> string: string

Converts the value to a string.

@return string β€” The string representation of the value.


json

Provides functions for encoding and decoding JSON.

decode

function json.decode(json: string)
  -> value: any

Decodes a JSON string into a Lua value.

@param json β€” The JSON string to decode.

@return value β€” The decoded value.

encode

function json.encode(value: any)
  -> json: string

Encodes a Lua value into a JSON string.

@param value β€” The value to encode.

@return json β€” The encoded JSON string.


levenshtein

Levenshtein distance algorithm

distance

function levenshtein.distance(s1: string, s2: string)
  -> distance: number

Compute Levenshtein distance between two strings

@return distance β€” Levenshtein distance between s1 and s2


md5

MD5 cryptographic hash function.

sum

function md5.sum(value: string)
  -> hash: string

Returns the MD5 hash of the given string.

@param value β€” The string to hash.

@return hash β€” The MD5 hash of the given string.


re

Compiled regular expression

find_submatch

(method) re:find_submatch(text: string)
  -> matches: table

Returns a slice of strings holding the text of the leftmost match of the regular expression in s and the matches, if any, of its subexpressions. A return value of empty table indicates no match.

@param text β€” A string to search in

@return matches β€” Found matches

groups

(method) re:groups(text: string)
  -> groups: table

Returns a table of all capture groups.

@param text β€” The text to split

@return groups β€” A table of all capture groups

match

(method) re:match(text: string)
  -> matched: table

Reports whether the string s contains any match of the regular expression.

@param text β€” A string to search in

@return matched β€” Whether the string s contains any match of the regular expression.

replace_all

(method) re:replace_all(text: string, replacement: string)
  -> replaced: string

Returns a copy of text, replacing matches of the regexp with the replacement string. Inside replacement, $ signs are interpreted as in expand, so for instance $1 represents the text of the first submatch.

@param text β€” A string to replace matches in

@param replacement β€” A string to replace matches with

@return replaced β€” The result of the replacement

replace_all_func

(method) re:replace_all_func(text: string, replacer: fun(match: string):string)
  -> replaced: string

Returns a copy of text, replacing matches of the regexp with the replacement function.

@param text β€” A string to replace matches in

@param replacer β€” A function to replace matches with

@return replaced β€” The result of the replacement

split

(method) re:split(text: string)
  -> parts: table

Splits the given text into a table of strings.

@param text β€” The text to split

@return parts β€” The result of the split


regexp

A regular expression library.

compile

function regexp.compile(pattern: string)
  -> regexp: re

Compiles the given pattern into a regular expression.

@param pattern β€” The pattern to compile

@return regexp β€” The compiled regular expression

match

function regexp.match(pattern: string, text: string)
  -> matched: table

Matches the given pattern against the given text.

@param pattern β€” The pattern to match

@param text β€” The text to match against

@return matched β€” A table of all matches

urls_relaxed

userdata

Matches all the urls it can find.

urls_strict

userdata

Only matches urls with a scheme to avoid false positives.


sdk

Contains various utilities for making HTTP requests, working with JSON, HTML, and more.

crypto

crypto

Various cryptographic functions.

encoding

encoding

headless

headless

Headless browser

html

html

This library provides functions for parsing HTML and querying it using CSS selectors. It is based on goquery.

http

http

Package http provides HTTP client implementations. Make HTTP (or HTTPS) requests

js

js

JavaScript execution.

levenshtein

levenshtein

Levenshtein distance algorithm

regexp

regexp

A regular expression library.

strings

strings

Simple functions to manipulate UTF-8 encoded strings.

time

time

Time library

urls

urls

URLs is a library for working with URLs.

util

util

Functional helpers


sha1

SHA1 cryptographic hash function.

sum

function sha1.sum(value: string)
  -> hash: string

Returns the SHA1 hash of the given string.

@param value β€” The string to hash.

@return hash β€” The SHA1 hash of the given string.


sha256

SHA256 cryptographic hash function.

sum

function sha256.sum(value: string)
  -> hash: string

Returns the SHA256 hash of the given string.

@param value β€” The string to hash.

@return hash β€” The SHA256 hash of the given string.


sha512

SHA-512 cryptographic hash function.

sum

function sha512.sum(value: string)
  -> hash: string

Returns the SHA-512 hash of the given string.

@param value β€” The string to hash.

@return hash β€” The SHA-512 hash of the given string.


strings

Simple functions to manipulate UTF-8 encoded strings.

contains

function strings.contains(s: string, substr: string)
  -> ok: boolean

Returns true if the string s contains substr.

@param s β€” The string to check.

@param substr β€” The substring to check for.

@return ok β€” True if the string contains the substring.

contains_any

function strings.contains_any(s: string, chars: string)
  -> ok: boolean

Returns true if the string s contains any of the runes in chars. If chars is the empty string, contains_any returns false.

@param s β€” The string to check.

@param chars β€” The characters to check for.

@return ok β€” True if the string contains any of the characters.

count

function strings.count(s: string, substr: string)
  -> n: number

Returns the number of non-overlapping instances of substr in s.

@param s β€” The string to check.

@param substr β€” The substring to check for.

@return n β€” The number of non-overlapping instances of substr in s.

count_any

function strings.count_any(s: string, chars: string)
  -> n: number

Returns the number of non-overlapping instances of any of the runes in chars in s.

@param s β€” The string to check.

@param chars β€” The characters to check for.

@return n β€” The number of non-overlapping instances of any of the runes in chars in s.

duplicate

function strings.duplicate(s: string, count: number)
  -> s: string

Returns a new string consisting of count copies of the string s. If count is zero or negative, returns the empty string.

@param s β€” The string to repeat.

@param count β€” The number of times to repeat the string.

@return s β€” A new string consisting of count copies of the string s.

equal_fold

function strings.equal_fold(s: string, t: string)
  -> ok: boolean

Returns true if the strings s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding.

@param s β€” The first string to check.

@param t β€” The second string to check.

@return ok β€” True if the strings are equal under Unicode case-folding.

has_prefix

function strings.has_prefix(s: string, prefix: string)
  -> ok: boolean

Returns true if the string s begins with prefix.

@param s β€” The string to check.

@param prefix β€” The prefix to check for.

@return ok β€” True if the string has the prefix.

has_suffix

function strings.has_suffix(s: string, suffix: string)
  -> ok: boolean

Returns true if the string s ends with suffix.

@param s β€” The string to check.

@param suffix β€” The suffix to check for.

@return ok β€” True if the string has the suffix.

index

function strings.index(s: string, substr: string)
  -> i: number

Returns the index of the first instance of substr in s, or -1 if substr is not present in s.

@param s β€” The string to check.

@param substr β€” The substring to check for.

@return i β€” The index of the first instance of substr in s, or -1 if substr is not present in s.

index_any

function strings.index_any(s: string, chars: string)
  -> i: number

Returns the index of the first instance of any of the runes in chars in s, or -1 if none of the runes in chars are present in s.

@param s β€” The string to check.

@param chars β€” The characters to check for.

@return i β€” The index of the first instance of any of the runes in chars in s, or -1 if none of the runes in chars are present in s.

last_index

function strings.last_index(s: string, substr: string)
  -> i: number

Returns the index of the last instance of substr in s, or -1 if substr is not present in s.

@param s β€” The string to check.

@param substr β€” The substring to check for.

@return i β€” The index of the last instance of substr in s, or -1 if substr is not present in s.

last_index_any

function strings.last_index_any(s: string, chars: string)
  -> i: number

Returns the index of the last instance of any of the runes in chars in s, or -1 if none of the runes in chars are present in s.

@param s β€” The string to check.

@param chars β€” The characters to check for.

@return i β€” The index of the last instance of any of the runes in chars in s, or -1 if none of the runes in chars are present in s.

replace

function strings.replace(s: string, old: string, new: string, n: number)
  -> new: string

Replaces the first n instances of old with new in s.

@param s β€” The string to replace in.

@param old β€” The string to replace.

@param new β€” The string to replace with.

@param n β€” The number of instances to replace.

@return new β€” The new string.

replace_all

function strings.replace_all(s: string, old: string, new: string)
  -> new: string

Replaces all instances of old with new in s.

@param s β€” The string to replace in.

@param old β€” The string to replace.

@param new β€” The string to replace with.

@return new β€” The new string.

split

function strings.split(s: string, sep: string)
  -> table: table

Splits s around each instance of sep and returns a table of the substrings between those instances.

@param s β€” The string to split.

@param sep β€” The string to split around.

@return table β€” A table of the substrings between each instance of sep.

title

function strings.title(s: string)
  -> s: string

Returns a copy of the string s with all Unicode letters that begin words mapped to their title case.

@param s β€” The string to convert.

@return s β€” A copy of the string s with all Unicode letters that begin words mapped to their title case.

to_lower

function strings.to_lower(s: string)
  -> new: string

Returns a copy of the string s with all Unicode letters mapped to their lower case.

@param s β€” The string to convert.

@return new β€” The new string.

to_upper

function strings.to_upper(s: string)
  -> new: string

Returns a copy of the string s with all Unicode letters mapped to their upper case.

@param s β€” The string to convert.

@return new β€” The new string.

trim

function strings.trim(s: string, cutset: string)
  -> new: string

Returns a copy of the string s with all leading and trailing Unicode code points contained in cutset removed.

@param s β€” The string to trim.

@param cutset β€” The set of Unicode code points to remove.

@return new β€” The new string.

trim_left

function strings.trim_left(s: string, cutset: string)
  -> new: string

Returns a copy of the string s with all leading Unicode code points contained in cutset removed.

@param s β€” The string to trim.

@param cutset β€” The set of Unicode code points to remove.

@return new β€” The new string.

trim_prefix

function strings.trim_prefix(s: string, prefix: string)
  -> new: string

Returns a copy of the string s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.

@param s β€” The string to trim.

@param prefix β€” The prefix to remove.

@return new β€” The new string.

trim_right

function strings.trim_right(s: string, cutset: string)
  -> new: string

Returns a copy of the string s with all trailing Unicode code points contained in cutset removed.

@param s β€” The string to trim.

@param cutset β€” The set of Unicode code points to remove.

@return new β€” The new string.

trim_space

function strings.trim_space(s: string)
  -> new: string

Returns a copy of the string s with all leading and trailing white space removed, as defined by Unicode.

@param s β€” The string to trim.

@return new β€” The new string.

trim_suffix

function strings.trim_suffix(s: string, suffix: string)
  -> new: string

Returns a copy of the string s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

@param s β€” The string to trim.

@param suffix β€” The suffix to remove.

@return new β€” The new string.


time

Time library

hour

number

Duration constant. 60 * minute

microsecond

number

Duration constant. 1000 * nanosecond

millisecond

number

Duration constant. 1000 * microsecond

minute

number

Duration constant. 60 * second

nanosecond

number

Duration constant

second

number

Duration constant. 1000 * millisecond

sleep

function time.sleep(duration: number)

Sleep for the given duration


urls

URLs is a library for working with URLs.

parse

function urls.parse(raw_url: string)
  -> url: urls_url

Parses URL

@param raw_url β€” URL string to parse

@return url β€” Parsed URL

path_escape

function urls.path_escape(path: string)
  -> escaped: string

Escapes the string so it can be safely placed inside a URL path segment, replacing special characters (including /) with %XX sequences as needed.

@param path β€” The path to escape.

@return escaped β€” The escaped path.

path_unescape

function urls.path_unescape(escaped: string)
  -> path: string

Unescapes a string; the inverse operation of path_escape. It converts each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB.

@param escaped β€” The escaped path.

@return path β€” The unescaped path.

query_escape

function urls.query_escape(query: string)
  -> escaped: string

Escapes the string so it can be safely placed inside a URL query parameter, replacing special characters (including /) with %XX sequences as needed.

@param query β€” The query to escape.

@return escaped β€” The escaped query.

query_unescape

function urls.query_unescape(escaped: string)
  -> query: string

Unescapes a string; the inverse operation of query_escape. It converts each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB.

@param escaped β€” The escaped query.

@return query β€” The unescaped query.

values

function urls.values()
  -> values: urls_values

Creates a new values.

@return values β€” The new values.


urls_url

Structured URL

copy

(method) urls_url:copy()
  -> url: urls_url

Copy URL

@return url β€” A copied URL

hostname

(method) urls_url:hostname()
  -> hostname: string

Return hostname without port numbers.

@return hostname β€” URLs hostname

join_path

(method) urls_url:join_path(...string)
  -> url: urls_url

Returns a new URL with the provided path elements joined to any existing path and the resulting path cleaned of any ./ or ../ elements. Any sequences of multiple / characters will be reduced to a single /.

parse

(method) urls_url:parse(ref: string)
  -> url: urls_url

Parses a URL in the context of the receiver. The provided URL may be relative or absolute.

query

(method) urls_url:query(query?: urls_values)
  -> query: urls_values?

string

(method) urls_url:string()
  -> url: string

Reassembles the URL into a valid URL string.


urls_values

Values maps a string key to a list of values. It is typically used for query parameters and form values. Unlike in the http.header map, the keys in a values map are case-sensitive.

add

(method) urls_values:add(key: string, value: string)

Adds the key and value to the values. It appends to any existing values associated with key.

@param key β€” The key to add. It must not be empty.

@param value β€” The value to add. It must not be empty.

del

(method) urls_values:del(key: string)

Deletes the values associated with key.

@param key β€” The key to delete.

get

(method) urls_values:get(key: string)
  -> value: string

Gets the first value associated with the given key. If there are no values associated with the key, Get returns "".

@param key β€” The key to get.

@return value β€” The first value associated with the given key.

has

(method) urls_values:has(key: string)
  -> has: boolean

Returns true if the values contains the specified key, false otherwise.

@param key β€” The key to check.

@return has β€” True if the values contains the specified key, false otherwise.

parse

(method) urls_values:parse(encoded: string)
  -> values: urls_values

Creates a values from the URL encoded form. It is the inverse operation of string.

@param encoded β€” The URL encoded form of the values.

@return values β€” The values created from the URL encoded form.

set

(method) urls_values:set(key: string, value: string)

Sets the key to value. It replaces any existing values associated with key.

@param key β€” The key to add. It must not be empty.

@param value β€” The value to add. It must not be empty.

string

(method) urls_values:string()
  -> encoded: string

Encodes the values into "URL encoded" form sorted by key.

@return encoded β€” The URL encoded form of the values.


util

Functional helpers

chunk

function util.chunk(list: <T>[], size?: number)
  -> chunks: <T>[][]

Split list into chunks

@param list β€” List to split

@param size β€” Chunk size

@return chunks β€” List of chunks

contains_by

function util.contains_by(list: <T>[], predicate: fun(value: <T>):boolean)
  -> contains: boolean

Checks if a list contains a value using predicate

@param list β€” List to check

@param predicate β€” Predicate function

@return contains β€” Whether the list contains the value

drop

function util.drop(list: <T>[], n: number)
  -> dropped: <T>[]

Drop n elements from the beginning list

@param list β€” List to drop from

@param n β€” Number of elements to drop

@return dropped β€” List of dropped elements

drop_right

function util.drop_right(list: <T>[], n: number)
  -> dropped: <T>[]

Drop n elements from the end of list

@param list β€” List to drop from

@param n β€” Number of elements to drop

@return dropped β€” List of dropped elements

drop_right_while

function util.drop_right_while(list: <T>[], predicate: fun(value: <T>):boolean)

Drop elements from the end of list while predicate is true

@param list β€” List to drop from

@param predicate β€” Predicate function

drop_while

function util.drop_while(list: <T>[], predicate: fun(value: <T>):boolean)

Drop elements from the beginning of list while predicate is true

@param list β€” List to drop from

@param predicate β€” Predicate function

filter

function util.filter(list: <T>[], predicate: fun(value: <T>, index: number):boolean)
  -> filtered: <T>[]

Filter list by predicate

@param list β€” List to filter

@param predicate β€” Predicate function

@return filtered β€” List of filtered elements

find

function util.find(list: <T>[], predicate: fun(value: <T>):boolean)
  -> element: <T>
  2. ok: boolean

Find first element in list that satisfies predicate.

@param list β€” List to search

@param predicate β€” Predicate function

@return element β€” First element that satisfies predicate

@return ok β€” True if element was found

find_index

function util.find_index(list: <T>[], predicate: fun(value: <T>):boolean)
  -> index: number
  2. ok: boolean

Find index of first element in list that satisfies predicate.

@param list β€” List to search

@param predicate β€” Predicate function

@return index β€” Index of first element that satisfies predicate

@return ok β€” True if element was found

find_last_index

function util.find_last_index(list: <T>[], predicate: fun(value: <T>):boolean)
  -> index: number
  2. ok: boolean

Find index of last element in list that satisfies predicate.

@param list β€” List to search

@param predicate β€” Predicate function

@return index β€” Index of last element that satisfies predicate

@return ok β€” True if element was found

for_each

function util.for_each(list: <T>[], func: fun(value: <T>, index: number))

Calls a function for each element in a list

@param list β€” List to iterate over

@param func β€” Function to call

head

function util.head(list: <T>[])
  -> element: <T>?

Get first element of list

@param list β€” List to get first element from

@return element β€” First element of list

id

function util.id(value: <T>)
  -> result: <T>

Returns the first argument

@param value β€” Value to return

@return result β€” The first argument

init

function util.init(list: <T>[])
  -> initial: <T>[]?

Get all elements of list except last

@param list β€” List to get initial from

@return initial β€” List of all elements except last

last

function util.last(list: <T>[])
  -> element: <T>?

Get last element of list

@param list β€” List to get last element from

@return element β€” Last element of list

map

function util.map(table: table<<T>, <G>>, func: fun(value: <G>, key: <T>):<Q>)
  -> mapped: table<<T>, <Q>>

Maps a function over a table values

@param table β€” Table to map over

@param func β€” Mapping function

@return mapped β€” Mapped list

map_async

function util.map_async(table: table<<T>, <G>>, func: fun(value: <G>, key: <T>):<Q>)
  -> mapped: table<<T>, <Q>>

Maps a function over a table values asynchronously

@param table β€” Table to map values over

@param func β€” Mapping function. Takes a value of each key

@return mapped β€” Mapped table

max_by

function util.max_by(list: <T>[], func: fun(a: <T>, b: <T>):boolean)
  -> result: <T>

Returns the maximum value of a list, based on a function

@param list β€” List to find the maximum value of

@param func β€” Function to use to compare values. Returns true if the first argument is less than the second

@return result β€” The maximum value

min_by

function util.min_by(list: <T>[], func: fun(a: <T>, b: <T>):boolean)
  -> result: <T>

Returns the minimum value of a list, based on a function

@param list β€” List to find the minimum value of

@param func β€” Function to use to compare values. Returns true if the first argument is less than the second

@return result β€” The minimum value

reduce

function util.reduce(list: <T>[], func: fun(accumulator: <G>, value: <T>, index: number):<G>, inital: <G>)
  -> result: <G>

Reduces a list to a single value

@param list β€” List to reduce

@param func β€” Function to reduce with

@param inital β€” An initial value

@return result β€” Result of the reduction

reduce_right

function util.reduce_right(list: <T>[], func: fun(accumulator: <G>, value: <T>, index: number):<G>, initial: <G>)
  -> result: <G>

Reduces a list to a single value

@param list β€” List to reduce

@param func β€” Function to reduce with

@param initial β€” An initial value

@return result β€” Result of the reduction

reject

function util.reject(list: <T>[], predicate: fun(value: <T>, index: number):boolean)
  -> result: <T>[]

Rejects all elements that match a predicate

@param list β€” List to reject from

@param predicate β€” Predicate function

@return result β€” List of rejected elements

reverse

function util.reverse(list: <T>[])
  -> reversed: <T>[]

Reverse list

@param list β€” List to reverse

@return reversed β€” Reversed list

slice

function util.slice(list: <T>[], start: number, finish?: number)
  -> slice: <T>[]

Get slice of list

@param list β€” List to get slice from

@param start β€” Start index of slice

@param finish β€” End index of slice. Defaults to length of list

@return slice β€” Slice of list

sort

function util.sort(list: <T>[], cmp: fun(a: <T>, b: <T>):boolean)
  -> sorted: <T>[]

Returns a sorted list

@param list β€” List to sort

@param cmp β€” Comparison function. Defaults to <

@return sorted β€” Sorted list

tail

function util.tail(list: <T>[])
  -> tail: <T>[]?

Get all elements of list except first

@param list β€” List to get tail from

@return tail β€” List of all elements except first

take

function util.take(list: <T>[], n: number)
  -> elements: <T>[]?

Get first n elements of list

@param list β€” List to get elements from

@param n β€” Number of elements to get

@return elements β€” First n elements of list

take_while

function util.take_while(list: <T>[], predicate: fun(value: <T>):boolean)
  -> elements: <T>[]?

Take elements from list while predicate is true

@param list β€” List to get elements from

@param predicate β€” Predicate function

@return elements β€” Elements of list before predicate is false

Clone this wiki locally