diff --git a/.gitignore b/.gitignore index c2658d7..c9e397f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +coverage/* diff --git a/build/test.html b/build/test.html index 8f9b65e..efbeae3 100644 --- a/build/test.html +++ b/build/test.html @@ -52,6 +52,11 @@ document.addEventListener('DOMContentLoaded', function() { console.log('AELX OUT: %o', aelx_out); build_test.innerHTML += aelx_out; + // console.log("TEST URL: %o", parseQueryString("http://foor.org/?key=value")); + console.log("TEST URL: %o", getQueryStringParam('', "http://foor.org/?param=foo¶m=other", true)); + console.log("TEST URL: %o", parseQueryString("http://foor.org/?param=foo¶m=other", '')); + // console.log("TEST URL: %o", getQueryStringParam('', "http://foor.org/?key=value&key=other&key=value&bar=")); + // console.log('TR: %s', l10n.__('Original')); // console.log('TR: %s', l10n.__('Not exists')); }); diff --git a/coverage/.gitignore b/coverage/.gitignore deleted file mode 100644 index d6b7ef3..0000000 --- a/coverage/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/src/utils/FormatBytes.mjs b/src/utils/FormatBytes.mjs index 0066262..400901b 100644 --- a/src/utils/FormatBytes.mjs +++ b/src/utils/FormatBytes.mjs @@ -19,6 +19,9 @@ function formatBytes(bytes) if (typeof bytes === "bigint") { bytes = Number(bytes); } + if (isNaN(bytes)) { + return bytes.toString(); + } do { bytes = bytes / 1024; i++; diff --git a/src/utils/UrlParser.mjs b/src/utils/UrlParser.mjs index eb54e04..e40d87b 100644 --- a/src/utils/UrlParser.mjs +++ b/src/utils/UrlParser.mjs @@ -5,9 +5,10 @@ Creator: Clemens Schwaighofer */ export { parseQueryString, getQueryStringParam }; -import { keyInObject } from './JavaScriptHelpers.mjs'; /** + * NOTE: this original code was wrong, now using URL and parsing through + * getQueryStringParam * parses a query string from window.location.search.substring(1) * ALTERNATIVE CODE * var url = new URL(window.location.href); @@ -16,44 +17,13 @@ import { keyInObject } from './JavaScriptHelpers.mjs'; * if not set will auto fill * @param {String} [return_key=''] if set only returns this key entry * or empty for none + * @param {Boolean} [single=false] if set to true then only the first found + * will be returned * @return {Object|String} parameter entry list */ -function parseQueryString(query = '', return_key = '') +function parseQueryString(query = '', return_key = '', single = false) { - if (!query) { - query = window.location.search.substring(1); - } - var vars = query.split('&'); - var query_string = {}; - for (var i = 0; i < vars.length; i++) { - var pair = vars[i].split('='); - var key = decodeURIComponent(pair[0]); - var value = decodeURIComponent(pair[1]); - // skip over run if there is nothing - if (!key || value === 'undefined') { - continue; - } - // If first entry with this name - if (typeof query_string[key] === 'undefined') { - query_string[key] = decodeURIComponent(value); - // If second entry with this name - } else if (typeof query_string[key] === 'string') { - var arr = [query_string[key], decodeURIComponent(value)]; - query_string[key] = arr; - // If third or later entry with this name - } else { - query_string[key].push(decodeURIComponent(value)); - } - } - if (return_key) { - if (keyInObject(return_key, query_string)) { - return query_string[return_key]; - } else { - return ''; - } - } else { - return query_string; - } + return getQueryStringParam(return_key, query, single); } /** diff --git a/tests/FormatBytes.test.js b/tests/FormatBytes.test.js index 83d2d3b..7e66332 100644 --- a/tests/FormatBytes.test.js +++ b/tests/FormatBytes.test.js @@ -7,6 +7,11 @@ import { } from '../src/utils/FormatBytes.mjs'; let bytes_map = [ + { + "in": "120MB", + "out": "120MB", + "out_l": "120MB", + }, { "in": -123123123, "out": "-120237.42kB", @@ -57,6 +62,7 @@ describe("formatBytes", () => { it('convert bytes to human readable, round up to next set', () => { // expect(formatBytes(1021152)).toBe('0.97MB'); for (const bytes of bytes_map) { + // @ts-ignore expect(formatBytes(bytes.in)).toBe(bytes.out); } }); @@ -66,6 +72,7 @@ describe("formatBytesLong", () => { it('convert bytes to human readable, keep on current set', () => { expect(formatBytesLong(1021152)).toBe('997.22 KB'); for (const bytes of bytes_map) { + // @ts-ignore expect(formatBytesLong(bytes.in)).toBe(bytes.out_l); } }); diff --git a/tests/HtmlElementCreator.test.js b/tests/HtmlElementCreator.test.js index 41e1508..e4957a2 100644 --- a/tests/HtmlElementCreator.test.js +++ b/tests/HtmlElementCreator.test.js @@ -257,6 +257,41 @@ describe("ael", () => { "tag": "div" } ); + let test_cel = hec.ael( + hec.cel('div', 'block-id'), + hec.cel('div', 'sub-id'), + ); + expect(hec.ael(test_cel, hec.cel('div', 'new-block'), 'sub-id')).toEqual( + { + "content": "", + "css": [], + "id": "block-id", + "name": undefined, + "options": {}, + "sub": [ + { + "content": "", + "css": [], + "id": "sub-id", + "name": undefined, + "options": {}, + "sub": [ + { + "content": "", + "css": [], + "id": "new-block", + "name": undefined, + "options": {}, + "sub": [], + "tag": "div" + } + ], + "tag": "div" + } + ], + "tag": "div" + } + ); }); }); @@ -640,11 +675,11 @@ describe("phfo", () => { "id": "block test", "in": hec.aelx(hec.cel('div', 'outer-id'), hec.aelx(hec.cel('div', 'inner-id'), - hec.cel('input', 'some-id', '', ['abc'], {"type": "button", "onclick": "send()"}) - + hec.cel('input', 'some-id', '', ['abc'], {"type": "button", "onclick": "send()"}), + hec.cel('div', '', 'content') ) ), - "out": '