@bernotieno/mini-framework
Version:
A lightweight JavaScript framework built from scratch with zero dependencies
60 lines (50 loc) • 1.27 kB
JavaScript
/**
* Object Utilities
* Functions for working with objects and data structures
*/
/**
* Deep clone an object
*/
export function deepClone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime());
}
if (obj instanceof Array) {
return obj.map(item => deepClone(item));
}
if (typeof obj === 'object') {
const cloned = {};
Object.keys(obj).forEach(key => {
cloned[key] = deepClone(obj[key]);
});
return cloned;
}
return obj;
}
/**
* Deep merge two objects
*/
export function deepMerge(target, source) {
const result = { ...target };
Object.keys(source).forEach(key => {
if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
result[key] = deepMerge(result[key] || {}, source[key]);
} else {
result[key] = source[key];
}
});
return result;
}
/**
* Check if a value is empty
*/
export function isEmpty(value) {
if (value === null || value === undefined) return true;
if (typeof value === 'string') return value.trim() === '';
if (Array.isArray(value)) return value.length === 0;
if (typeof value === 'object') return Object.keys(value).length === 0;
return false;
}