
export class ObjectUtil {
    /**
     * 判断指定的值是否为对象
     * @param value 值
     */
    static isObject(value: any) {
        return Object.prototype.toString.call(value) === '[object Object]';
    }

    /**
     * 深拷贝
     * @param target 目标
     */
    static deepCopy(target: any) {
        if (!target || typeof target !== 'object') {
            return target;
        }

        let result: any = null;

        if (target instanceof Date) {
            result = new Date();
            result.setTime(target.getTime());
            return result;
        }

        if (target instanceof Array) {
            result = [];
            for (let i = 0, length = target.length; i < length; ++i) {
                result[i] = this.deepCopy(target[i]);
            }
            return result;
        }

        if (target instanceof Object) {
            result = {};
            for (const key in target) {
                if (target.hasOwnProperty(key)) {
                    result[key] = this.deepCopy(target[key]);
                }
            }
            return result;
        }
    }

    /**
     * 拷贝对象
     * @param target 目标
     */
    static copy(target: object) {
        return JSON.parse(JSON.stringify(target));
    }

    static mapToObject(data: any) {
        let obj = {}

        if (data instanceof Map) {
            for (let [key, value] of data) {
                if (value instanceof Map) {
                    let temp = {}
                    for (let [k, v] of value) {
                        temp[k] = v;
                    }
                    obj[key] = temp;
                } else if (value instanceof Array) {
                    obj[key] = value;
                } else if (typeof value === "object") {
                    let newObj = this.mapToObject(value)
                    obj[key] = newObj
                } else {
                    obj[key] = value
                }
            }
        } else {
            for (let key in data) {
                if (data[key] instanceof Map) {
                    let temp = {}
                    for (let [k, v] of data[key]) {
                        temp[k] = v;
                    }
                    obj[key] = temp
                } else if (typeof data[key] === "number" || typeof data[key] === "string" || data[key] instanceof Array || typeof data[key] === "boolean") {
                    obj[key] = data[key];
                } else if (typeof data[key] === "object") {
                    obj = this.mapToObject(data[key])
                }
                else if (typeof data === "object") {
                    obj = data
                    break
                }
                else {
                    obj[key] = data[key]
                }
            }
        }



        return obj
    }

 
}
