Util functions
Various utility functions.
assert()
Returns an Error with message
if the value
is false. If the value
is true, nothing happens.
assert(value, [message])
Arguments
Returns
- (Error)
- Error if the
value
is false, else return nothing.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
attempt()
Attempts to evaluate function
and returns either the result or any caught error object. Additional arguments are passed to the function
when evaluated.
attempt(function, [arg1, [arg2, [...]]])
Arguments
function
(Expression)- The expression to evaluate.
args
(Any)- (Optional) The arguments to evaluate
function
with.
Returns
- (Any)
- The
function
result or error object.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
case_default()
Helper function that works exclusively within switch() or cond() functions. Returns the result of default
only when no other case functions are truthy.
case_default(default)
Arguments
default
(Expression, Any)- (Optional) The value or the expression to evaluate. For switch(), uses the function context; for cond(), uses
value
. Defaults to&@
.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
case_when()
Helper function that works exclusively within switch() or cond() functions. Evaluates the predicate
and returns the result of ifTrue
only if the predicate
is truthy.
case_when(predicate, [ifTrue])
Arguments
predicate
(Expression, Any)- The predicate to evaluate.
ifTrue
(Expression, Any)- (Optional) The value or the expression to evaluate when
predicate
is truthy. For switch(), uses the function context; for cond(), usesvalue
. Defaults to&@
.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
case_with()
Helper function that works exclusively within switch() or cond() functions. Evaluates the predicate
and returns the result of ifTrue
only if the predicate
is truthy, using the predicate
’s result as context.
case_with(predicate, [ifTrue])
Arguments
predicate
(Expression, Any)- The predicate to evaluate.
ifTrue
(Expression, Any)- (Optional) The value or the expression to evaluate when
predicate
is truthy. Uses the result ofpredicate
as context. Defaults to&@
.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
cond()
Iterates through cases
and evaluates each case helper function or predicate
with value
in context. Returns the result of the corresponding function or function
expression from the first case
whose predicate is truthy.
cond(value, case1, [case2, [...]]])
Arguments
value
(Any)- The input value.
cases
(Expression, Array)The case helper functions (case_when(), case_with(), case_default()) expressions or predicate-function pairs [
predicate
,function
].predicate
(Expression | Array | Object | String) – the predicate to evaluate, withvalue
in context. Optional for the last pair (default condition).function
(Expression | Array | Object | String) – the value or the expression to evaluate whenpredicate
is truthy. Uses thevalue
as context.
Returns
- (Any)
- The result of the evaluated function.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
dataurl_to_blob()
Given a string in Data URL format, returns a File object that can be submitted with the HTTP Connector using multipart/form-data
encoding type.
dataurl_to_blob(dataURL)
Arguments
dataURL
(String)- String, in Data URL format.
Returns
- (Blob)
- The File object.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
See Also
dataurl_to_text()
Given a string in Data URL format, returns a text. Assumes that Data URL contains text in UTF-8 encoding.
dataurl_to_text(dataURL)
Arguments
dataURL
(String)- String, in Data URL format.
Returns
- (String)
- The decoded text.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
See Also
default_to()
Checks value
to determine whether a default value should be returned in its place. The defaultValue
is returned if value
is NaN, null, or undefined.
default_to(value, [defaultValue])
Arguments
Returns
- (Any)
- The resolved value.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
from_formdata()
Given FormData object returns array of key-value pairs.
from_formdata(formData)
Arguments
formData
(FormData)- The FormObject data to convert.
Returns
- (Array)
- The key-value pairs.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
See Also
if()
Returns the result of ifTrue
if the predicate
is truthy; otherwise, returns the result of ifFalse
.
if(predicate, ifTrue, [ifFalse])
Arguments
predicate
(Expression, Any)- The predicate to evaluate.
ifTrue
(Expression, Any)- The value or the expression to evaluate when
predicate
is truthy. ifFalse
(Expression, Any)- (Optional) The value or the expression to evaluate when
predicate
is falsy. Defaults to&null
.
Returns
- (Any)
- The value returned by either
ifTrue
orifFalse
, depending on which one is evaluated.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
json_parse()
Parses a well-formed JSON string
, constructing the value or object described by the string
.
Aliases: parse_json().
json_parse(string)
Arguments
string
(String)- The string to parse as JSON. See the JavaScript Object Notation (JSON) for a description of JSON syntax.
Returns
- (Array)
- The value (Object, Array, String, Number, Boolean, or null) corresponding to the given JSON
string
.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_stringify()
Converts a value
to a JSON string.
Aliases: to_json().
json_stringify(value, [space])
Arguments
value
(Any)- The value to convert to a JSON string.
space
(String | Number)A String or Number object that's used to insert white space into the output JSON string for readability purposes.
If this is a Number, it indicates the number of space characters to use as white space for indenting purposes; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used.
If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.
Returns
- (Array)
- The JSON string representing the given
value
.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parse_json()
Alias for json_parse().
range()
Creates an array of numbers (positive and/or negative) progressing from start
up to, but not including, end
. If step
is not specified, the default 1
or -1
is used.
range(start, end, [step])
Arguments
start
(Number)- The start of the range.
end
(Number)- The end of the range.
step
(Number)- (Optional) The value to increment or decrement by. The default value is
1
or-1
Returns
- (Array)
- The range of numbers.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
range_right()
This method is like range() except that it populates values in descending order.
range_right(start, end, [step])
Arguments
start
(Number)- The start of the range.
end
(Number)- The end of the range.
step
(Number)- (Optional) The value to increment or decrement by. The default value is
1
or-1
Returns
- (Array)
- The range of numbers.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
repl()
Evaluates KelpQL expression represented as a string. This function makes possible to build REPL style interfaces in Kelp. See KelpQL Playground.
repl(expression, [data])
Arguments
expression
(String)- The string representing a KelpQL expression, or sequence of expression.
data
(Any)- (Optional) The arguments to invoke the
expression
with.
Returns
- (Object)
The object with the completion result or error of evaluating the given
expression
.status
(String) - The status of the evaluation:success
orerror
.result
(Any) - The completion value of evaluating the given code.kind
(String) - The kind of error occured during the evaluation.reason
(Error) - The Error object.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
switch()
Iterates through cases
and evaluates each case helper function or predicate
. Returns the result of the corresponding function or function
expression from the first case
whose predicate is truthy.
switch(case1, [case2, [...]]])
Arguments
cases
(Expression, Array)The case helper functions (case_when(), case_with(), case_default()) expressions or predicate-function pairs [
predicate
,function
].predicate
(Expression | Array | Object | String) – the predicate to evaluate, with function context. Optional for the last pair (default condition).function
(Expression | Array | Object | String) – the value or the expression to evaluate whenpredicate
is truthy. Uses the function context.
Returns
- (Any)
- The result of the evaluated function.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
times()
Invokes the iteratee
n
times, returning an array of the results of each invocation. The iteratee
is invoked with one argument: (index
).
times(n, [iteratee])
Arguments
n
(Number)- The number of times to invoke
iteratee
. iteratee
(Expression)- (Optional) The expression invoked per iteration. The default value is
&@
. Theiteratee
is invoked with one argument: (index
). Theindex
starts from0
.
Returns
- (Array)
- The array of results.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
to_blob()
Creates a File from a plain string, blob, file, array buffer or typed array that can be submitted with an HTTP Connector using multipart/form-data
encoding type. If the fileName
is not provided, it returns a Blob object. If the input
is already Blob or File, will use provided fileName and mimeType to override original values.
to_blob(input, [fileName, [mimeType]])
Arguments
input
(String, Blob, Array, ArrayBuffer, TypedArray)- String, Blob, Array, ArrayBuffer or TypedArray, that will be converted to Blob object.
fileName
(String)- (Optional) Filename to attach to Blob object.
mimeType
(String)- (Optional) Mime-Type to associate with a string, i.e. text/plain.
Returns
- (Blob)
- The Blob or File object.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
to_formdata()
Given plain object or array of key-value pairs creates FormData object that can be submitted with an HTTP Connector using multipart/form-data
encoding type.
to_formdata(pairs)
Arguments
Returns
- (FormData)
- The FormData object.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
See Also
to_json()
Alias for json_stringify().
to_path()
Converts value
to a property path array.
to_path(value)
Arguments
value
(Any)- The value to convert.
Returns
- (Array)
- The new property path array.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
try()
Evaluates function
expression and returns either the onSuccess
result with the function
's output as context, or the onFailure
result with the error object as context if it fails.
try(function, onSuccess, [onFailure])
Arguments
function
(Expression)- The expression to evaluate.
onSuccess
(Expression, Any)- The value or the expression to evaluate when the
function
succeeds. The expression will be evaluated using thefunction
's result as context. onFailure
(Expression, Any)- (Optional) The value or the expression to evaluate when the
function
fails. The expression will be evaluated using the error object as context. Defaults to&null
.
Returns
- (Any)
- The value returned by either
onSuccess
oronFailure
, depending on which one is evaluated.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
uuid_nil()
The "nil" UUID, a special case, is the UUID 00000000-0000-0000-0000-000000000000
; that is, all bits set to zero.
uuid_nil()
Returns
- (String)
- The "nil" UUID string.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
uuid_v1()
Generates a universally unique identifier (UUID) string, version 1 (timestamp).
uuid_v1()
Returns
- (String)
- The UUID string.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
uuid_v4()
Generates pseudo-random universally unique identifier (UUID) string, version 4 (random).
uuid_v4()
Returns
- (String)
- The random UUID string.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
uuid_v5()
Generates a name-based UUID, as described in RFC 4122 section 4.3, also known as a "version 5" UUID. Unlike the pseudo-random UUIDs generated by uuid_v4(), name-based UUIDs derive from namespace
and an name
, producing the same UUID value every time if the namespace
and name
are unchanged.
uuid_v5(name, namespace)
Arguments
name
(String)- The name of UUID.
namespace
(String)- The namespace of UUID. Use uuid_v5_ns_url() or uuid_v5_ns_dns() to generate custom URL or DNS namespace.
Returns
- (String)
- The UUID string.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
uuid_v5_ns_dns()
Returns DNS UUID namespace string (6ba7b810-9dad-11d1-80b4-00c04fd430c8
).
uuid_v5_ns_dns()
Returns
- (String)
- The DNS UUID namespace.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
uuid_v5_ns_url()
Returns URL UUID namespace string (6ba7b811-9dad-11d1-80b4-00c04fd430c8
).
uuid_v5_ns_url()
Returns
- (String)
- The URL UUID namespace.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
uuid_v7()
Generates a time-ordered UUID, as described in RFC 9562 section 5.7, also known as a "version 7" UUID. Has improved entropy characteristics over UUIDs generated by uuid_v1().
uuid_v7()
Returns
- (String)
- The UUID string.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
uuid_validate()
Checks is string
is a valid UUID.
uuid_validate(value)
Arguments
value
(String)- The value to inspect.
Returns
- (String)
- true if
value
is a valid UUID, false otherwise.
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
uuid_version()
Detect version of a valid UUID string.
uuid_version(value)
Arguments
value
(String)- The value to inspect.
Returns
Examples
Input | Expression | Result |
---|---|---|
|
|
|
|
|
|
|
|
|