2016-03-13 05:19:24 +08:00
|
|
|
export class Utils {
|
|
|
|
|
2016-07-07 18:19:08 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
static clone<T>(object: T): T {
|
|
|
|
return JSON.parse(JSON.stringify(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
static equalsFilter(object: any, filter: any): boolean {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof filter !== 'object' || filter == null) {
|
|
|
|
return object === filter;
|
2017-07-15 18:47:11 +08:00
|
|
|
}
|
2018-05-29 02:03:12 +08:00
|
|
|
if (!object) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-08 18:43:42 +08:00
|
|
|
const keys = Object.keys(filter);
|
2017-06-11 04:32:56 +08:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
2017-07-08 18:43:42 +08:00
|
|
|
const key = keys[i];
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof filter[key] === 'object') {
|
|
|
|
if (Utils.equalsFilter(object[key], filter[key]) === false) {
|
2017-07-08 18:43:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (object[key] !== filter[key]) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return false;
|
2017-07-08 18:43:42 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 05:19:24 +08:00
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-07 18:19:08 +08:00
|
|
|
|
2016-03-26 23:25:48 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
static concatUrls(...args: Array<string>) {
|
2018-05-13 00:19:51 +08:00
|
|
|
let url = '';
|
2017-06-11 04:32:56 +08:00
|
|
|
for (let i = 0; i < args.length; i++) {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (args[i] === '' || typeof args[i] === 'undefined') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-26 23:25:48 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
const part = args[i].replace('\\', '/');
|
|
|
|
if (part === '/' || part === './') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-22 19:23:44 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
url += part + '/';
|
2016-05-13 00:24:26 +08:00
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
url = url.replace('//', '/');
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
if (url.trim() === '') {
|
|
|
|
url = './';
|
2017-07-20 02:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
return url.substring(0, url.length - 1);
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
public static updateKeys(targetObject: any, sourceObject: any) {
|
2017-06-11 04:32:56 +08:00
|
|
|
Object.keys(sourceObject).forEach((key) => {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof targetObject[key] === 'undefined') {
|
2017-06-11 04:32:56 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof targetObject[key] === 'object') {
|
2017-06-11 04:32:56 +08:00
|
|
|
Utils.updateKeys(targetObject[key], sourceObject[key]);
|
|
|
|
} else {
|
|
|
|
targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
public static setKeys(targetObject: any, sourceObject: any) {
|
2017-06-11 04:32:56 +08:00
|
|
|
Object.keys(sourceObject).forEach((key) => {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof targetObject[key] === 'object') {
|
2017-06-11 04:32:56 +08:00
|
|
|
Utils.setKeys(targetObject[key], sourceObject[key]);
|
|
|
|
} else {
|
|
|
|
targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
public static setKeysForced(targetObject: any, sourceObject: any) {
|
2017-06-11 04:32:56 +08:00
|
|
|
Object.keys(sourceObject).forEach((key) => {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof sourceObject[key] === 'object') {
|
|
|
|
if (typeof targetObject[key] === 'undefined') {
|
2017-06-11 04:32:56 +08:00
|
|
|
targetObject[key] = {};
|
2016-05-02 03:30:43 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
Utils.setKeysForced(targetObject[key], sourceObject[key]);
|
|
|
|
} else {
|
|
|
|
targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
public static enumToArray(EnumType: any): Array<{
|
2017-07-08 18:43:42 +08:00
|
|
|
key: number;
|
|
|
|
value: string;
|
|
|
|
}> {
|
2018-05-13 00:19:51 +08:00
|
|
|
const arr: Array<{ key: number; value: string; }> = [];
|
|
|
|
for (const enumMember in EnumType) {
|
2017-06-11 04:32:56 +08:00
|
|
|
if (!EnumType.hasOwnProperty(enumMember)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
const key = parseInt(enumMember, 10);
|
2017-06-11 04:32:56 +08:00
|
|
|
if (key >= 0) {
|
|
|
|
arr.push({key: key, value: EnumType[enumMember]});
|
|
|
|
}
|
2016-05-02 03:30:43 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
return arr;
|
|
|
|
}
|
2016-04-22 19:23:44 +08:00
|
|
|
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2017-07-20 02:47:09 +08:00
|
|
|
public static findClosest(number: number, arr: Array<number>) {
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
let curr = arr[0];
|
|
|
|
let diff = Math.abs(number - curr);
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
arr.forEach((value) => {
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
const newDiff = Math.abs(number - value);
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (newDiff < diff) {
|
|
|
|
diff = newDiff;
|
|
|
|
curr = value;
|
|
|
|
}
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
return curr;
|
|
|
|
}
|
2016-05-12 17:00:46 +08:00
|
|
|
|
2016-03-13 05:19:24 +08:00
|
|
|
}
|