arayts
Version:
让 TypeScript 开发如丝般顺滑。ArayTS 提供了一套高效、优雅的算法工具集,包含常用的数据结构与算法实现,帮助开发者轻松构建可靠的应用程序。
227 lines (226 loc) • 7.45 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectUtils = void 0;
var ObjectUtils = /** @class */ (function () {
function ObjectUtils() {
}
/**
* 深拷贝对象
*/
ObjectUtils.deepClone = function (obj) {
var _this = this;
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime());
}
if (obj instanceof Array) {
return obj.map(function (item) { return _this.deepClone(item); });
}
if (obj instanceof Object) {
var copy_1 = {};
Object.keys(obj).forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
copy_1[key] = _this.deepClone(obj[key]);
}
});
return copy_1;
}
return obj;
};
/**
* 合并对象
*/
ObjectUtils.merge = function (target) {
var sources = [];
for (var _i = 1; _i < arguments.length; _i++) {
sources[_i - 1] = arguments[_i];
}
return Object.assign.apply(Object, __spreadArray([target], sources, false));
};
/**
* 获取对象指定路径的值
*/
ObjectUtils.get = function (obj, path, defaultValue) {
var keys = path.split('.');
var result = obj;
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
if (result === undefined || result === null) {
return defaultValue;
}
result = result[key];
}
return result === undefined ? defaultValue : result;
};
/**
* 判断对象是否为空
*/
ObjectUtils.isEmpty = function (obj) {
return Object.keys(obj).length === 0;
};
/**
* 剔除对象中的指定属性
* @param obj 源对象
* @param props 要剔除的属性数组
* @param exclude 为true时剔除指定属性,为false时只保留指定属性
*/
ObjectUtils.omit = function (obj, props, exclude) {
if (exclude === void 0) { exclude = true; }
var result = {};
Object.keys(obj).forEach(function (key) {
var k = key;
if (exclude ? !props.includes(k) : props.includes(k)) {
result[k] = obj[k];
}
});
return result;
};
/**
* 比较两个对象是否相等
*/
ObjectUtils.isEqual = function (obj1, obj2) {
var _this = this;
if (obj1 === obj2)
return true;
if (typeof obj1 !== 'object' || typeof obj2 !== 'object' ||
obj1 === null || obj2 === null) {
return false;
}
var keys1 = Object.keys(obj1);
var keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length)
return false;
return keys1.every(function (key) {
return Object.prototype.hasOwnProperty.call(obj2, key) &&
_this.isEqual(obj1[key], obj2[key]);
});
};
/**
* 将对象扁平化,支持自定义分隔符
*/
ObjectUtils.flatten = function (obj, delimiter, prefix) {
var _this = this;
if (delimiter === void 0) { delimiter = '.'; }
if (prefix === void 0) { prefix = ''; }
return Object.keys(obj).reduce(function (acc, key) {
var pre = prefix.length ? prefix + delimiter : '';
if (typeof obj[key] === 'object' && obj[key] !== null) {
Object.assign(acc, _this.flatten(obj[key], delimiter, pre + key));
}
else {
acc[pre + key] = obj[key];
}
return acc;
}, {});
};
/**
* 将对象转换为查询字符串
*/
ObjectUtils.toQueryString = function (obj) {
return Object.entries(obj)
.filter(function (_a) {
var _ = _a[0], value = _a[1];
return value !== undefined && value !== null;
})
.map(function (_a) {
var key = _a[0], value = _a[1];
if (Array.isArray(value)) {
return value
.map(function (item) { return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(item)); })
.join('&');
}
return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
})
.join('&');
};
/**
* 重命名对象的属性
*/
ObjectUtils.renameKeys = function (obj, keysMap) {
return Object.keys(obj).reduce(function (acc, key) {
var newKey = keysMap[key] || key;
acc[newKey] = obj[key];
return acc;
}, {});
};
/**
* 根据条件过滤对象的属性
*/
ObjectUtils.filterProperties = function (obj, predicate) {
return Object.keys(obj).reduce(function (acc, key) {
if (predicate(obj[key], key)) {
acc[key] = obj[key];
}
return acc;
}, {});
};
/**
* 对象深度合并
*/
ObjectUtils.deepMerge = function (target) {
var _this = this;
var sources = [];
for (var _i = 1; _i < arguments.length; _i++) {
sources[_i - 1] = arguments[_i];
}
if (!sources.length)
return target;
var source = sources.shift();
if (source === undefined)
return target;
if (this.isObject(target) && this.isObject(source)) {
Object.keys(source).forEach(function (key) {
var _a, _b;
if (_this.isObject(source[key])) {
if (!target[key]) {
Object.assign(target, (_a = {}, _a[key] = {}, _a));
}
_this.deepMerge(target[key], source[key]);
}
else {
Object.assign(target, (_b = {}, _b[key] = source[key], _b));
}
});
}
return this.deepMerge.apply(this, __spreadArray([target], sources, false));
};
/**
* 判断是否为对象
*/
ObjectUtils.isObject = function (item) {
return item && typeof item === 'object' && !Array.isArray(item);
};
/**
* 将对象转换为数组
*/
ObjectUtils.toArray = function (obj) {
return Object.entries(obj);
};
/**
* 从数组创建对象
*/
ObjectUtils.fromArray = function (arr) {
return Object.fromEntries(arr);
};
/**
* 对象属性排序
*/
ObjectUtils.sort = function (obj, compareFn) {
var defaultCompare = function (a, b) { return a[0].localeCompare(b[0]); };
var sortedEntries = Object.entries(obj).sort(compareFn || defaultCompare);
return Object.fromEntries(sortedEntries);
};
return ObjectUtils;
}());
exports.ObjectUtils = ObjectUtils;