import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot } from '@angular/router'; /* tslint:disable */ /** * 帮助类 * * @export * @class Utils */ @Injectable({ providedIn: 'root' }) export class NaUtils { /** * 判断是否为空 * * @param {*} value * @returns {boolean} true: 为空 * @memberof NaUtils */ static isNullOrEmpty(value: any): boolean { // 判断字符串是否为空 if ( value == null || value === 'undefined' || typeof value === 'undefined' ) { return true; } const type = Object.prototype.toString .call(value) .toLowerCase() .slice(8, -1); switch (type) { case 'boolean': case 'number': return false; case 'string': return !value.replace(/(^\s*)|(\s*$)/g, ''); case 'array': return !value.length; case 'map': return !value.size; case 'object': if ( Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0 ) { return true; } Object.keys(value).map(t => { return false; }); return true; default: console.warn( `unknown type:${type}, value:${value}, please perfect the judgment logic` ); return false; // 其他对象均视作非空 } } static toBoolean(value: boolean | string): boolean { return value === '' || (value && value !== 'false'); } // public isObject(value): boolean { // return value !== null && typeof value === 'object'; // } static isUndefined(value) { return typeof value === 'undefined'; } static isEmpty(value) { return typeof value === 'undefined' || value === null; } static random(): number { return Math.random(); } /** * @name equals * * @description * Determines if two objects or two values are equivalent. * * Two objects or values are considered equivalent if at least one of the following is true: * * * Both objects or values pass `===` comparison. * * Both objects or values are of the same type and all of their properties are equal by * comparing them with `equals`. * * @param {*} o1 Object or value to compare. * @param {*} o2 Object or value to compare. * @returns {boolean} True if arguments are equal. */ static equals(o1: any, o2: any): boolean { if (o1 === o2) return true; if (o1 === null || o2 === null) return false; if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN let t1 = typeof o1, t2 = typeof o2, length: number, key: any, keySet: any; if (t1 == t2 && t1 == 'object') { if (Array.isArray(o1)) { if (!Array.isArray(o2)) return false; if ((length = o1.length) == o2.length) { for (key = 0; key < length; key++) { if (!this.equals(o1[key], o2[key])) return false; } return true; } } else { if (Array.isArray(o2)) { return false; } keySet = Object.create(null); for (key in o1) { if (!this.equals(o1[key], o2[key])) { return false; } keySet[key] = true; } for (key in o2) { if (!(key in keySet) && typeof o2[key] !== 'undefined') { return false; } } return true; } } return false; } static isDefined(value: any): boolean { return typeof value !== 'undefined' && value != null; } static isObject(item: any): boolean { return item && typeof item === 'object' && !Array.isArray(item); } static mergeDeep(json1: any, source: any): any { json1 = JSON.parse(JSON.stringify(json1)); source = JSON.parse(JSON.stringify(source)); const output = Object.assign({}, json1); if (this.isObject(json1) && this.isObject(source)) { Object.keys(source).forEach((key: any) => { if (this.isObject(source[key])) { if (!(key in json1)) { Object.assign(output, { [key]: source[key] }); } else { output[key] = this.mergeDeep(json1[key], source[key]); } } else { Object.assign(output, { [key]: source[key] }); } }); } return output; } /** * 根据快照获取URL地址 */ static getUrl(route: ActivatedRouteSnapshot): string { let next = this.getTruthRoute(route); const segments = []; while (next) { segments.push(next.url.join('/')); next = next.parent; } const url = '/' + segments .filter(i => i) .reverse() .join('/'); return url; } /** * JSON合并 * * 遇到相同元素级属性,以(source)为准,target为合并最新数据 * @param {*} target * @param {*} source */ static mergeJSON(target: any, source: any): void { for (var key in source) { var value = source[key]; // target不存在key,直接赋值 if (value === undefined) { target[key] = value; continue; } if (this.isJSON(target[key]) || this.isArray(target[key])) { // arguments.callee 递归调用,并且与函数名解耦 // arguments.callee(target[key], source[key]); this.mergeJSON(target[key], source[key]); } else { target[key] = value; } } } static isJSON(target: any) { return typeof target == 'object' && target.constructor == Object; } static isArray(o: any) { return Object.prototype.toString.call(o) == '[object Array]'; } private static getTruthRoute(route: ActivatedRouteSnapshot) { let next = route; while (next.firstChild) next = next.firstChild; return next; } }