Added StringHelpers, UniqIdGenerators and UrlParser test

UniqIdGenerators skips window.crypt test

URL tests need to be more in detail
This commit is contained in:
2025-03-07 19:01:58 +09:00
parent ea9882256e
commit 233fb6a235
8 changed files with 118 additions and 32 deletions

View File

@@ -145,10 +145,10 @@ Currently covered:
- FormatBytes - FormatBytes
- MathHelpers - MathHelpers
- StringHelpers
- UniqIdGenerators (Part)
- UrlParser
TODO: TODO:
- JavaScriptHelpers - JavaScriptHelpers
- StringHelpers
- UniqIdGenerators
- UrlParser

View File

@@ -14,28 +14,6 @@
</body> </body>
<script languagae="JavaScript"> <script languagae="JavaScript">
/**
* converts a int number into bytes with prefix in two decimals precision
* currently precision is fixed, if dynamic needs check for max/min precision
* @param {Number|BigInt} bytes bytes in int
* @return {String} string in GB/MB/KB
*/
function _formatBytes(bytes)
{
var i = -1;
// If this ia bigint -> convert to number, we need the decimals
if (typeof bytes === "bigint") {
bytes = Number(bytes);
}
do {
bytes = bytes / 1024;
i++;
} while (bytes > 99);
return (
Math.round(bytes * Math.pow(10, 2)) / Math.pow(10, 2)
) + ['kB', 'MB', 'GB', 'TB', 'PB', 'EB'][i];
}
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
console.log('JavaScript Utils Test'); console.log('JavaScript Utils Test');
let el = document.getElementById('test-div'); let el = document.getElementById('test-div');
@@ -52,10 +30,6 @@ document.addEventListener('DOMContentLoaded', function() {
el.innerHTML += '<div>formatBytes: ' + formatBytes(bytes) + '</div>'; el.innerHTML += '<div>formatBytes: ' + formatBytes(bytes) + '</div>';
el.innerHTML += '<div>formatBytesLong: ' + formatBytesLong(bytes) + '</div>'; el.innerHTML += '<div>formatBytesLong: ' + formatBytesLong(bytes) + '</div>';
// el.innerHTML += '<div>_formatBytes: ' + _formatBytes(1024n) + '</div>';
el.innerHTML += '<div>_formatBytes: ' + _formatBytes(1021152n) + '</div>';
el.innerHTML += '<div>_formatBytes: ' + _formatBytes(1021152) + '</div>';
// console.log('TR: %s', l10n.__('Original')); // console.log('TR: %s', l10n.__('Original'));
// console.log('TR: %s', l10n.__('Not exists')); // console.log('TR: %s', l10n.__('Not exists'));
}); });

View File

@@ -3,7 +3,8 @@
"version": "1.0.0", "version": "1.0.0",
"main": "", "main": "",
"scripts": { "scripts": {
"test": "node test test/Test*.js", "test": "vitest",
"coverage": "vitest run --coverage",
"utils-min-build": "node_modules/esbuild/bin/esbuild utils.min=src/utils.mjs --outdir=build/js/output/ --format=esm --bundle --charset=utf8 --tree-shaking=false --minify-whitespace --minify-syntax --sourcemap", "utils-min-build": "node_modules/esbuild/bin/esbuild utils.min=src/utils.mjs --outdir=build/js/output/ --format=esm --bundle --charset=utf8 --tree-shaking=false --minify-whitespace --minify-syntax --sourcemap",
"utils-build": "node_modules/esbuild/bin/esbuild utils=src/utils.mjs --outdir=build/js/output/ --format=esm --bundle --charset=utf8 --tree-shaking=false", "utils-build": "node_modules/esbuild/bin/esbuild utils=src/utils.mjs --outdir=build/js/output/ --format=esm --bundle --charset=utf8 --tree-shaking=false",
"utils-build-all": "npm run utils-min-build && npm run utils-build", "utils-build-all": "npm run utils-min-build && npm run utils-build",

View File

@@ -8,7 +8,7 @@ export { formatString, numberWithCommas, convertLBtoBR };
/** /**
* simple sprintf formater for replace * simple sprintf formater for replace
* usage: "{0} is cool, {1} is not".format("Alpha", "Beta"); * usage: formatString("{0} is cool, {1} is not", "Alpha", "Beta");
* First, checks if it isn't implemented yet. * First, checks if it isn't implemented yet.
* @param {String} string String with {..} entries * @param {String} string String with {..} entries
* @param {...any} args List of replacement * @param {...any} args List of replacement

View File

@@ -25,7 +25,7 @@ function generateId(len)
} }
/** /**
* creates a pseudo random string of 10 characters * creates a pseudo random string of 11 characters
* works on all browsers * works on all browsers
* after many runs it will create duplicates * after many runs it will create duplicates
* @return {String} not true random string * @return {String} not true random string

View File

@@ -0,0 +1,60 @@
import { describe, it, expect } from "vitest";
import {
formatString,
numberWithCommas,
convertLBtoBR
} from '../src/utils/StringHelpers.mjs';
describe("formatString", () => {
it('Should replace {n} entries in strings', () => {
expect(
formatString("{0} is cool, {1} is not", "Alpha", "Beta")
).toBe("Alpha is cool, Beta is not");
expect(
formatString("{0} is cool, {1} is not", "Alpha")
).toBe("Alpha is cool, {1} is not");
});
});
describe("numberWithCommas", () => {
it('Should format a number into a decimal formatted', () => {
let numbers_list = [
{
"in": 123,
"out": "123",
},
{
"in": 1234,
"out": "1,234"
},
{
"in": 12345,
"out": "12,345"
},
{
"in": 123456,
"out": "123,456"
},
{
"in": 1234567,
"out": "1,234,567"
},
{
"in": 1234567890,
"out": "1,234,567,890"
},
];
for (let number of numbers_list) {
expect(numberWithCommas(number.in)).toBe(number.out);
}
});
});
describe("convertLBtoBR", () => {
it('Should convert LF/CR to <br>', () => {
expect(convertLBtoBR("AB\nCD")).toBe("AB<br>CD");
});
});
// __END__

View File

@@ -0,0 +1,29 @@
import { describe, it, expect } from "vitest";
import {
// generateId,
randomIdF,
} from '../src/utils/UniqIdGenerators.mjs';
// window. is not defined in headless mode
// TODO: fix
/* describe("generateId", () => {
it('Should create a random id in defined length', () => {
let len_list = [
1, 12, 24, 32, 64
];
for (let len of len_list) {
expect(generateId(len)).lengthOf(len);
}
});
}); */
describe("randomIdF", () => {
it('Should create a 10 character long random id', () => {
let rand_id = randomIdF();
expect(rand_id).lengthOf(11);
expect(rand_id).match(/^[a-z0-9]{11}$/);
});
});
// __END__

22
tests/UrlParser.test.js Normal file
View File

@@ -0,0 +1,22 @@
import { describe, it, expect } from "vitest";
import {
parseQueryString,
getQueryStringParam,
} from '../src/utils/UrlParser.mjs';
describe("parseQueryString", () => {
it('Should parse query string for key', () => {
let kv = parseQueryString("http://foor.org?key=value");
expect(kv).toEqual({"http://foor.org?key": "value"});
});
});
describe("getQueryStringParam", () => {
it('Should parse query string for key', () => {
let kv = getQueryStringParam("key", "http://foor.org?key=value");
expect(kv).toEqual("value");
});
});
// __END__