diff --git a/src/components/CacheFS.brs b/src/components/CacheFS.brs index ee02c64..bf861b2 100644 --- a/src/components/CacheFS.brs +++ b/src/components/CacheFS.brs @@ -59,18 +59,18 @@ function CacheFS(folder = "kopytkoCache" as String) as Object ' Clears all data from cachefs under the specific directory ' @returns {Boolean} true if all directory data is successfully removed - prototype.clear = sub () as Boolean + prototype.clear = function () as Boolean if (m._fileSystem.delete(m._PREFIX + m._folder)) return m._createFolder() end if return false - end sub + end function ' @private - prototype._createFolder = sub () as Boolean + prototype._createFolder = function () as Boolean return m._fileSystem.createDirectory(m._PREFIX + m._folder) - end sub + end function ' @private prototype._hash = function (text as String) as String diff --git a/src/components/_tests/buildUrl.test.brs b/src/components/_tests/buildUrl.test.brs index ceb54ef..131aa31 100644 --- a/src/components/_tests/buildUrl.test.brs +++ b/src/components/_tests/buildUrl.test.brs @@ -42,18 +42,14 @@ function TestSuite__buildUrl() as Object ts.addTest("returns the proper URL for multiple params", function (ts as Object) as String ' Given path = "/example-path" - params = { paramOne: "valueOne", paramTwo: "valueTwo" } - ' Because "for each" loop for AAs doesn't guarantee the order, the result may be one of the following - expectedUrls = [ - "/example-path?paramone=valueOne¶mtwo=valueTwo", - "/example-path?paramtwo=valueTwo¶mone=valueOne", - ] + params = { paramOne: "valueOne", paramTwo: 2, paramThree: false } + expectedUrl = "/example-path?paramone=valueOne¶mthree=false¶mtwo=2" ' When result = buildUrl(path, params) ' Then - return ts.assertArrayContains(expectedUrls, result) + return ts.assertEqual(result, expectedUrl) end function) ts.addTest("returns URL with encoded space sign", function (ts as Object) as String @@ -82,10 +78,10 @@ function TestSuite__buildUrl() as Object return ts.assertEqual(result, expectedUrl) end function) - ts.addTest("ignores empty string parameter", function (ts as Object) as String + ts.addTest("ignores parameters that can't be converted to non-empty string", function (ts as Object) as String ' Given path = "/example-path" - params = { parameter: "" } + params = { param1: "", param2: ["p2"], param3: { p3: "p3"}, param4: CreateObject("roSGNode", "Node"), param5: invalid } expectedUrl = "/example-path" ' When diff --git a/src/components/buildUrl.brs b/src/components/buildUrl.brs index 91edc47..c3d4f12 100644 --- a/src/components/buildUrl.brs +++ b/src/components/buildUrl.brs @@ -3,7 +3,7 @@ ' ?buildUrl("http://myurl.com", { queryString: "123" }) ' ' prints http://myurl.com?queryString=123 ' @param {String} path -' @param {Object} [params=Invalid] - The AA of strings. +' @param {Object} [params=Invalid] - The AA of values convertible to string. ' @returns {String} function buildUrl(path as String, params = Invalid as Object) as String ' It might be encoded already. To avoid encoding encoded url it needs to be decoded first. @@ -14,10 +14,18 @@ function buildUrl(path as String, params = Invalid as Object) as String end if paramParts = [] - for each paramKey in params - value = params[paramKey] + for each paramKey in params.keys() + rawValue = params[paramKey] + value = Invalid + + if (rawValue <> Invalid AND GetInterface(rawValue, "ifToStr") <> Invalid) + value = rawValue.toStr() + end if + if (value <> Invalid AND value <> "") paramParts.push(paramKey.encodeUriComponent() + "=" + value.encodeUriComponent()) + else + ?"buildUrl: '";paramKey;"' param is ignored because it can't be converted to string" end if end for