* Update and fix JS docs This could really use some automation, atleast for API reference There are TypeScript definitions and typedocs, we don't need to be monkeys copying and reformatting this to API reference by hand * Fix bugged character * JS: Fix Number.toString() with decimals * Fix * Forgot this one * docs: mention per-view child format * Added @portasynthinca3 to docs' codeowners * Updated CODEOWNERS --------- Co-authored-by: Anna Antonenko <portasynthinca3@gmail.com> Co-authored-by: hedger <hedger@nanode.su> Co-authored-by: hedger <hedger@users.noreply.github.com>
5.2 KiB
Built-in methods
require()
Load a module plugin.
Parameters
- Module name
Examples
let serial = require("serial"); // Load "serial" module
delay()
Parameters
- Delay value in ms
Examples
delay(500); // Delay for 500ms
print()
Print a message on a screen console.
Parameters The following argument types are supported:
- String
- Number
- Bool
- undefined
Examples
print("string1", "string2", 123);
Console object
Same as print
, but output to serial console only, with corresponding log level.
console.log()
console.warn()
console.error()
console.debug()
load()
Runs a JS file and returns value from it.
Parameters
- The path to the file
- An optional object to use as the global scope while running this file
Examples
load("/ext/apps/Scripts/script.js");
chr()
Convert an ASCII character number to string.
Examples
chr(65); // "A"
die()
Exit JavaScript with given message.
Examples
die("Some error occurred");
parseInt()
Convert a string to number with an optional base.
Examples
parseInt("123"); // 123
parseInt("7b", 16); // 123
Number object
Number.toString()
Convert a number to string with an optional base.
Examples
let num = 123;
num.toString(); // "123"
num.toString(16); // "0x7b"
ArrayBuffer object
Fields
- byteLength: The length of the buffer in bytes
ArrayBuffer.slice()
Creates an ArrayBuffer
that contains a sub-part of the buffer.
Parameters
- The index to start the new buffer at
- An optional non-inclusive index of where to stop the new buffer
Examples
Uint8Array([1, 2, 3]).buffer.slice(0, 1) // ArrayBuffer([1])
DataView objects
Wrappers around ArrayBuffer
objects, with dedicated types such as:
Uint8Array
Int8Array
Uint16Array
Int16Array
Uint32Array
Int32Array
Fields
- byteLength: The length of the buffer in bytes
- length: The length of the buffer in typed elements
- buffer: The underlying
ArrayBuffer
Array object
Fields
- length: How many elements there are in the array
Array.splice()
Removes elements from the array and returns them in a new array.
Parameters
- The index to start taking elements from
- An optional count of how many elements to take
Examples
let arr = [1, 2, 3];
arr.splice(1); // [2, 3]
arr; // [1]
Array.push()
Adds a value to the end of the array.
Examples
let arr = [1, 2];
arr.push(3);
arr; // [1, 2, 3]
String object
Fields
- length: How many characters there are in the string
String.charCodeAt()
Returns the character code at an index in the string.
Examples
"A".charCodeAt(0) // 65
String.at()
Same as String.charCodeAt()
.
String.indexOf()
Return index of first occurrence of substr within the string or -1
if not found.
Parameters
- Substring to search for
- Optional index to start searching from
Examples
"Example".indexOf("amp") // 2
String.slice()
Return a substring between two indices.
Parameters
- The index to start the new string at
- An optional non-inclusive index of where to stop the new string
Examples
"Example".slice(2) // "ample"
String.toUpperCase()
Transforms the string to upper case.
Examples
"Example".toUpperCase() // "EXAMPLE"
String.toLowerCase()
Transforms the string to lower case.
Examples
"Example".toLowerCase() // "example"
__dirname
Path to the directory containing the current script.
Examples
print(__dirname); // /ext/apps/Scripts
__filename
Path to the current script file.
Examples
print(__filename); // /ext/apps/Scripts/path.js
SDK compatibility methods
sdkCompatibilityStatus()
Checks compatibility between the script and the JS SDK that the firmware provides.
Returns
"compatible"
if the script and the JS SDK are compatible"firmwareTooOld"
if the expected major version is larger than the version of the firmware, or if the expected minor version is larger than the version of the firmware"firmwareTooNew"
if the expected major version is lower than the version of the firmware
Examples
sdkCompatibilityStatus(0, 3); // "compatible"
isSdkCompatible()
Checks compatibility between the script and the JS SDK that the firmware provides in a boolean fashion.
Examples
isSdkCompatible(0, 3); // true
checkSdkCompatibility()
Asks the user whether to continue executing the script if the versions are not compatible. Does nothing if they are.
Examples
checkSdkCompatibility(0, 3);
doesSdkSupport()
Checks whether all of the specified extra features are supported by the interpreter.
Examples
doesSdkSupport(["gui-widget"]); // true
checkSdkFeatures()
Checks whether all of the specified extra features are supported by the interpreter, asking the user if they want to continue running the script if they're not.
Examples
checkSdkFeatures(["gui-widget"]);