2016-03-12 22:19:24 +01:00
|
|
|
export class Utils {
|
2019-07-27 22:56:12 +02:00
|
|
|
static GUID() {
|
|
|
|
const s4 = function () {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
return s4() + s4() + '-' + s4() + s4();
|
|
|
|
}
|
|
|
|
|
2018-12-02 12:22:05 +01:00
|
|
|
static chunkArrays<T>(arr: T[], chunkSize: number): T[][] {
|
|
|
|
const R = [];
|
|
|
|
for (let i = 0; i < arr.length; i += chunkSize) {
|
|
|
|
R.push(arr.slice(i, i + chunkSize));
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
2016-03-12 22:19:24 +01:00
|
|
|
|
2018-12-07 22:06:13 +01:00
|
|
|
static wait(time: number) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, time);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-07 12:19:08 +02:00
|
|
|
|
2018-12-05 17:29:33 +01:00
|
|
|
static removeNullOrEmptyObj(obj: any) {
|
|
|
|
if (typeof obj !== 'object' || obj == null) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
const keys = Object.keys(obj);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (obj[key] !== null && typeof obj[key] === 'object') {
|
|
|
|
if (Utils.removeNullOrEmptyObj(obj[key])) {
|
|
|
|
if (Object.keys(obj[key]).length === 0) {
|
|
|
|
delete obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (obj[key] === null) {
|
|
|
|
delete obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
static clone<T>(object: T): T {
|
|
|
|
return JSON.parse(JSON.stringify(object));
|
|
|
|
}
|
|
|
|
|
2018-11-28 23:49:33 +01:00
|
|
|
static zeroPrefix(value: string | number, length: number) {
|
2018-11-17 20:15:48 +01:00
|
|
|
const ret = '00000' + value;
|
|
|
|
return ret.substr(ret.length - length);
|
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
static equalsFilter(object: any, filter: any): boolean {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof filter !== 'object' || filter == null) {
|
|
|
|
return object === filter;
|
2017-07-15 12:47:11 +02:00
|
|
|
}
|
2018-05-28 14:03:12 -04:00
|
|
|
if (!object) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-08 12:43:42 +02:00
|
|
|
const keys = Object.keys(filter);
|
2017-06-10 22:32:56 +02:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
2017-07-08 12:43:42 +02:00
|
|
|
const key = keys[i];
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof filter[key] === 'object') {
|
|
|
|
if (Utils.equalsFilter(object[key], filter[key]) === false) {
|
2017-07-08 12:43:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (object[key] !== filter[key]) {
|
2017-06-10 22:32:56 +02:00
|
|
|
return false;
|
2017-07-08 12:43:42 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-03-12 22:19:24 +01:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-07 12:19:08 +02:00
|
|
|
|
2016-03-26 16:25:48 +01:00
|
|
|
|
2018-11-02 16:24:37 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param from
|
|
|
|
* @param to inclusive
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
static createRange(from: number, to: number): Array<number> {
|
|
|
|
const arr = new Array(to - from + 1);
|
|
|
|
let c = to - from + 1;
|
|
|
|
while (c--) {
|
|
|
|
arr[c] = to--;
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:47:09 +02:00
|
|
|
static concatUrls(...args: Array<string>) {
|
2018-05-12 12:19:51 -04:00
|
|
|
let url = '';
|
2017-06-10 22:32:56 +02:00
|
|
|
for (let i = 0; i < args.length; i++) {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (args[i] === '' || typeof args[i] === 'undefined') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-26 16:25:48 +01:00
|
|
|
|
2019-01-19 13:00:38 +01:00
|
|
|
const part = args[i].replace(new RegExp('\\\\', 'g'), '/');
|
2018-05-12 12:19:51 -04:00
|
|
|
if (part === '/' || part === './') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-22 13:23:44 +02:00
|
|
|
|
2018-05-12 12:19:51 -04:00
|
|
|
url += part + '/';
|
2016-05-12 18:24:26 +02:00
|
|
|
}
|
2019-01-19 13:00:38 +01:00
|
|
|
url = url.replace(new RegExp('/+', 'g'), '/');
|
2017-06-10 22:32:56 +02:00
|
|
|
|
2018-05-12 12:19:51 -04:00
|
|
|
if (url.trim() === '') {
|
|
|
|
url = './';
|
2017-07-19 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
return url.substring(0, url.length - 1);
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:47:09 +02:00
|
|
|
public static updateKeys(targetObject: any, sourceObject: any) {
|
2017-06-10 22:32:56 +02:00
|
|
|
Object.keys(sourceObject).forEach((key) => {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof targetObject[key] === 'undefined') {
|
2017-06-10 22:32:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof targetObject[key] === 'object') {
|
2017-06-10 22:32:56 +02:00
|
|
|
Utils.updateKeys(targetObject[key], sourceObject[key]);
|
|
|
|
} else {
|
|
|
|
targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:47:09 +02:00
|
|
|
public static setKeys(targetObject: any, sourceObject: any) {
|
2017-06-10 22:32:56 +02:00
|
|
|
Object.keys(sourceObject).forEach((key) => {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof targetObject[key] === 'object') {
|
2017-06-10 22:32:56 +02:00
|
|
|
Utils.setKeys(targetObject[key], sourceObject[key]);
|
|
|
|
} else {
|
|
|
|
targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:47:09 +02:00
|
|
|
public static setKeysForced(targetObject: any, sourceObject: any) {
|
2017-06-10 22:32:56 +02:00
|
|
|
Object.keys(sourceObject).forEach((key) => {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof sourceObject[key] === 'object') {
|
|
|
|
if (typeof targetObject[key] === 'undefined') {
|
2017-06-10 22:32:56 +02:00
|
|
|
targetObject[key] = {};
|
2016-05-01 21:30:43 +02:00
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
Utils.setKeysForced(targetObject[key], sourceObject[key]);
|
|
|
|
} else {
|
|
|
|
targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:47:09 +02:00
|
|
|
public static enumToArray(EnumType: any): Array<{
|
2017-07-08 12:43:42 +02:00
|
|
|
key: number;
|
|
|
|
value: string;
|
|
|
|
}> {
|
2018-05-12 12:19:51 -04:00
|
|
|
const arr: Array<{ key: number; value: string; }> = [];
|
|
|
|
for (const enumMember in EnumType) {
|
2017-06-10 22:32:56 +02:00
|
|
|
if (!EnumType.hasOwnProperty(enumMember)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-12 12:19:51 -04:00
|
|
|
const key = parseInt(enumMember, 10);
|
2017-06-10 22:32:56 +02:00
|
|
|
if (key >= 0) {
|
|
|
|
arr.push({key: key, value: EnumType[enumMember]});
|
|
|
|
}
|
2016-05-01 21:30:43 +02:00
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
return arr;
|
|
|
|
}
|
2016-04-22 13:23:44 +02:00
|
|
|
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2017-07-19 20:47:09 +02:00
|
|
|
public static findClosest(number: number, arr: Array<number>) {
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
let curr = arr[0];
|
|
|
|
let diff = Math.abs(number - curr);
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
arr.forEach((value) => {
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2018-05-12 12:19:51 -04:00
|
|
|
const newDiff = Math.abs(number - value);
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
if (newDiff < diff) {
|
|
|
|
diff = newDiff;
|
|
|
|
curr = value;
|
|
|
|
}
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
});
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
return curr;
|
|
|
|
}
|
2016-05-12 11:00:46 +02:00
|
|
|
|
2019-02-02 22:22:51 -05:00
|
|
|
public static isUInt32(value: number, max: number = 4294967295) {
|
|
|
|
return !isNaN(value) && value >= 0 && value <= max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isInt32(value: number) {
|
|
|
|
return !isNaN(value) && value >= -2147483648 && value <= 2147483647;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isFloat32(value: number) {
|
|
|
|
const E = Math.pow(10, 38);
|
|
|
|
const nE = Math.pow(10, -38);
|
|
|
|
return !isNaN(value) && ((value >= -3.402823466 * E && value <= -1.175494351 * nE) ||
|
|
|
|
(value <= 3.402823466 * E && value >= 1.175494351 * nE));
|
|
|
|
}
|
|
|
|
|
2016-03-12 22:19:24 +01:00
|
|
|
}
|