{"id":13908062,"url":"https://github.com/juliomalves/roku-libs","last_synced_at":"2025-07-18T06:32:35.405Z","repository":{"id":45833674,"uuid":"98805193","full_name":"juliomalves/roku-libs","owner":"juliomalves","description":"Compilation of utilities for Roku development","archived":false,"fork":false,"pushed_at":"2024-02-17T16:05:39.000Z","size":216,"stargazers_count":59,"open_issues_count":1,"forks_count":11,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-08-07T23:55:34.956Z","etag":null,"topics":["brightscript","cache","google-analytics","roku","roku-development","utilities"],"latest_commit_sha":null,"homepage":"","language":"Brightscript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juliomalves.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-07-30T15:05:11.000Z","updated_at":"2024-07-03T00:44:50.000Z","dependencies_parsed_at":"2024-02-11T18:25:11.727Z","dependency_job_id":null,"html_url":"https://github.com/juliomalves/roku-libs","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomalves%2Froku-libs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomalves%2Froku-libs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomalves%2Froku-libs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomalves%2Froku-libs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliomalves","download_url":"https://codeload.github.com/juliomalves/roku-libs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226361669,"owners_count":17612939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["brightscript","cache","google-analytics","roku","roku-development","utilities"],"created_at":"2024-08-06T23:02:26.515Z","updated_at":"2024-11-25T16:31:24.517Z","avatar_url":"https://github.com/juliomalves.png","language":"Brightscript","funding_links":[],"categories":["HarmonyOS","Players \u0026 Clients"],"sub_categories":["Windows Manager","Hardware Players"],"readme":"# Roku Libs\n\nLibraries and utilities for BrightScript development.\n\nThis includes the following libraries and utilities:\n\n-   [Google Analytics](#google-analytics) (deprecated - native support now exists through [Roku Analytics Component Library](https://developer.roku.com/en-gb/docs/developer-program/libraries/roku-analytics-component.md#google-analytics))\n-   [HTTP Request](#http-request)\n-   [Cache](#cache)\n-   [Console](#console-utilities)\n-   [Array](#array-utilities)\n-   [String](#string-utilities)\n-   [Math](#math-utilities)\n-   [Registry](#registry-utilities)\n\n## Getting Started\n\nThe libraries and utilities are bundled into a sample project which sole purpose is to run unit tests built with [Roku Unit Testing Framework](https://github.com/rokudev/unit-testing-framework).\nAny file can and should be used separately as they do not have dependencies with each other.\n\n### Makefile\n\nThe `Makefile` included provides a few simple rules to help with the app installation and testing.\n\nFrom a terminal, you can first start by exporting the following variables\n\n```bash\nexport DEVICEIP=\u003cyour_device_IP\u003e\nexport ROKU=\u003cyour_dev_mode_user\u003e\nexport ROKUPASS=\u003cyour_dev_mode_password\u003e\n```\n\nTo run the unit tests (output will be available on the [debug console](https://developer.roku.com/en-gb/docs/developer-program/debugging/debugging-channels.md))\n\n```bash\nmake tests\n```\n\n## Description\n\n### [Google Analytics](./source/libs/google-analytics.brs)\n\n**Note: An [official library](https://developer.roku.com/en-gb/docs/developer-program/libraries/roku-analytics-component.md) now exists with support for Google Analytics, Omniture, Brightcove and Ooyala.**\n\nThis library provides tracking capabilities by sending data reports to Google Analytics using the [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/reference).\nBelow are some example usages on how to use its different tracking functionalities.\n\nTo retrieve the GA global singleton object\n\n```javascript\ngoogleAnalytics = GoogleAnalyticsLib();\n```\n\nAfter having a reference to the GA global (we'll be using `googleAnalytics` in the following examples), initialise it by passing one or more Tracking IDs. This is required in order to enable reporting altogether, but only needs to be done **once**.\n\n```javascript\ngoogleAnalytics.init(\"UA-12345678-90\")\n//Or when passing multiple Tracking IDs in an array\ngoogleAnalytics.init([\"UA-12345678-90\", ..., \"UA-09876543-21\"])\n```\n\nSending tracking reports\n\n```javascript\n// Event tracking\ngoogleAnalytics.trackEvent({ category: \"application\", action: \"launch\" });\n\n// Screen Tracking\ngoogleAnalytics.trackScreen({ name: \"mainScreen\" });\n\n// E-Commerce tracking\ngoogleAnalytics.trackTransaction({ id: \"OD564\", revenue: \"10.00\" });\ngoogleAnalytics.trackItem({\n    transactionId: \"OD564\",\n    name: \"Test01\",\n    price: \"10.00\",\n    code: \"TEST001\",\n    category: \"vod\",\n});\n\n// Timing tracking\ngoogleAnalytics.trackTiming({\n    category: \"category\",\n    variable: \"timing\",\n    time: \"1000\",\n});\n\n// Exception tracking\ngoogleAnalytics.trackException({ description: \"description\", isFatal: \"1\" });\n```\n\nAdding custom parameters to all tracking reports\n\n```javascript\ngoogleAnalytics.setParams({ sr: \"1280x800\", ul: \"en-gb\" });\n```\n\n---\n\n### [HTTP Request](./source/libs/http-request.brs)\n\nA library that makes HTTP requests easier to deal with in BrightScript.\n\nBelow are some examples showing its different capabilities.\n\n#### Request with 2s timeout and 3 retries\n\n```javascript\nrequest = HttpRequest({\n    url: \"https://postman-echo.com/delay/5\",\n    timeout: 2000,\n    retries: 3,\n});\nresponse = request.send();\n```\n\n#### POST request with 'application/json' Content-Type\n\n```javascript\nrequest = HttpRequest({\n    url: \"https://postman-echo.com/post\",\n    method: \"POST\",\n    headers: { \"Content-Type\": \"application/json\" },\n    data: { user: \"johndoe\", password: \"12345\" },\n});\nresponse = request.send();\n```\n\n#### Abort on-going request\n\n```javascript\nrequest = HttpRequest({\n    url: \"https://postman-echo.com/delay/5\",\n});\nrequest.send();\nrequest.abort();\n```\n\n---\n\n### [Cache](./source/utils/cache.brs)\n\nThe `Cache` utility creates a simple interface for caching data to [Roku's volatile storage](https://developer.roku.com/en-gb/docs/developer-program/getting-started/architecture/file-system.md). Uses `cachefs:` storage by default, but can be overwritten to `tmp:` storage.\n\n```\nCacheUtil(key [, options])\n```\n\nAccepts a `key` string and an optional `options` object as second parameter, which can have the following fields:\n\n-   `algorithm`: `String` - hash algorithm for the key hashing, which will be used for the stored file name. **Defaults to `\"sha1\"`**.\n-   `storage`: `String` - storage to be used by the cache. **Defaults to `\"cachefs:/\"`**.\n-   `ttl`: `Integer` - time to live (in seconds) for the cache, after which the cache will expire. If set to `invalid` the cache will never expire. **Defaults to `5` seconds**.\n\n#### Use Case\n\nAs a sample use case, `CacheUtil` can be used to cache the response of a given HTTP request. Assuming a request was made to `https://postman-echo.com/get` and `responseString` contains its response as a `string` value.\n\nThe response can be stored in cache as follows:\n\n```javascript\ncache = CacheUtil(\"https://postman-echo.com/get\");\ncache.put(responseString);\n```\n\nOn a following request to the same URL the cached response could be retrieved before making a new request, given the TTL expiry hasn't been reached yet.\n\n```javascript\ncache = CacheUtil(\"https://postman-echo.com/get\");\ncachedValue = cache.get(); // Returns response string that was cached previously\n```\n\n---\n\n### [Console Utilities](./source/utils/console.brs)\n\nSmall logging utility that enhances the built-in `print` debugging capabilities, with a syntax similiar to JavaScript's `console` object. It adds a timestamp to every log, provides group indentation, timers, counters, and different logging levels (`info`, `assert` and `error`).\n\nExample usages:\n\n```javascript\nconsole = ConsoleUtil();\nconsole.log(\"Hello World\"); // [14:56:16:891] Hello World\nconsole.time(\"Hello World\"); // [14:56:16:891] Hello World: timer started\nconsole.group();\nconsole.log(\"Hello World\"); // [14:56:16:892]     Hello World\nconsole.count(); // [14:56:16:894]     default: 1\nconsole.info(\"Hello World\"); // [14:56:16:893]     [INFO] Hello World\nconsole.group();\nconsole.assert(false, \"Hello World\"); // [14:56:16:894]         [ASSERT] Hello World\nconsole.count(); // [14:56:16:894]         default: 2\nconsole.groupEnd();\nconsole.assert(true, \"Hello World\");\nconsole.error(\"Hello World\"); // [14:56:16:895]     [ERROR] Hello World\nconsole.count(); // [14:56:16:894]     default: 3\nconsole.groupEnd();\nconsole.timeEnd(\"Hello World\"); // [14:56:16:895] Hello World: 4ms\n```\n\n---\n\n### [Array Utilities](./source/utils/array.brs)\n\nThis utility expands the array functionalities provided by the built-in `roArray` type. It implements the following functions: `isArray`, `contains`, `indexOf`, `lastIndexOf`, `slice`, `fill`, `flat`, `map`, `reduce`, `filter`, `find`, `findIndex`, `every`, `some`, `groupBy`.\n\nExample usages:\n\n```javascript\narrUtil = ArrayUtil();\narr = [5, 2, 3, 2, 1];\n\narrUtil.isArray(arr); // true\narrUtil.contains(arr, 2); // true\narrUtil.indexOf(arr, 2); // 1\narrUtil.lastIndexOf(arr, 2); // 3\narrUtil.slice(arr, 1, 3); // [2,3,2]\narrUtil.fill(arr, 0, 1, 3); // [5,0,0,0,1]\narrUtil.flat([0, 1, 2, [3, 4]]); // [0,1,2,3,4]\n\n// mapFunc = function(element, index, arr)\n//     return element + 1\n// end function\narrUtil.map(arr, mapFunc); // [6,3,4,3,2]\n\n// reduceFunc = function(acc, element, index, arr)\n//     return acc + element\n// end function\narrUtil.reduce(arr, reduceFunc); // 13\narrUtil.reduce(arr, reduceFunc, 5); // 18\n\n// filterFunc = function(element, index, arr)\n//     return element \u003e 2\n// end function\narrUtil.filter(arr, filterFunc); // [5,3]\n\n// testFunc = function(element, index, arr)\n//     return element \u003e 2\n// end function\narrUtil.find(arr, testFunc); // 5\narrUtil.findIndex(arr, testFunc); // 0\narrUtil.every(arr, testFunc); // false\narrUtil.some(arr, testFunc); // true\n\ngroupArr = [\n    { name: \"asparagus\", type: \"vegetables\" },\n    { name: \"bananas\", type: \"fruit\" },\n    { name: \"cherries\", type: \"fruit\" },\n];\narrUtils.groupBy(groupArr, \"type\"); // { vegetables: [{ name: \"asparagus\", type: \"vegetables\" }], fruit: [{ name: \"bananas\", type: \"fruit\" }, { name: \"cherries\", type: \"fruit\" }] }\n```\n\n---\n\n### [String Utilities](./source/utils/string.brs)\n\nThis utility expands the string functionalities provided by the built-in `String`/`roString` types. It implements the following functions: `isString`, `charAt`, `startsWith`, `endsWith`, `contains`, `indexOf`, `match`, `replace`, `truncate`, `repeat`, `padStart`, `padEnd`, `concat`, `toString`, `toMD5`, `toSHA1`, `toSHA256`, `toSHA512`.\n\nExample usages:\n\n```javascript\nstrUtil = StringUtil();\nstr = \"AbraCadabra\";\n\nstrUtil.isString(str); // true\nstrUtil.charAt(str, 1); // \"b\"\nstrUtil.startsWith(str, \"Abra\"); // true\nstrUtil.endsWith(str, \"Cadabra\"); // true\nstrUtil.contains(str, \"bra\"); // true\nstrUtil.indexOf(str, \"ra\"); // 2\nstrUtil.match(str, \"(ab)(ra)\", \"i\"); // [\"Abra\",\"Ab\",\"ra\"]\nstrUtil.replace(str, \"Cad\", \"-\"); // \"Abra-abra\"\nstrUtil.truncate(str, 4, \"...\"); // \"Abra...\"\nstrUtil.repeat(str, 2); // \"AbraCadabraAbraCadabra\"\nstrUtil.padStart(str, 15); // \"    AbraCadabra\"\nstrUtil.padEnd(str, 15); // \"AbraCadabra    \"\nstrUtil.concat(str, \" Cadabra\"); // \"AbraCadabra Cadabra\"\nstrUtil.toString([\"1\", 2, true]); // \"[1,2,true]\"\nstrUtil.toMD5(str); // \"3aa51d002ab23a353b13df9ba059b4fc\"\n```\n\n---\n\n### [Math Utilities](./source/utils/string.brs)\n\nThis utility provides additional mathematical constants and functions for BrightScript. It implements the following functions: `isNumber`, `isInt`, `isFloat`, `isDouble`, `ceil`, `floor`, `round`, `min`, `max`, `power`.\n\nExample usages:\n\n```javascript\nmath = MathUtil();\n\nmath.isNumber(1.4); // true\nmath.ceil(1.4); // 2\nmath.floor(1.4); // 1\nmath.round(1.4); // 1\nmath.round(1.4159, 3); // 1.416\nmath.min(0, 3); // 0\nmath.max(0, 3); // 3\nmath.power(2, 8); // 256\nmath.E; // 2.718281828459\nmath.PI; // 3.1415926535898\n```\n\n---\n\n### [Registry Utilities](./source/utils/registry.brs)\n\nThis utility makes it easier to deal with `roRegistry` and `roRegistrySection` objects by simplifiyng all registry functions. It implements the following functions: `read`, `write`, `delete`, `readSection`, `deleteSection`, `getSections`, `clear`.\n\nExample usages:\n\n```javascript\nregistry = RegistryUtil();\n\nregistry.write(\"myKey\", \"myValue\", \"mySection\"); // Replaces the value of the specified key for a given section\nregistry.read(\"myKey\", \"mySection\"); // Reads and returns the value of the specified key from a given section\nregistry.delete(\"myKey\", \"mySection\"); // Deletes the specified key from a given section\nregistry.readSection(\"mySection\"); // Reads and return all key values from a given section\nregistry.deleteSection(\"mySection\"); // Deletes all key values from a specified section\nregistry.getSections(); // Returns all sections in the registry\nregistry.clear(); // Deletes all sections from the registry\n```\n\n## Contributing\n\nFeel free to submit any pull request or issue to contribute to this project. Suggestions for new utilities or features are also welcomed.\n\n## Authors\n\n-   [juliomalves](https://github.com/juliomalves) - Initial work \u0026 maintainer\n\n## License\n\nThis project is licensed under the [MIT License](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliomalves%2Froku-libs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliomalves%2Froku-libs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliomalves%2Froku-libs/lists"}