@extralam/microui
Version:
A lightweight, modern JavaScript utility library that makes DOM manipulation and event handling simple and efficient.
104 lines (94 loc) • 2.59 kB
JavaScript
/**
* Utility Functions
* Provides helper functions for common tasks
*/
/**
* Debounce function
* @param {Function} func - Function to debounce
* @param {number} wait - Wait time in milliseconds
* @returns {Function} - Debounced function
*/
export function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func.apply(this, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* Throttle function
* @param {Function} func - Function to throttle
* @param {number} limit - Time limit in milliseconds
* @returns {Function} - Throttled function
*/
export function throttle(func, limit) {
let inThrottle;
return function executedFunction(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
/**
* Merge objects
* @param {Object} target - Target object
* @param {...Object} sources - Source objects
* @returns {Object} - Merged object
*/
export function extend(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
extend(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return extend(target, ...sources);
}
/**
* Check if value is an object
* @param {any} item - Value to check
* @returns {boolean} - True if value is an object
*/
function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
/**
* Generate unique ID
* @param {string} prefix - ID prefix
* @returns {string} - Unique ID
*/
export function uniqueId(prefix = 'id') {
return `${prefix}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Check if element is visible
* @param {Element} element - Element to check
* @returns {boolean} - True if element is visible
*/
export function isVisible(element) {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
}
/**
* Get element offset
* @param {Element} element - Element to get offset for
* @returns {Object} - Object with top and left properties
*/
export function offset(element) {
const rect = element.getBoundingClientRect();
return {
top: rect.top + window.pageYOffset,
left: rect.left + window.pageXOffset
};
}