typescript-util
Version:
JS/TS 的简单工具
247 lines • 7.07 kB
JavaScript
import { KeyValue } from '../model/KeyValue';
/**
* ObjectTool
* @author 冰凝
* @date 2022-09-19 09:42:58
**/
export class ObjectTool {
/**
* 判断入参是否是空
* @param {Object} o 待检查对象
* @return {boolean} 如果 o 是 null 或者 undefined 返回 true, 否则 返回 false
*/
static isNull(o) {
return o === null || o === undefined;
}
/**
* 对象非空
* @param o
* @return {boolean}
*/
static isNotNull(o) {
return o !== null && o !== undefined;
}
/**
* 对象是空对象: {}
* @param o 待检查对象
* @return {boolean} 是空?
*/
static isEmpty(o) {
return this.isNull(o) || Object.keys(o).length <= 0;
}
/**
* 对象非空
*/
static isNotEmpty(o) {
return !this.isEmpty(o);
}
/**
* 要求参数非空, 否则执行自定义回调
*/
static requireNotNullElse(o, callback = this.throwNPE) {
if (this.isNull(o)) {
this.requireNotNullElse(callback);
callback();
}
}
static requireNotNull(o) {
if (this.isNull(o)) {
this.throwNPE();
}
return o;
}
/**
* 要求非空, 否则返回默认值
* @param o 检查对象
* @param defaultValue 默认值, 非空
*/
static requireNotNullDefault(o, defaultValue) {
return this.isNotNull(o) ? o : this.requireNotNull(defaultValue);
}
/**
* 要求非空, 否则执行给定 {@link Supplier} 并返回
* @param o 检查对象
* @param supplier 默认值提供
*/
static requireNotNullSupplier(o, supplier) {
return this.isNotNull(o) ? o : supplier();
}
/**
* 对象"复制", 使用 Object.keys 复制第一层属性的引用
* @param source 源
* @param target 目标
* @param ignoreKeys 忽略的属性
*/
static copyByKeys(source, target, ...ignoreKeys) {
this.requireNotNull(source);
this.requireNotNull(target);
const ignore = this.requireNotNullDefault(ignoreKeys, []);
// 忽略下划线
// @ts-ignore
for (const key of Object.keys(source)) {
if (ignore.some(i => i === key)) {
continue;
}
// @ts-ignore
target[key] = source[key];
}
}
/**
* 对象键值反转, 不修改原对象
* @param o
*/
static keyValueReverse(o) {
const res = {};
if (this.isEmpty(o)) {
return res;
}
for (let key of Object.keys(o)) {
// @ts-ignore
res[o[key]] = key;
}
return res;
}
/**
* 根据指定key匹配谓词, 删除一部分属性;
* 修改源对象
* @param o 待处理对象
* @param pre 入参key ,返回 true 删除这个属性
*/
static deleteByKey(o, pre) {
if (this.isEmpty(o)) {
return;
}
this.requireNotNull(pre);
for (const key of Object.keys(o)) {
if (pre(key)) {
delete o[key];
}
}
}
/**
* 根据指定value匹配谓词, 删除一部分属性;
* 修改源对象
*/
static deleteByValue(o, pre) {
if (this.isEmpty(o)) {
return;
}
this.requireNotNull(pre);
for (let key of Object.keys(o)) {
if (pre(o[key])) {
delete o[key];
}
}
}
/**
* 根据指定 key | value 匹配谓词, 删除一部分属性;
* 修改源对象
*/
static deleteProperty(o, keyPre, valPre) {
if (this.isEmpty(o)) {
return;
}
this.requireNotNull(keyPre);
this.requireNotNull(valPre);
for (const key of Object.keys(o)) {
if (keyPre(key) || valPre(o[key])) {
delete o[key];
}
}
}
/**
* 过滤空属性, 不修改源对象
* 空定义: {@link #isNull}
* @param {object} o 允许为空
* @return 永远非空
*/
static filterNullProperty(o) {
if (this.isNull(o)) {
return {};
}
const res = this.copy(o);
// @ts-ignore
this.deleteByValue(res, arg => this.isNull(arg));
return res;
}
/**
* 对象属性和值遍历
* 真 - 把对象当 Map 用;
* 回调中传递的是原对象属性和值, 这意味着对其进行任何操作都反馈到源对象 (如果可能, 一般所有引用类型都是如此)
* @param o 待遍历对象, 允许为空 (不执行任何操作)
* @param bc 对象中每一组 key: value 的消费者; 第一个参数为属性名; 第二个参数为属性值
* @exception Error NPE 如果 bc 为空
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
*/
static forEach(o, bc) {
if (this.isNull(bc)) {
this.throwNPE();
}
if (this.isNull(o)) {
return;
}
const keys = Object.keys(o);
if (keys.length <= 0) {
return;
}
// @ts-ignore
keys.forEach(key => bc(key, o[key]));
}
/**
* 对象转 {@link KeyValue} 数组
* @param o
*/
static toArray(o) {
if (this.isEmpty(o)) {
return [];
}
// noinspection TypeScriptValidateTypes
// @ts-ignore
return Object.keys(o).map(key => new KeyValue(key, o[key]));
}
/**
* 对象值相等比较
* <li>如果 a b 引用相等 返回 true</li>
* <li>如果 a b 全 Null 返回 true</li>
* <li>如果 a b 含任意一个 Null (另外一个不是 Null) 返回 false</li>
* <li>最后 根据 a b 转为 JSON 后的字符串判断相等性</li>
*
* 注意: 因为 {@code JSON.stringify} 的问题; 不要传入二进制对象比较, 结果未知
* @param a 待判断值 允许为空
* @param b 待判断值 允许为空
* @return {boolean} a b 值相等
*/
static equals(a, b) {
if (a === b) {
return true;
}
let aIsNull = this.isNull(a);
let bIsNull = this.isNull(b);
// 都是 Null
if (aIsNull && aIsNull === bIsNull) {
return true;
}
// 排除了全 Null; 这里 应该是: true || false ; false || true
if (aIsNull || bIsNull) {
return false;
}
let aJson = JSON.stringify(a);
let bJson = JSON.stringify(b);
if (aJson.length !== bJson.length) {
return false;
}
return aJson == bJson;
}
/**
* 复制, 不要对二进制对象使用, 性能差, 结果未知
* @param o 源
* @return 新对象
*/
static copy(o) {
return JSON.parse(JSON.stringify(o));
}
static throwNPE() {
throw new Error('NPE');
}
}
//# sourceMappingURL=ObjectTool.js.map