reblendjs
Version:
This is build using react way of handling dom but with web components
172 lines (171 loc) • 5.26 kB
JavaScript
import NullException from '../exceptions/NullException';
export function objectEquals(obj1, obj2) {
// Check if both object are strictly equal
if (obj1 === obj2) {
return true;
}
// Check if either object is null or not
if (typeof obj1 !== 'object' || obj1 == null || typeof obj2 !== 'object' || obj2 == null) {
return false;
}
// Get the keys of both objects
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
// Check if the number of keys is the same
if (keys1.length !== keys2.length) {
return false;
}
// Iterate through the keys and recursively check for equality
for (const key of keys1) {
if (!keys2.includes(key) || !objectEquals(obj1[key], obj2[key])) {
return false;
}
}
return true;
}
export function getDefinedValuesFrom(object) {
const definedValues = {};
for (const key in object) {
const value = object[key];
if (value != null && value != undefined) {
definedValues[key] = value;
}
}
return definedValues;
}
export function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export function sumField(objArr, ...field) {
if (!objArr || (Array.isArray(objArr) && objArr.length <= 0)) {
return 0;
}
let sum = 0;
for (const obj of objArr) {
let theValue = getObjectField(obj, field);
if (theValue) {
if (typeof theValue === 'string') {
theValue = parseInt(theValue);
}
if (typeof theValue === 'number') {
sum += theValue;
}
}
}
return sum;
}
export function getObjectField(obj, fields) {
const f = [...fields];
if (f.length <= 0 || !obj) {
return obj;
}
const leftMostFieldName = f.shift();
if (!leftMostFieldName) {
return obj;
}
return getObjectField(obj[leftMostFieldName], f);
}
export const shallowRemoveDuplicates = (arr) => {
const unique = new Set();
const filtered = arr?.filter((item) => {
if (item && !unique.has(item)) {
unique.add(item);
return true;
}
return false;
});
return filtered;
};
export const snakeCase = (camelCase) => {
return camelCase.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`).substring(1);
};
export const appendChildren = (parent, ...children) => {
if (!parent) {
throw new NullException();
}
for (const child of children) {
parent.appendChild(child);
}
return parent;
};
export const removeLastChild = (parent) => {
if (!parent) {
throw new NullException();
}
const removedChild = parent.removeChild(parent.lastChild);
return removedChild;
};
export const cssString = (styleObject) => {
let styleString = '';
for (const [key, value] of Object.entries(styleObject)) {
styleString += `${key}: ${value === undefined ? 'initial' : value}; \n`;
}
return styleString.trim();
};
export const cssObjectFromString = (styleString) => {
const regex = /([a-zA-Z-]+)\s*:\s*([^;]+)/g;
const cssObject = {};
let match;
while ((match = regex.exec(styleString)) !== null) {
const styleName = match[1].trim();
const value = match[2].trim();
cssObject[styleName] = value;
}
return cssObject;
};
export const spreadTo = (parentObj, objToSpread) => {
if (!objToSpread || !parentObj) {
return parentObj;
}
const keys = Object.keys(objToSpread);
const values = Object.values(objToSpread);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = values[i];
parentObj[key] = value;
}
return parentObj;
};
export function registerElement(name, element) {
if (!element) {
throw new Error('Element to register is null');
}
const tagName = snakeCase(name);
if (!customElements.get(tagName)) {
try {
customElements.define(tagName, element);
}
catch (error) {
console.warn(error.message);
}
}
return element;
}
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export const rand = (min = 1234, max = 9876) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
export const isCallable = (obj) => {
if (typeof obj !== 'function') {
return false;
}
try {
// Check if obj is a class constructor by inspecting its string representation
// Classes typically have a string representation starting with "class"
const str = Function.prototype.toString.call(obj);
if (str.startsWith('class')) {
return false;
}
}
catch (e) {
// If any error occurs during string conversion, assume it's not callable
return false;
}
return true;
};
export const REBLEND_COMPONENT = 'reblendcomponent';
export const REBLEND_WRAPPER_FOR_REACT_COMPONENT = 'reblendwrapperforreactcomponent';
export const REBLEND_CHILDREN_WRAPPER_FOR_REACT_COMPONENT = 'reblendchildrenwrapperforreactcomponent';
export const CUSTOM_TAGNAME = 'customTagName';