diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/path.js b/applications/system/js_app/examples/apps/Scripts/js_examples/path.js index 0381150d2..9d3159d9f 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/path.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/path.js @@ -2,7 +2,7 @@ let storage = require("storage"); print("script has __dirpath of" + __dirpath); print("script has __filepath of" + __filepath); -if (storage.exists(__dirpath + "/math.js")) { +if (storage.fileExists(__dirpath + "/math.js")) { print("math.js exist here."); } else { print("math.js does not exist here."); diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js b/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js index 9d873af97..5f273c628 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js @@ -1,43 +1,29 @@ let storage = require("storage"); let path = "/ext/storage.test"; -function arraybuf_to_string(arraybuf) { - let string = ""; - let data_view = Uint8Array(arraybuf); - for (let i = 0; i < data_view.length; i++) { - string += chr(data_view[i]); - } - return string; -} - -print("File exists:", storage.exists(path)); +print("File exists:", storage.fileExists(path)); print("Writing..."); -// write(path, data, offset) -// If offset is specified, the file is not cleared, content is kept and data is written at specified offset -// Takes both strings and array buffers -storage.write(path, "Hello "); +let file = storage.openFile(path, "w", "create_always"); +file.write("Hello "); +file.close(); -print("File exists:", storage.exists(path)); +print("File exists:", storage.fileExists(path)); -// Append will create the file even if it doesnt exist! -// Takes both strings and array buffers -storage.append(path, "World!"); +file = storage.openFile(path, "w", "open_append"); +file.write("World!"); +file.close(); print("Reading..."); -// read(path, size, offset) -// If no size specified, total filesize is used -// If offset is specified, size is capped at (filesize - offset) -let data = storage.read(path); -// read returns an array buffer, to allow proper usage of raw binary data -print(arraybuf_to_string(data)); +file = storage.openFile(path, "r", "open_existing"); +let text = file.read("ascii", 128); +file.close(); +print(text); print("Removing...") storage.remove(path); print("Done") -// There's also: -// storage.copy(old_path, new_path); -// storage.move(old_path, new_path); -// storage.mkdir(path); \ No newline at end of file +// You don't need to close the file after each operation, this is just to show some different ways to use +// There's also many more functions and options, check types docs in firmware repo \ No newline at end of file diff --git a/applications/system/js_app/types/global.d.ts b/applications/system/js_app/types/global.d.ts index b3e4aac33..01a8fbcef 100644 --- a/applications/system/js_app/types/global.d.ts +++ b/applications/system/js_app/types/global.d.ts @@ -56,6 +56,15 @@ declare const __filepath: string; */ declare function load(path: string): any; +/** + * @brief Return 1-byte string whose ASCII code is the integer `n` + * + * If `n` is not numeric or outside of `0-255` range, `null` is returned + * + * @param n The ASCII code to convert to string + */ +declare function chr(n: number): string | null; + /** * @brief mJS Foreign Pointer type * @@ -189,6 +198,18 @@ declare class String { * See `charCodeAt` */ at(index: number): number; + /** + * @brief Return index of first occurence of substr within the string or `-1` if not found + * @param substr The string to search for + * @param fromIndex The index to start searching from + */ + indexOf(substr: string, fromIndex?: number): number; + /** + * @brief Return a substring between two indices + * @param start The index to start substring at + * @param end The index to end substring at + */ + slice(start: number, end?: number): string; } declare class Boolean { } diff --git a/applications/system/js_app/types/gui/byte_input.ts b/applications/system/js_app/types/gui/byte_input.d.ts similarity index 100% rename from applications/system/js_app/types/gui/byte_input.ts rename to applications/system/js_app/types/gui/byte_input.d.ts