43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.isEmptyObject = exports.isEmptyJsonObject = void 0;
|
|
function isEmptyJsonObject(jsonString) {
|
|
try {
|
|
const obj = JSON.parse(jsonString);
|
|
return isEmptyObject(obj);
|
|
}
|
|
catch (error) {
|
|
// 如果解析失败,返回 false
|
|
return false;
|
|
}
|
|
}
|
|
exports.isEmptyJsonObject = isEmptyJsonObject;
|
|
function isEmptyObject(obj) {
|
|
// 处理 null 或 undefined
|
|
if (obj === null || obj === undefined) {
|
|
return true;
|
|
}
|
|
// 处理空字符串
|
|
if (typeof obj === 'string' && obj === '') {
|
|
return true;
|
|
}
|
|
// 如果是数组
|
|
if (Array.isArray(obj)) {
|
|
// 所有元素都为空才返回 true
|
|
return obj.length === 0 || obj.every(item => isEmptyObject(item));
|
|
}
|
|
// 如果是对象
|
|
if (typeof obj === 'object') {
|
|
const keys = Object.keys(obj);
|
|
// 空对象返回 true
|
|
if (keys.length === 0) {
|
|
return true;
|
|
}
|
|
// 递归检查所有值
|
|
return keys.every(key => isEmptyObject(obj[key]));
|
|
}
|
|
// 其他类型(数字、布尔值等)视为非空
|
|
return false;
|
|
}
|
|
exports.isEmptyObject = isEmptyObject;
|