Files
BarangaySystem/resources/js/utils/object.js
2026-06-06 18:43:00 +08:00

33 lines
797 B
JavaScript

/**
* Checks whether an object has only empty values.
*
* Empty values:
* - undefined
* - null
* - empty string
* - empty array
* - empty object
*
* @param {Object} obj
* @returns {boolean}
*
* @example
* isObjectEmpty({}) // true
* isObjectEmpty({ a: '' }) // true
* isObjectEmpty({ a: [] }) // true
* isObjectEmpty({ a: {} }) // true
* isObjectEmpty({ a: 1 }) // false
*/
export function isObjectEmpty(obj) {
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
return false
}
return Object.values(obj).every(value => {
if (value === undefined || value === null || value === '') return true
if (Array.isArray(value)) return value.length === 0
if (typeof value === 'object') return Object.keys(value).length === 0
return false
})
}