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
168 lines
4.3 KiB
JavaScript
168 lines
4.3 KiB
JavaScript
import { describe, it, expect } from "vitest";
|
|
|
|
import {
|
|
// errorCatch,
|
|
// isFunction,
|
|
// executeFunctionByName,
|
|
isObject,
|
|
isArray,
|
|
isIterable,
|
|
getObjectCount,
|
|
keyInObject,
|
|
objectKeyExists,
|
|
getKeyByValue,
|
|
valueInObject,
|
|
objectValueExists,
|
|
deepCopyFunction
|
|
} from '../src/utils/JavaScriptHelpers.mjs';
|
|
|
|
|
|
// needs window class
|
|
// function test() {}
|
|
/* describe("isFunction", () => {
|
|
it('Should return brack if function', () => {
|
|
expect(isFunction("test")).toEqual(true);
|
|
expect(isFunction("testFalse")).toEqual(false);
|
|
});
|
|
}); */
|
|
|
|
describe("isObject", () => {
|
|
it('should return bool if object', () => {
|
|
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(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 = {};
|
|
let one = {
|
|
"a": 1
|
|
};
|
|
let notset = "";
|
|
expect(getObjectCount(zero)).toEqual(0);
|
|
expect(getObjectCount(one)).toEqual(1);
|
|
expect(getObjectCount(notset)).toEqual(-1);
|
|
});
|
|
});
|
|
|
|
describe("objectKeyExists", () => {
|
|
it('should return true if key exists in object', () => {
|
|
let zero = {};
|
|
let one = {
|
|
"a": 1,
|
|
"b": "foo"
|
|
};
|
|
let notset = "";
|
|
expect(keyInObject("", zero)).toEqual(false);
|
|
expect(objectKeyExists(zero, "")).toEqual(false);
|
|
expect(keyInObject("a", one)).toEqual(true);
|
|
expect(objectKeyExists(one, "a")).toEqual(true);
|
|
expect(keyInObject("c", one)).toEqual(false);
|
|
expect(objectKeyExists(one, "c")).toEqual(false);
|
|
expect(keyInObject("", notset)).toEqual(false);
|
|
expect(objectKeyExists(notset, "")).toEqual(false);
|
|
});
|
|
});
|
|
|
|
describe("getKeyByValue", () => {
|
|
it('should return the key for matching volume, first serve', () => {
|
|
let zero = {};
|
|
let one = {
|
|
"a": 1,
|
|
"b": "foo"
|
|
};
|
|
let notset = "";
|
|
expect(getKeyByValue(zero, "")).toEqual("");
|
|
expect(getKeyByValue(one, 1)).toEqual("a");
|
|
expect(getKeyByValue(one, "foo")).toEqual("b");
|
|
expect(getKeyByValue(one, "bar")).toEqual("");
|
|
expect(getKeyByValue(notset, "")).toEqual("");
|
|
});
|
|
});
|
|
|
|
describe("objectValueExists", () => {
|
|
it('should return true if key exists in object', () => {
|
|
let zero = {};
|
|
let one = {
|
|
"a": 1,
|
|
"b": "foo"
|
|
};
|
|
let notset = "";
|
|
expect(valueInObject(zero, "")).toEqual(false);
|
|
expect(objectValueExists(zero, "")).toEqual(false);
|
|
expect(valueInObject(one, 1)).toEqual(true);
|
|
expect(objectValueExists(one, 1)).toEqual(true);
|
|
expect(valueInObject(one, "foo")).toEqual(true);
|
|
expect(objectValueExists(one, "foo")).toEqual(true);
|
|
expect(valueInObject(one, "bar")).toEqual(false);
|
|
expect(objectValueExists(one, "bar")).toEqual(false);
|
|
expect(valueInObject(notset, "")).toEqual(false);
|
|
expect(objectValueExists(notset, "")).toEqual(false);
|
|
});
|
|
});
|
|
|
|
// deepCopyFunction
|
|
describe("deepCopyFunction", () => {
|
|
it('should deep copy an object', () => {
|
|
let one = {
|
|
"a": 1,
|
|
"b": "foo"
|
|
};
|
|
let new_object = deepCopyFunction(one);
|
|
expect(new_object).toStrictEqual(one);
|
|
});
|
|
});
|
|
|
|
// __END__
|