-
Notifications
You must be signed in to change notification settings - Fork 0
Builtins
These built-in dicts are namespaces with separate documentations:
Name | Documentation Link |
---|---|
Class |
Class |
String |
String |
Array |
Array |
Dict |
Dict |
Function |
Function |
Number |
Number |
Math |
Math |
JSON |
JSON |
Promise |
Promise |
Time |
Time |
help(target)
Returns the help information of specific target.
injectHelp(helpInfo, target)
Inject help information into specific target.
(Returns target
after injection.)
Name | Type |
---|---|
true |
boolean |
false |
boolean |
Infinity |
number |
NaN |
number |
null |
null |
There's one more built-in variable: _
,
which is a magic variable in hxs.
Whenever a sentence(an expression separated by semicolons) is evaluated,
the result will be stored in _
.
(_
is initially null
.)
For example,
a = _;
x = 2;
b = _ + _;
c = String('c', _);
"----------------------+
| Now, a becomes null, |
| b becomes 4, |
| and c becomes 'c4'. |
+----------------------";
The placeholder is useful in certain cases, where you just want to store something temporarily. However, if you need to store some value for future use, using ordinary variables should be a better choice.
See Process Control.
See Basics#Variables.
getContextStore()
Returns current context store. (A dict that stores all the variables in current scope.)
See Module Usage.
typeOf(value)
Returns a string('number'
/'string'
/'boolean'
/'function'
/'null'
/'array'
/'dict'
)
according to the type of the given value.
same(a, b)
Returns true
if a
is the same as b
;
(That is, (a == b) || (isNaN(a) && isNaN(b))
.)
returns false
otherwise.
keys(dict)
Returns the keys of the given dict.
(Invokes dict.__keys
if available.)
remove(arrayOrDict, indexOrKey)
A utility function that removes specific index or key.
(Invokes arrayOrDict.__remove
if available.)
sizeOf(arrayOrString)
A utility function that combines Array.sizeOf
and String.sizeOf
.
slice(arrayOrString, begin = 0, end = sizeOf(arrayOrString))
A utility function that combines Array.slice
and String.slice
.
clone(arrayOrDict, indexOrKey)
A utility function that combines Array.clone
and Dict.clone
.
indexOf(arrayOrString, valueOrSubstring)
A utility function that combines Array.indexOf
and String.indexOf
.
lastIndexOf(arrayOrString, valueOrSubstring)
A utility function that combines Array.lastIndexOf
and String.lastIndexOf
.
includes(arrayOrString, valueOrSubstring)
A utility function that combines Array.includes
and String.includes
.
print(data...)
Print all arguments to the console.
assert(condition, errorMessage = 'assertion failed')
Perform an assertion:
if (!condition) {
raise(errorMessage);
};
number(value)
Built-in function number
converts the given value to a number
according to the following rules:
- If the value is already a number, return it as is;
- If the value is a string, try to parse it as a number
- If the value is a boolean, return
1
if it istrue
and return0
otherwise; and return the result(NaN
if it can't be parsed as a number); - Return
NaN
otherwise.
string(value, previewSize = 10, previewIndent = ' ')
Built-in function string
converts the given value to a string
according to the following rules:
- If the value is already a string, wrap it with proper quotes and return it;
- If the value is a number, return its default string representation;
- If the value is a boolean, return
'true'
if it istrue
and return'false'
otherwise; - If the value is an array, return it in preview format;
- If the value is a dict, return it in preview format;
- If the value is a function, return
'<function>'
.
When arrays/dicts are converted into strings,
they are displayed as preview format.
The option previewSize
specifies how many elements/entries are displayed.
(The rest will be omitted and ...
wil be shown.)
The option previewIndent
sets the indentation string of preview display.
When previewSize
is a non-positive value,
'<array>'
/'<dict>'
will be displayed instead;
Otherwise, arrays are displayed as:
(size: X) [
element0,
element1,
...
]
And dicts are displayed as:
(size: X) {
key0 -> value0,
key1 -> value1,
...
}
boolean(value)
Built-in function boolean
converts the given value to a boolean
according to the following rules:
- If the value is already a boolean, return it as is;
- If the value is a number, return
false
if it is zero and returntrue
otherwise; - If the value is a string, return
false
if it is empty and returntrue
otherwise; - Return
true
otherwise(function/array/dict).
These are builtin shortcuts:
Shortcut | Source |
---|---|
forEach |
Array.forEach |
map |
Array.map |
filter |
Array.filter |
assign |
Dict.assign |
isInvocable |
Function.isInvocable |
invoke |
Function.invoke |
bind |
Function.bind |
isNaN |
Number.isNaN |
getConstructorOf |
Class.getConstructorOf |
isInstanceOf |
Class.isInstanceOf |