light-h5-core-lib 2.8.0 → 2.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LightCore.ts +49 -0
- package/common/base/BaseSingleton.ts +28 -0
- package/common/base/IClear.ts +5 -0
- package/common/base/IDestroy.ts +3 -0
- package/common/md5/{MD5.js → MD5.ts} +376 -346
- package/common/util/BeizerUtil.ts +87 -0
- package/common/util/{BitUtil.js → BitUtil.ts} +37 -43
- package/common/util/BooleanUtil.ts +15 -0
- package/common/util/Crc32Util.ts +30 -0
- package/common/util/DateUtil.ts +120 -0
- package/common/util/FloatDigitUtil.ts +55 -0
- package/common/util/NumberUtil.ts +15 -0
- package/common/util/PromiseUtil.ts +25 -0
- package/common/util/RandomNumUtil.ts +81 -0
- package/common/util/SelfDecrypt.ts +31 -0
- package/common/util/StringUtil.ts +83 -0
- package/common/util/UniqueIDUtil.ts +14 -0
- package/package.json +1 -1
- package/pool/PoolObj.ts +69 -0
- package/type/index.d.ts +81 -3
- package/LightCore.js +0 -44
- package/common/base/BaseSingleton.js +0 -23
- package/common/base/IClear.js +0 -2
- package/common/base/IDestroy.js +0 -2
- package/common/util/BeizerUtil.js +0 -83
- package/common/util/Crc32Util.js +0 -35
- package/common/util/CryptUtil.js +0 -43
- package/common/util/DateUtil.js +0 -89
- package/common/util/FloatDigitUtil.js +0 -22
- package/common/util/PromiseUtil.js +0 -74
- package/common/util/RandomNumUtil.js +0 -54
- package/common/util/StringUtil.js +0 -81
- package/common/util/UniqueIDUtil.js +0 -21
- package/pool/PoolObj.js +0 -79
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 贝塞尔曲线运动
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class BeizerUtil {
|
|
6
|
+
//#region 获得当前坐标
|
|
7
|
+
/**
|
|
8
|
+
* 获得当前坐标
|
|
9
|
+
* @param timer 时间[0,1]
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
public static getPosition(
|
|
13
|
+
points: { x: number; y: number }[],
|
|
14
|
+
timer: number
|
|
15
|
+
): { x: number; y: number } {
|
|
16
|
+
let x: number = 0,
|
|
17
|
+
y: number = 0;
|
|
18
|
+
let bezier: number;
|
|
19
|
+
for (let i: number = 0, n: number = points.length; i < n; i++) {
|
|
20
|
+
bezier =
|
|
21
|
+
Math.min(Math.pow(1 - timer, n - i - 1) * Math.pow(timer, i)) *
|
|
22
|
+
BeizerUtil.calculationC(i, n);
|
|
23
|
+
x += points[i].x * bezier;
|
|
24
|
+
y += points[i].y * bezier;
|
|
25
|
+
}
|
|
26
|
+
return { x, y };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private static calculationC: (i: number, n: number) => number = (i, n): number => {
|
|
30
|
+
return (
|
|
31
|
+
BeizerUtil.factorial(n - 1) /
|
|
32
|
+
(BeizerUtil.factorial(i) * BeizerUtil.factorial(n - 1 - i))
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
private static factorial: (num: number) => number = (num): number => {
|
|
37
|
+
if (num < 0) {
|
|
38
|
+
return 1;
|
|
39
|
+
} else if (num === 0 || num === 1) {
|
|
40
|
+
return 1;
|
|
41
|
+
} else {
|
|
42
|
+
return num * BeizerUtil.factorial(num - 1);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
|
|
47
|
+
//region 获得路线的长度
|
|
48
|
+
private static distance(p1: { x: number; y: number }, p2: { x: number; y: number }): number {
|
|
49
|
+
const dx: number = p2.x - p1.x;
|
|
50
|
+
const dy: number = p2.y - p1.y;
|
|
51
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 获得整个路线的长度
|
|
55
|
+
* @param precision 精确度(微积分方案)
|
|
56
|
+
*/
|
|
57
|
+
public static getLength(points: { x: number; y: number }[], precision: number = 100): number {
|
|
58
|
+
let length: number = 0;
|
|
59
|
+
let point1: { x: number; y: number };
|
|
60
|
+
let point2: { x: number; y: number };
|
|
61
|
+
for (let t: number = 0; t < 1; t += 1 / precision) {
|
|
62
|
+
point1 = BeizerUtil.getBezierPoint(points, t);
|
|
63
|
+
point2 = BeizerUtil.getBezierPoint(points, t + 1 / precision);
|
|
64
|
+
length += BeizerUtil.distance(point1, point2);
|
|
65
|
+
}
|
|
66
|
+
return length;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private static getBezierPoint: (
|
|
70
|
+
points: { x: number; y: number }[],
|
|
71
|
+
t: number
|
|
72
|
+
) => { x: number; y: number } = (points, t) => {
|
|
73
|
+
if (points.length === 1) {
|
|
74
|
+
return points[0];
|
|
75
|
+
}
|
|
76
|
+
const nextPoints: { x: number; y: number }[] = [];
|
|
77
|
+
let x: number;
|
|
78
|
+
let y: number;
|
|
79
|
+
for (let i: number = 0; i < points.length - 1; i++) {
|
|
80
|
+
x = (1 - t) * points[i].x + t * points[i + 1].x;
|
|
81
|
+
y = (1 - t) * points[i].y + t * points[i + 1].y;
|
|
82
|
+
nextPoints.push({ x, y });
|
|
83
|
+
}
|
|
84
|
+
return BeizerUtil.getBezierPoint(nextPoints, t);
|
|
85
|
+
};
|
|
86
|
+
//#endregion
|
|
87
|
+
}
|
|
@@ -1,43 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
return count;
|
|
40
|
-
};
|
|
41
|
-
return BitUtil;
|
|
42
|
-
}());
|
|
43
|
-
exports.BitUtil = BitUtil;
|
|
1
|
+
/**
|
|
2
|
+
* 位操作工具(从右向左←)
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class BitUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 读取uint类型某位的值, bit从1开始
|
|
8
|
+
*/
|
|
9
|
+
public static getBit(value: number, bit: number): number {
|
|
10
|
+
return (value & (1 << (bit - 1))) >> (bit - 1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 设置value 第bit(从1开始)位的值为v(0|1)
|
|
15
|
+
* 谨记bit<32
|
|
16
|
+
*/
|
|
17
|
+
public static setBit(value: number, bit: number, v: 0 | 1): number {
|
|
18
|
+
if (bit > 32) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
if (v == 1) {
|
|
22
|
+
return value | (v << (bit - 1));
|
|
23
|
+
} else {
|
|
24
|
+
return (value &= ~0 ^ (1 << (bit - 1)));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**获取1的个数 */
|
|
29
|
+
public static get1num(value: number): number {
|
|
30
|
+
let count: number = 0;
|
|
31
|
+
while (value) {
|
|
32
|
+
value = value & (value - 1);
|
|
33
|
+
count++;
|
|
34
|
+
}
|
|
35
|
+
return count;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bool类型处理
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class BooleanUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 获得Proto中的Bool值
|
|
8
|
+
*/
|
|
9
|
+
public static getProtoBool(data: any, key: string, defaultValue: boolean = false): boolean {
|
|
10
|
+
if (key in data) {
|
|
11
|
+
return data[key];
|
|
12
|
+
}
|
|
13
|
+
return defaultValue;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crc32验证工具
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export default class Crc32Util{
|
|
6
|
+
private static table: string = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
|
|
7
|
+
/**
|
|
8
|
+
* Crc32 number
|
|
9
|
+
*/
|
|
10
|
+
public static getCrc32Number(str: string,crc: number = 0): number{
|
|
11
|
+
let n: number = 0;
|
|
12
|
+
let x;
|
|
13
|
+
crc = crc ^ (-1);
|
|
14
|
+
for( let i: number = 0, iTop: number = str.length; i < iTop; i++ ) {
|
|
15
|
+
n = ( crc ^ str.charCodeAt( i ) ) & 0xFF;
|
|
16
|
+
x = "0x" + Crc32Util.table.substr( n * 9, 8 );
|
|
17
|
+
crc = ( crc >>> 8 ) ^ x;
|
|
18
|
+
}
|
|
19
|
+
return crc ^ (-1);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Crc32 string
|
|
23
|
+
*/
|
|
24
|
+
public static getCrc32String(str: string,crc: number = 0): string{
|
|
25
|
+
return Crc32Util.getCrc32Number(str,crc).toString(16);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日期(时间)工具
|
|
3
|
+
* @author aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class DateUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 获取指定时间的时间戳 (单位:ms)
|
|
8
|
+
* @param date 如: "2020-3-14 11:30:30"
|
|
9
|
+
*/
|
|
10
|
+
public static getTargetTimeStamp(date: string): number {
|
|
11
|
+
return new Date(date).getTime();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 获取指定时间的时间秒 (单位:s)
|
|
16
|
+
* @param date 如: "2020-3-14 11:30:30"
|
|
17
|
+
*/
|
|
18
|
+
public static getTargetSeconds(date: string): number {
|
|
19
|
+
return Math.floor(DateUtil.getTargetTimeStamp(date) / 1000);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 格式化时间(时,分,秒)
|
|
24
|
+
* @param s 秒
|
|
25
|
+
* @param separator 分隔符(default: ":")
|
|
26
|
+
* @param isSingle0 是否补0(default:true)
|
|
27
|
+
* @param isDiscardH 小时数为0时,是否舍弃显示变成分,秒(default:false)
|
|
28
|
+
*/
|
|
29
|
+
public static format_HMS(
|
|
30
|
+
s: number,
|
|
31
|
+
separator: string = ":",
|
|
32
|
+
isSingle0: boolean = true,
|
|
33
|
+
isDiscardH: boolean = false
|
|
34
|
+
): string {
|
|
35
|
+
const h: number = Math.floor(s / 3600);
|
|
36
|
+
if (isDiscardH && h == 0) {
|
|
37
|
+
return DateUtil.format_MS(s, separator, isSingle0);
|
|
38
|
+
}
|
|
39
|
+
const m: number = Math.floor((s - h * 3600) / 60);
|
|
40
|
+
s = s - h * 3600 - m * 60;
|
|
41
|
+
let result: string = isSingle0 ? (h < 10 ? `0${h}` : h.toString()) : h.toString();
|
|
42
|
+
result += separator;
|
|
43
|
+
result = result + (isSingle0 ? (m < 10 ? `0${m}` : m.toString()) : m.toString());
|
|
44
|
+
result += separator;
|
|
45
|
+
result = result + (isSingle0 ? (s < 10 ? `0${s}` : s.toString()) : s.toString());
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 格式化时间(分,秒)
|
|
51
|
+
* @param s 秒
|
|
52
|
+
* @param separator 分隔符(default: ":")
|
|
53
|
+
* @param isSingle0 是否补0(default:true)
|
|
54
|
+
*/
|
|
55
|
+
public static format_MS(s: number, separator: string = ":", isSingle0: boolean = true): string {
|
|
56
|
+
const m: number = Math.floor(s / 60);
|
|
57
|
+
s = s - m * 60;
|
|
58
|
+
let result: string = isSingle0 ? (m < 10 ? `0${m}` : m.toString()) : m.toString();
|
|
59
|
+
result += separator;
|
|
60
|
+
result = result + (isSingle0 ? (s < 10 ? `0${s}` : s.toString()) : s.toString());
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 获取下一天的时间(凌晨)秒 存在1ms误差可以忽略不计
|
|
66
|
+
* @param nowSecond 现在的时间秒 ( 服务器的时间(一般))
|
|
67
|
+
* @return 下一天的时间Second
|
|
68
|
+
* */
|
|
69
|
+
public static getNextDayTimeStamp(nowSecond: number): number {
|
|
70
|
+
return DateUtil.getTodayAppointTimeStamp(nowSecond, 23, 59, 59, 999);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 获取今日指定的时间秒, 例如今日20:00
|
|
75
|
+
* @param nowSecond 现在的时间秒 ( 服务器的时间(一般))
|
|
76
|
+
* @param hour 指定的小时数 0~23
|
|
77
|
+
* @param minute 指定的分钟数 0~59
|
|
78
|
+
* @param second 指定的秒数 0~59
|
|
79
|
+
* @param millisecond 指定的毫秒数 0~999
|
|
80
|
+
*/
|
|
81
|
+
public static getTodayAppointTimeStamp(
|
|
82
|
+
nowSecond: number,
|
|
83
|
+
hour: number,
|
|
84
|
+
minute: number = 0,
|
|
85
|
+
second: number = 0,
|
|
86
|
+
millisecond: number = 0
|
|
87
|
+
): number {
|
|
88
|
+
return Math.ceil(
|
|
89
|
+
new Date(
|
|
90
|
+
new Date(nowSecond * 1000).setHours(hour, minute, second, millisecond)
|
|
91
|
+
).getTime() / 1000
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 获得剩余的过期时间秒(S)用于倒计时
|
|
97
|
+
* @param targetExpireSecond 过期的时间戳(s)
|
|
98
|
+
* @param curSecond 当前时间戳(s)
|
|
99
|
+
* @param offsetSecond 误差值(s) (targetExpireSecond + offsetSecond)用于延长/缩短一些过期时间
|
|
100
|
+
*/
|
|
101
|
+
public static getRemainingSecond(targetExpireSecond: number, curSecond: number = null, offsetSecond: number = 1): number {
|
|
102
|
+
if (curSecond == null) {
|
|
103
|
+
curSecond = Math.floor(Date.now() / 1000);
|
|
104
|
+
}
|
|
105
|
+
return targetExpireSecond + offsetSecond - curSecond;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 获得过期的时间戳(S)
|
|
110
|
+
* @param durationSecond 过期的时间倒计时(s)
|
|
111
|
+
* @param curSecond 当前的时间(s)
|
|
112
|
+
* @return 过期的时间戳(s)
|
|
113
|
+
*/
|
|
114
|
+
public static getExpireSecond(durationSecond: number, curSecond: number = null): number {
|
|
115
|
+
if (curSecond == null) {
|
|
116
|
+
curSecond = Math.floor(Date.now() / 1000);
|
|
117
|
+
}
|
|
118
|
+
return curSecond + durationSecond;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Float精度
|
|
3
|
+
* @author aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class FloatDigitUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 确定小数的精度<b style="color:red">2个小数相加会有精度错误</b>
|
|
8
|
+
* @param fNum 小数
|
|
9
|
+
* @param digit 精度
|
|
10
|
+
*/
|
|
11
|
+
public static formatFloat(fNum: number, digit: number): number {
|
|
12
|
+
let m: number = Math.pow(10, digit);
|
|
13
|
+
return parseInt((fNum * m).toString(), 10) / m;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#region 小数格式化
|
|
17
|
+
/**
|
|
18
|
+
* 小数的格式化
|
|
19
|
+
*/
|
|
20
|
+
public static formatDecimalNumber(num: number, factor: number, isRound: boolean = true): number {
|
|
21
|
+
if (factor <= 0) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
let factorX: number = Math.pow(10, factor);
|
|
25
|
+
let value: number;
|
|
26
|
+
if (isRound) {
|
|
27
|
+
value = Math.round(num * factorX) / factorX;
|
|
28
|
+
} else {
|
|
29
|
+
value = Math.trunc(num * factorX) / factorX;
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 小数的格式化
|
|
36
|
+
*/
|
|
37
|
+
public static formatDecimalString(num: number, factor: number, isRound: boolean = true, isSupplementZero: boolean = true): string {
|
|
38
|
+
if (factor <= 0) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
let factorX: number = Math.pow(10, factor);
|
|
42
|
+
let value: number;
|
|
43
|
+
if (isRound) {
|
|
44
|
+
value = Math.round(num * factorX) / factorX;
|
|
45
|
+
} else {
|
|
46
|
+
value = Math.trunc(num * factorX) / factorX;
|
|
47
|
+
}
|
|
48
|
+
if (isSupplementZero) {
|
|
49
|
+
return value.toFixed(factor);
|
|
50
|
+
}
|
|
51
|
+
return `${value}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数值类型处理
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class NumberUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 获得Proto中的数字
|
|
8
|
+
*/
|
|
9
|
+
public static getProtoNum(data: any, key: string, defaultValue: number = 0): number {
|
|
10
|
+
if (key in data) {
|
|
11
|
+
return data[key];
|
|
12
|
+
}
|
|
13
|
+
return defaultValue;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 异步处理工具
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export default class PromiseUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 执行所有异步并返回数据
|
|
8
|
+
*/
|
|
9
|
+
public static async all(promiseArr: Promise<any>[]): Promise<any> {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const promises: Promise<any>[] = Array.from(promiseArr);
|
|
12
|
+
let r: any[] = [];
|
|
13
|
+
const len: number = promises.length;
|
|
14
|
+
let count: number = 0;
|
|
15
|
+
for (let i: number = 0; i < len; i++) {
|
|
16
|
+
Promise.resolve(promises[i]).then((o) => {
|
|
17
|
+
r[i] = o;
|
|
18
|
+
if (++count === len) {
|
|
19
|
+
resolve(r);
|
|
20
|
+
}
|
|
21
|
+
}).catch(e => reject(e));
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 随机数生成工具
|
|
3
|
+
* @author aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class RandomNumUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 获取一定范围的随机整数
|
|
8
|
+
*
|
|
9
|
+
* <b>( min ≤ r ≤ max )</b>
|
|
10
|
+
*
|
|
11
|
+
* @param min 最小数
|
|
12
|
+
* @param max 最大数
|
|
13
|
+
* @param isInt 是否返回整数
|
|
14
|
+
*/
|
|
15
|
+
public static randomNumBoth(min: number, max: number, isInt: boolean = true): number {
|
|
16
|
+
const Range: number = max - min;
|
|
17
|
+
if (Range > 0.0) {
|
|
18
|
+
const Rand: number = Math.random();
|
|
19
|
+
let num: number;
|
|
20
|
+
if (isInt) {
|
|
21
|
+
num = min + Math.round(Rand * Range); //四舍五入
|
|
22
|
+
} else {
|
|
23
|
+
num = min + Rand * Range;
|
|
24
|
+
}
|
|
25
|
+
return num;
|
|
26
|
+
} else {
|
|
27
|
+
return isInt ? min : Math.round(min);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 打乱数组
|
|
33
|
+
*/
|
|
34
|
+
public static shuffleSort<T>(arr: T[]): void {
|
|
35
|
+
let n: number = arr.length;
|
|
36
|
+
let index: number;
|
|
37
|
+
let temp: T;
|
|
38
|
+
while (n--) {
|
|
39
|
+
index = Math.floor(Math.random() * n);
|
|
40
|
+
temp = arr[index];
|
|
41
|
+
arr[index] = arr[n];
|
|
42
|
+
arr[n] = temp;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 获得偏振系数
|
|
48
|
+
* @param factor 偏振因子(>0)
|
|
49
|
+
* @param negativeIntRadio 负数的比率(> 0的整数)
|
|
50
|
+
* @param positiveIntRadio 正数的比率 (>0的整数)
|
|
51
|
+
* @return -factor 或者factor
|
|
52
|
+
*/
|
|
53
|
+
public static getPolarizationFactor(factor: number = 1, negativeIntRadio: number = 1, positiveIntRadio: number = 1): number {
|
|
54
|
+
const r: number = RandomNumUtil.randomNumWithRadio(0, 1, [negativeIntRadio, positiveIntRadio]);
|
|
55
|
+
if (r == 0) return -1 * factor;
|
|
56
|
+
return r * factor;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 按照比率获得随机数(对于整数)
|
|
61
|
+
* @param min 最小数
|
|
62
|
+
* @param max 最大数
|
|
63
|
+
* @param radioArr 比率
|
|
64
|
+
*/
|
|
65
|
+
public static randomNumWithRadio(min: number, max: number, radioArr: number[]): number {
|
|
66
|
+
let total: number = 0;
|
|
67
|
+
let i: number;
|
|
68
|
+
for (i = 0; i < radioArr.length; i++) {
|
|
69
|
+
total += radioArr[i];
|
|
70
|
+
}
|
|
71
|
+
const r: number = RandomNumUtil.randomNumBoth(1, total, true);
|
|
72
|
+
let target: number = 0;
|
|
73
|
+
for (i = 0; i < radioArr.length; i++) {
|
|
74
|
+
target += radioArr[i];
|
|
75
|
+
if (r <= target) {
|
|
76
|
+
return i + min;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return max;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 解码
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class SelfDecrypt {
|
|
6
|
+
private static b64Decode(b64: string): Uint8Array {
|
|
7
|
+
// 浏览器安全解码,自动补 =
|
|
8
|
+
const s: string = b64.replace(/[^A-Za-z0-9+/]/g, '');
|
|
9
|
+
const bin: string = atob(s);
|
|
10
|
+
return new Uint8Array(bin.length).map((_, i) => bin.charCodeAt(i));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private static b64Encode(bytes: Uint8Array): string {
|
|
14
|
+
const bin: string = String.fromCharCode(...bytes);
|
|
15
|
+
return btoa(bin);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private static xorBytes(bytes: Uint8Array, key: string): Uint8Array {
|
|
19
|
+
const k: Uint8Array = new TextEncoder().encode(key);
|
|
20
|
+
return bytes.map((b, i) => b ^ k[i % k.length]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 解码
|
|
25
|
+
*/
|
|
26
|
+
public static decryptBase64(b64: string, key: string): string {
|
|
27
|
+
const cipher: Uint8Array = SelfDecrypt.b64Decode(b64);
|
|
28
|
+
const plain: Uint8Array = SelfDecrypt.xorBytes(cipher, key);
|
|
29
|
+
return new TextDecoder().decode(plain);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字符串工具
|
|
3
|
+
* @author aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class StringUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 字符串的格式化 , 使用 {index = 0}的模式
|
|
8
|
+
* @param str 源字符串
|
|
9
|
+
* @param args 填充参数
|
|
10
|
+
*/
|
|
11
|
+
public static format(str: string, ...args: any): string {
|
|
12
|
+
if (!args || args.length == 0) return str;
|
|
13
|
+
for (let i: number = 0, j: number = args.length; i < j; i++) {
|
|
14
|
+
str = str.replace(`{${i}}`, args[i]);
|
|
15
|
+
}
|
|
16
|
+
return str;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 将字符串中所有的\/替换成/
|
|
21
|
+
*/
|
|
22
|
+
public static replaceBackslash(input: string): string {
|
|
23
|
+
// 使用正则表达式替换所有的 \\/ 为 /
|
|
24
|
+
return input.replace(/\\\//g, '/');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private static code2utf8(uni: number): string {
|
|
28
|
+
let uni2: string = uni.toString(2);
|
|
29
|
+
if (uni < 128) {
|
|
30
|
+
return uni.toString(16);
|
|
31
|
+
} else if (uni < 2048) {
|
|
32
|
+
uni2 = ("00000000000000000" + uni2).slice(-11);
|
|
33
|
+
const s1 = parseInt("110" + uni2.substring(0, 5), 2);
|
|
34
|
+
const s2 = parseInt("10" + uni2.substring(5), 2);
|
|
35
|
+
return s1.toString(16) + "," + s2.toString(16);
|
|
36
|
+
} else {
|
|
37
|
+
uni2 = ("00000000000000000" + uni2).slice(-16);
|
|
38
|
+
const s1 = parseInt("1110" + uni2.substring(0, 4), 2);
|
|
39
|
+
const s2 = parseInt("10" + uni2.substring(4, 10), 2);
|
|
40
|
+
const s3 = parseInt("10" + uni2.substring(10), 2);
|
|
41
|
+
return s1.toString(16) + "," + s2.toString(16) + "," + s3.toString(16);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 将字符串转化成UTF8的Buffer
|
|
47
|
+
* @param str 字符串
|
|
48
|
+
*/
|
|
49
|
+
public static string2UTF8Buffer(str: string): ArrayBuffer {
|
|
50
|
+
let val: string = "";
|
|
51
|
+
for (let i: number = 0; i < str.length; i++) {
|
|
52
|
+
val += "," + StringUtil.code2utf8(str.charCodeAt(i));
|
|
53
|
+
}
|
|
54
|
+
val += ",00";
|
|
55
|
+
// 将16进制转化为ArrayBuffer
|
|
56
|
+
return new Uint8Array(
|
|
57
|
+
val.match(/[\da-f]{2}/gi).map((h) => {
|
|
58
|
+
return parseInt(h, 16);
|
|
59
|
+
})
|
|
60
|
+
).buffer;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// /**
|
|
64
|
+
// * 将buffer转化成UTF8
|
|
65
|
+
// * @param buffer
|
|
66
|
+
// */
|
|
67
|
+
// public static buffer2UTF8String(buffer: ArrayBuffer): string {
|
|
68
|
+
// const encodedString: string = String.fromCharCode.apply(null, new Uint8Array(buffer));
|
|
69
|
+
// // const decodedString: string = decodeURIComponent(escape(atob(encodedString)));
|
|
70
|
+
// return encodedString;
|
|
71
|
+
// }
|
|
72
|
+
/* 将buffer转化成UTF8
|
|
73
|
+
* @param buffer
|
|
74
|
+
*/
|
|
75
|
+
public static buffer2UTF8String = (buffer: ArrayBuffer) => {
|
|
76
|
+
const uint8Array = new Uint8Array(buffer);
|
|
77
|
+
const data = uint8Array.reduce(
|
|
78
|
+
(acc, i) => (acc += String.fromCharCode.apply(null, [i])),
|
|
79
|
+
""
|
|
80
|
+
);
|
|
81
|
+
return data;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 唯一ID生成器
|
|
3
|
+
* @author Aonaufly
|
|
4
|
+
*/
|
|
5
|
+
export class UniqueIDUtil {
|
|
6
|
+
/**生成唯一ID值*/
|
|
7
|
+
public static getUniqueId(): string {
|
|
8
|
+
return new Date().getTime() + Math.random().toString(36).substring(2);
|
|
9
|
+
}
|
|
10
|
+
/**生成复合的Key值*/
|
|
11
|
+
public static getCompositeKey(key: string | number, uniqueId: string): string {
|
|
12
|
+
return `${key}-id:${uniqueId}`;
|
|
13
|
+
}
|
|
14
|
+
}
|