data-utils-grok
Version:
A comprehensive JavaScript utility library for data processing, organized by data types (arrays, objects, strings, forms, tables)
215 lines (183 loc) • 4.69 kB
JavaScript
// src/objectUtils.js
/**
* 对象类型处理工具函数
*/
/**
* 深拷贝对象
* @param {Object} obj - 要拷贝的对象
* @returns {Object} 深拷贝后的对象
*/
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 = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
cloned[key] = deepClone(obj[key]);
}
}
return cloned;
}
return obj;
}
/**
* 合并对象
* @param {...Object} objects - 要合并的对象
* @returns {Object} 合并后的对象
*/
function merge(...objects) {
return objects.reduce((result, obj) => {
if (obj && typeof obj === 'object') {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] && typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
result[key] = merge(result[key] || {}, obj[key]);
} else {
result[key] = obj[key];
}
}
}
}
return result;
}, {});
}
/**
* 获取对象的嵌套属性值
* @param {Object} obj - 对象
* @param {string} path - 属性路径,如 'user.address.city'
* @param {*} defaultValue - 默认值
* @returns {*} 属性值或默认值
*/
function get(obj, path, defaultValue = undefined) {
if (!obj || typeof obj !== 'object') {
return defaultValue;
}
const keys = path.split('.');
let result = obj;
for (const key of keys) {
if (result && typeof result === 'object' && key in result) {
result = result[key];
} else {
return defaultValue;
}
}
return result;
}
/**
* 设置对象的嵌套属性值
* @param {Object} obj - 对象
* @param {string} path - 属性路径
* @param {*} value - 要设置的值
* @returns {Object} 修改后的对象
*/
function set(obj, path, value) {
if (!obj || typeof obj !== 'object') {
throw new TypeError('Target must be an object');
}
const keys = path.split('.');
const lastKey = keys.pop();
let current = obj;
for (const key of keys) {
if (!(key in current) || typeof current[key] !== 'object') {
current[key] = {};
}
current = current[key];
}
current[lastKey] = value;
return obj;
}
/**
* 移除对象的指定属性
* @param {Object} obj - 对象
* @param {string|Array} keys - 要移除的属性或属性数组
* @returns {Object} 移除属性后的新对象
*/
function omit(obj, keys) {
if (!obj || typeof obj !== 'object') {
return {};
}
const keyArray = Array.isArray(keys) ? keys : [keys];
const result = {};
for (const key in obj) {
if (obj.hasOwnProperty(key) && !keyArray.includes(key)) {
result[key] = obj[key];
}
}
return result;
}
/**
* 只保留对象的指定属性
* @param {Object} obj - 对象
* @param {string|Array} keys - 要保留的属性或属性数组
* @returns {Object} 保留属性后的新对象
*/
function pick(obj, keys) {
if (!obj || typeof obj !== 'object') {
return {};
}
const keyArray = Array.isArray(keys) ? keys : [keys];
const result = {};
for (const key of keyArray) {
if (key in obj) {
result[key] = obj[key];
}
}
return result;
}
/**
* 检查对象是否为空
* @param {Object} obj - 对象
* @returns {boolean} 是否为空
*/
function isEmpty(obj) {
if (obj === null || obj === undefined) {
return true;
}
if (typeof obj !== 'object') {
return false;
}
if (Array.isArray(obj)) {
return obj.length === 0;
}
return Object.keys(obj).length === 0;
}
/**
* 获取对象的所有键路径
* @param {Object} obj - 对象
* @param {string} [prefix=''] - 前缀
* @returns {Array} 所有键路径数组
*/
function getKeys(obj, prefix = '') {
if (!obj || typeof obj !== 'object') {
return [];
}
const keys = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const currentPath = prefix ? `${prefix}.${key}` : key;
keys.push(currentPath);
if (obj[key] && typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
keys.push(...getKeys(obj[key], currentPath));
}
}
}
return keys;
}
module.exports = {
deepClone,
merge,
get,
set,
omit,
pick,
isEmpty,
getKeys,
};