Update isObject and isIterable, isArray
Some checks failed
JavaScriptUtilsVitest / ci-tests (push) Has been cancelled
Some checks failed
JavaScriptUtilsVitest / ci-tests (push) Has been cancelled
isObject only returns true for objects and nothing else. Update cel/ael in the HTML builder to use isArray for array check
This commit is contained in:
1458
package-lock.json
generated
1458
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,8 @@ import {
|
||||
isFunction as _isFunction,
|
||||
executeFunctionByName as _executeFunctionByName,
|
||||
isObject as _isObject,
|
||||
isArray as _isArray,
|
||||
isIterable as _isIterable,
|
||||
getObjectCount as _getObjectCount,
|
||||
keyInObject as _keyInObject,
|
||||
getKeyByValue as _getKeyByValue,
|
||||
@@ -85,6 +87,11 @@ import { l10nTranslation } from './utils/l10nTranslation.mjs';
|
||||
import { HtmlElementCreator } from './utils/HtmlElementCreator.mjs';
|
||||
import { ActionBox } from './utils/ActionBox.mjs';
|
||||
import { LoginNavMenu } from './utils/LoginNavMenu.mjs';
|
||||
import {
|
||||
isWebkit as _isWebkit,
|
||||
isMobileWebKit as _isMobileWebKit,
|
||||
isDesktopWebKit as _isDesktopWebKit
|
||||
} from './utils/BrowserDetect.mjs';
|
||||
|
||||
let aiob = new ActionIndicatorOverlayBox();
|
||||
let hec = new HtmlElementCreator();
|
||||
@@ -446,6 +453,28 @@ function isObject(val) // eslint-disable-line no-unused-vars
|
||||
return _isObject(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if variable is array
|
||||
* @param {any} val possible array
|
||||
* @returns {Boolean} true/false if it an array
|
||||
*/
|
||||
// @ts-ignore
|
||||
function isArray(val) // eslint-disable-line no-unused-vars
|
||||
{
|
||||
return _isArray(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if a variable is an iterable
|
||||
* @param {any} val possible iterable
|
||||
* @return {Boolean} true/false if it is an object or not
|
||||
*/
|
||||
// @ts-ignore
|
||||
function isIterable(val) // eslint-disable-line no-unused-vars
|
||||
{
|
||||
return _isIterable(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the length of an object (entries)
|
||||
* @param {Object} object object to check
|
||||
@@ -1200,4 +1229,20 @@ function adjustActionBoxHeight(target_id = 'actionBox', override = 0, content_ov
|
||||
ab.adjustActionBoxHeight(target_id, override, content_override);
|
||||
}
|
||||
|
||||
// MARK: BROWSER DETECTION
|
||||
function isWebkit() // eslint-disable-line no-unused-vars
|
||||
{
|
||||
return _isWebkit();
|
||||
}
|
||||
|
||||
function isMobileWebKit() // eslint-disable-line no-unused-vars
|
||||
{
|
||||
return _isMobileWebKit();
|
||||
}
|
||||
|
||||
function isDesktopWebKit() // eslint-disable-line no-unused-vars
|
||||
{
|
||||
return _isDesktopWebKit();
|
||||
}
|
||||
|
||||
/* END */
|
||||
|
||||
31
src/utils/BrowserDetect.mjs
Normal file
31
src/utils/BrowserDetect.mjs
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
Description: Browser detect
|
||||
Date: 2025/11.25
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
export { isWebkit, isMobileWebKit, isDesktopWebKit };
|
||||
|
||||
// Desktop Safari, all mobile WebKit browsers on iOS and webview iOS
|
||||
function isWebkit()
|
||||
{
|
||||
return "GestureEvent" in window;
|
||||
}
|
||||
|
||||
// all mobile webkit browsers and webview iOS
|
||||
function isMobileWebKit()
|
||||
{
|
||||
return "ongesturechange" in window;
|
||||
}
|
||||
|
||||
// Desktop Safari
|
||||
function isDesktopWebKit()
|
||||
{
|
||||
return (
|
||||
typeof window !== 'undefined' &&
|
||||
'safari' in window &&
|
||||
'pushNotification' in window.safari
|
||||
);
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: Date Time functions
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: DOM Helpers
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: Byte string formatting
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: DOM Management and HTML builder
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ export {
|
||||
// deprecated name
|
||||
HtmlElementCreator as DomManagement
|
||||
};
|
||||
import { deepCopyFunction, isObject } from './JavaScriptHelpers.mjs';
|
||||
import { deepCopyFunction, isObject, isArray } from './JavaScriptHelpers.mjs';
|
||||
|
||||
class HtmlElementCreator {
|
||||
/**
|
||||
@@ -50,7 +50,7 @@ class HtmlElementCreator {
|
||||
base.sub.push(deepCopyFunction(attach));
|
||||
} else {
|
||||
// sub check
|
||||
if (isObject(base.sub) && base.sub.length > 0) {
|
||||
if (isArray(base.sub) && base.sub.length > 0) {
|
||||
for (var i = 0; i < base.sub.length; i ++) {
|
||||
// recursive call to sub element
|
||||
this.ael(base.sub[i], attach, id);
|
||||
@@ -209,7 +209,7 @@ class HtmlElementCreator {
|
||||
}
|
||||
}
|
||||
// second CSS
|
||||
if (isObject(tree.css) && tree.css.length > 0) {
|
||||
if (isArray(tree.css) && tree.css.length > 0) {
|
||||
line += ' class="';
|
||||
for (i = 0; i < tree.css.length; i ++) {
|
||||
line += tree.css[i] + ' ';
|
||||
@@ -234,7 +234,7 @@ class HtmlElementCreator {
|
||||
// dive into sub tree to attach sub nodes
|
||||
// NOTES: we can have content (text) AND sub nodes at the same level
|
||||
// CONTENT (TEXT) takes preference over SUB NODE in order
|
||||
if (isObject(tree.sub) && tree.sub.length > 0) {
|
||||
if (isArray(tree.sub) && tree.sub.length > 0) {
|
||||
if (tree.content) {
|
||||
content.push(tree.content);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: HTML Helpers
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
Description: JavaScript Helpers
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
export {
|
||||
errorCatch, isFunction,
|
||||
executeFunctionByName, runFunction, runFunctionArgsArray,
|
||||
isObject, getObjectCount,
|
||||
isObject, isIterable, isArray, getObjectCount,
|
||||
keyInObject, objectKeyExists,
|
||||
getKeyByValue, valueInObject, objectValueExists,
|
||||
deepCopyFunction
|
||||
@@ -114,10 +114,33 @@ function runFunctionArgsArray(name, args)
|
||||
*/
|
||||
function isObject(val)
|
||||
{
|
||||
if (val === null) {
|
||||
return false;
|
||||
return val !== null && typeof val === 'object' && !Array.isArray(val);
|
||||
}
|
||||
return ((typeof val === 'function') || (typeof val === 'object'));
|
||||
|
||||
/**
|
||||
* Check if variable is array
|
||||
* @param {any} val possible array
|
||||
* @returns {Boolean} true/false if it an array
|
||||
*/
|
||||
function isArray(val)
|
||||
{
|
||||
return val !== null && Array.isArray(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any iterable type
|
||||
* @param {any} val possible iteratable object/array
|
||||
* @returns {Boolean} true/false if it is iterable
|
||||
*/
|
||||
function isIterable(val)
|
||||
{
|
||||
if (val == null) return false;
|
||||
|
||||
// Check if it's iterable, but excldue strings
|
||||
if (typeof val[Symbol.iterator] === 'function' && typeof val !== 'string') return true;
|
||||
|
||||
// Check if it's a plain object
|
||||
return typeof val === 'object' && val.constructor === Object;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: Login access and menu
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: Login access and menu
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: Resize and Move Javascript
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: String Helpers
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: HTML Helpers
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Description: Translation call
|
||||
Date: 2025//3/6
|
||||
Date: 2025/3/6
|
||||
Creator: Clemens Schwaighofer
|
||||
*/
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
// isFunction,
|
||||
// executeFunctionByName,
|
||||
isObject,
|
||||
isArray,
|
||||
isIterable,
|
||||
getObjectCount,
|
||||
keyInObject,
|
||||
objectKeyExists,
|
||||
@@ -30,15 +32,57 @@ describe("isObject", () => {
|
||||
let is_object = {
|
||||
"a": 1
|
||||
};
|
||||
let empty_array = [];
|
||||
let is_array = [1, 2, 3];
|
||||
let is_string = "";
|
||||
let is_number = 1;
|
||||
expect(isObject(empty_object)).toEqual(true);
|
||||
expect(isObject(is_object)).toEqual(true);
|
||||
expect(isObject(empty_array)).toEqual(false);
|
||||
expect(isObject(is_array)).toEqual(false);
|
||||
expect(isObject(is_string)).toEqual(false);
|
||||
expect(isObject(is_number)).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isArray", () => {
|
||||
it('should return bool if array', () => {
|
||||
let empty_object = {};
|
||||
let is_object = {
|
||||
"a": 1
|
||||
};
|
||||
let empty_array = [];
|
||||
let is_array = [1, 2, 3];
|
||||
let is_string = "";
|
||||
let is_number = 1;
|
||||
expect(isArray(empty_object)).toEqual(false);
|
||||
expect(isArray(is_object)).toEqual(false);
|
||||
expect(isArray(empty_array)).toEqual(true);
|
||||
expect(isArray(is_array)).toEqual(true);
|
||||
expect(isArray(is_string)).toEqual(false);
|
||||
expect(isArray(is_number)).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isIterable", () => {
|
||||
it('should return bool if iterable', () => {
|
||||
let empty_object = {};
|
||||
let is_object = {
|
||||
"a": 1
|
||||
};
|
||||
let empty_array = [];
|
||||
let is_array = [1, 2, 3];
|
||||
let is_string = "";
|
||||
let is_number = 1;
|
||||
expect(isIterable(empty_object)).toEqual(true);
|
||||
expect(isIterable(is_object)).toEqual(true);
|
||||
expect(isIterable(empty_array)).toEqual(true);
|
||||
expect(isIterable(is_array)).toEqual(true);
|
||||
expect(isIterable(is_string)).toEqual(false);
|
||||
expect(isIterable(is_number)).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getObjectCount", () => {
|
||||
it('should return count of objects', () => {
|
||||
let zero = {};
|
||||
|
||||
Reference in New Issue
Block a user