@etsoo/shared 1.2.52 → 1.2.55
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/.github/workflows/main.yml +6 -5
- package/__tests__/ArrayUtils.ts +126 -126
- package/__tests__/ColorUtils.ts +24 -24
- package/__tests__/ContentDisposition.ts +18 -18
- package/__tests__/DataTypes.ts +222 -204
- package/__tests__/DateUtils.ts +82 -82
- package/__tests__/DomUtils.ts +352 -352
- package/__tests__/EHistory.ts +62 -62
- package/__tests__/ExtendUtils.ts +40 -40
- package/__tests__/Keyboard.ts +14 -14
- package/__tests__/NumberUtils.ts +37 -39
- package/__tests__/StorageUtils.ts +27 -27
- package/__tests__/Utils.ts +361 -363
- package/__tests__/tsconfig.json +15 -15
- package/lib/cjs/ActionResult.d.ts +1 -1
- package/lib/cjs/ActionResult.js +3 -3
- package/lib/cjs/ArrayUtils.d.ts +1 -1
- package/lib/cjs/ArrayUtils.js +4 -4
- package/lib/cjs/ColorUtils.d.ts +1 -1
- package/lib/cjs/ColorUtils.js +2 -2
- package/lib/cjs/DataTypes.d.ts +6 -6
- package/lib/cjs/DataTypes.js +50 -51
- package/lib/cjs/DateUtils.d.ts +1 -1
- package/lib/cjs/DateUtils.js +27 -28
- package/lib/cjs/DomUtils.d.ts +3 -3
- package/lib/cjs/DomUtils.js +64 -73
- package/lib/cjs/ExtendUtils.d.ts +1 -1
- package/lib/cjs/ExtendUtils.js +6 -6
- package/lib/cjs/IActionResult.d.ts +1 -1
- package/lib/cjs/NumberUtils.d.ts +1 -1
- package/lib/cjs/NumberUtils.js +9 -9
- package/lib/cjs/StorageUtils.js +2 -2
- package/lib/cjs/Utils.d.ts +4 -4
- package/lib/cjs/Utils.js +58 -62
- package/lib/cjs/index.d.ts +22 -22
- package/lib/cjs/storage/WindowStorage.d.ts +1 -1
- package/lib/cjs/types/ContentDisposition.d.ts +2 -2
- package/lib/cjs/types/ContentDisposition.js +11 -13
- package/lib/cjs/types/EColor.js +5 -7
- package/lib/cjs/types/EHistory.d.ts +3 -3
- package/lib/cjs/types/EHistory.js +4 -4
- package/lib/cjs/types/ErrorData.d.ts +1 -1
- package/lib/cjs/types/EventClass.js +1 -1
- package/lib/mjs/ActionResult.d.ts +1 -1
- package/lib/mjs/ActionResult.js +3 -3
- package/lib/mjs/ArrayUtils.d.ts +1 -1
- package/lib/mjs/ArrayUtils.js +5 -5
- package/lib/mjs/ColorUtils.d.ts +1 -1
- package/lib/mjs/ColorUtils.js +3 -3
- package/lib/mjs/DataTypes.d.ts +6 -6
- package/lib/mjs/DataTypes.js +50 -51
- package/lib/mjs/DateUtils.d.ts +1 -1
- package/lib/mjs/DateUtils.js +27 -28
- package/lib/mjs/DomUtils.d.ts +3 -3
- package/lib/mjs/DomUtils.js +67 -76
- package/lib/mjs/ExtendUtils.d.ts +1 -1
- package/lib/mjs/ExtendUtils.js +6 -6
- package/lib/mjs/IActionResult.d.ts +1 -1
- package/lib/mjs/NumberUtils.d.ts +1 -1
- package/lib/mjs/NumberUtils.js +9 -9
- package/lib/mjs/StorageUtils.js +4 -4
- package/lib/mjs/Utils.d.ts +4 -4
- package/lib/mjs/Utils.js +61 -65
- package/lib/mjs/index.d.ts +22 -22
- package/lib/mjs/index.js +22 -22
- package/lib/mjs/storage/WindowStorage.d.ts +1 -1
- package/lib/mjs/storage/WindowStorage.js +2 -2
- package/lib/mjs/types/ContentDisposition.d.ts +2 -2
- package/lib/mjs/types/ContentDisposition.js +12 -14
- package/lib/mjs/types/EColor.js +5 -7
- package/lib/mjs/types/EHistory.d.ts +3 -3
- package/lib/mjs/types/EHistory.js +5 -5
- package/lib/mjs/types/ErrorData.d.ts +1 -1
- package/lib/mjs/types/EventClass.js +1 -1
- package/package.json +61 -63
- package/src/ActionResult.ts +23 -23
- package/src/ArrayUtils.ts +164 -172
- package/src/ColorUtils.ts +80 -82
- package/src/DataTypes.ts +745 -754
- package/src/DateUtils.ts +266 -268
- package/src/DomUtils.ts +806 -831
- package/src/ExtendUtils.ts +191 -191
- package/src/IActionResult.ts +42 -42
- package/src/Keyboard.ts +258 -258
- package/src/NumberUtils.ts +135 -135
- package/src/StorageUtils.ts +117 -117
- package/src/Utils.ts +908 -930
- package/src/index.ts +22 -22
- package/src/node/Storage.ts +53 -53
- package/src/storage/IStorage.ts +62 -62
- package/src/storage/WindowStorage.ts +140 -140
- package/src/types/ContentDisposition.ts +59 -63
- package/src/types/DataError.ts +15 -15
- package/src/types/DelayedExecutorType.ts +15 -15
- package/src/types/EColor.ts +241 -248
- package/src/types/EHistory.ts +151 -151
- package/src/types/ErrorData.ts +11 -11
- package/src/types/EventClass.ts +220 -220
- package/src/types/FormData.ts +25 -25
- package/src/types/ParsedPath.ts +5 -5
- package/tsconfig.cjs.json +16 -16
- package/tsconfig.json +16 -16
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -29
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
|
@@ -85,7 +85,7 @@ export class EventClass {
|
|
|
85
85
|
* @param options Options
|
|
86
86
|
*/
|
|
87
87
|
on(type, callback, options) {
|
|
88
|
-
if (typeof type ===
|
|
88
|
+
if (typeof type === "object") {
|
|
89
89
|
for (const key in type) {
|
|
90
90
|
const item = key;
|
|
91
91
|
const itemCallback = type[item] ?? callback;
|
package/package.json
CHANGED
|
@@ -1,67 +1,65 @@
|
|
|
1
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
|
-
"ts",
|
|
31
|
-
"d.ts"
|
|
32
|
-
],
|
|
33
|
-
"transform": {
|
|
34
|
-
".+\\.ts$": "ts-jest"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"repository": {
|
|
38
|
-
"type": "git",
|
|
39
|
-
"url": "git+https://github.com/ETSOO/Shared.git"
|
|
40
|
-
},
|
|
41
|
-
"keywords": [
|
|
42
|
-
"Shared utilities",
|
|
43
|
-
"TypeScript",
|
|
44
|
-
"ETSOO",
|
|
45
|
-
"SmartERP",
|
|
46
|
-
"司友云平台",
|
|
47
|
-
"青岛亿速思维",
|
|
48
|
-
"上海亿商"
|
|
2
|
+
"name": "@etsoo/shared",
|
|
3
|
+
"version": "1.2.55",
|
|
4
|
+
"description": "TypeScript shared utilities and functions",
|
|
5
|
+
"main": "lib/cjs/index.js",
|
|
6
|
+
"module": "lib/mjs/index.js",
|
|
7
|
+
"types": "lib/mjs/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./lib/mjs/index.js",
|
|
11
|
+
"require": "./lib/cjs/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",
|
|
17
|
+
"test": "jest",
|
|
18
|
+
"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand"
|
|
19
|
+
},
|
|
20
|
+
"jest": {
|
|
21
|
+
"automock": false,
|
|
22
|
+
"testMatch": [
|
|
23
|
+
"<rootDir>/__tests__/**/*.ts"
|
|
24
|
+
],
|
|
25
|
+
"testEnvironment": "jsdom",
|
|
26
|
+
"moduleFileExtensions": [
|
|
27
|
+
"js",
|
|
28
|
+
"ts",
|
|
29
|
+
"d.ts"
|
|
49
30
|
],
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
"bugs": {
|
|
53
|
-
"url": "https://github.com/ETSOO/Shared/issues"
|
|
54
|
-
},
|
|
55
|
-
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
56
|
-
"devDependencies": {
|
|
57
|
-
"@types/jest": "^29.5.14",
|
|
58
|
-
"@types/lodash.isequal": "^4.5.8",
|
|
59
|
-
"jest": "^29.7.0",
|
|
60
|
-
"jest-environment-jsdom": "^29.7.0",
|
|
61
|
-
"ts-jest": "^29.2.5",
|
|
62
|
-
"typescript": "^5.6.3"
|
|
63
|
-
},
|
|
64
|
-
"dependencies": {
|
|
65
|
-
"lodash.isequal": "^4.5.0"
|
|
31
|
+
"transform": {
|
|
32
|
+
".+\\.ts$": "ts-jest"
|
|
66
33
|
}
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/ETSOO/Shared.git"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"Shared utilities",
|
|
41
|
+
"TypeScript",
|
|
42
|
+
"ETSOO",
|
|
43
|
+
"SmartERP",
|
|
44
|
+
"司友云平台",
|
|
45
|
+
"青岛亿速思维",
|
|
46
|
+
"上海亿商"
|
|
47
|
+
],
|
|
48
|
+
"author": "Garry Xiao",
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/ETSOO/Shared/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/jest": "^29.5.14",
|
|
56
|
+
"@types/lodash.isequal": "^4.5.8",
|
|
57
|
+
"jest": "^29.7.0",
|
|
58
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
59
|
+
"ts-jest": "^29.2.5",
|
|
60
|
+
"typescript": "^5.7.2"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"lodash.isequal": "^4.5.0"
|
|
64
|
+
}
|
|
67
65
|
}
|
package/src/ActionResult.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { IActionResult } from
|
|
1
|
+
import { IActionResult } from "./IActionResult";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Action result
|
|
5
5
|
*/
|
|
6
6
|
export class ActionResult {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Create a result from error
|
|
9
|
+
* @returns Action result interface
|
|
10
|
+
*/
|
|
11
|
+
static create<D extends object = {}>(error: Error) {
|
|
12
|
+
// If the error has status / statusCode
|
|
13
|
+
const status =
|
|
14
|
+
"status" in error
|
|
15
|
+
? error.status
|
|
16
|
+
: "statusCode" in error
|
|
17
|
+
? error.statusCode
|
|
18
|
+
: undefined;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
// Result
|
|
21
|
+
const result: IActionResult<D> = {
|
|
22
|
+
status: typeof status === "number" ? status : undefined,
|
|
23
|
+
ok: false,
|
|
24
|
+
type: error.name,
|
|
25
|
+
title: error.message
|
|
26
|
+
};
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
// Return
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
31
|
}
|
package/src/ArrayUtils.ts
CHANGED
|
@@ -1,223 +1,215 @@
|
|
|
1
|
-
import isEqual from
|
|
2
|
-
import { DataTypes } from
|
|
1
|
+
import isEqual from "lodash.isequal";
|
|
2
|
+
import { DataTypes } from "./DataTypes";
|
|
3
3
|
|
|
4
4
|
declare global {
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
*/
|
|
87
|
-
toUnique(): Array<T>;
|
|
88
|
-
}
|
|
5
|
+
interface Array<T> {
|
|
6
|
+
/**
|
|
7
|
+
* Items do not exist in target array or reverse match
|
|
8
|
+
* @param target Target array
|
|
9
|
+
* @param round A round for both matches
|
|
10
|
+
*/
|
|
11
|
+
different(target: Array<T>, round?: boolean): Array<T>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get max number item or number item property
|
|
15
|
+
* @param field Object field to calculate
|
|
16
|
+
*/
|
|
17
|
+
max(
|
|
18
|
+
...field: T extends number
|
|
19
|
+
? [undefined?]
|
|
20
|
+
: T extends object
|
|
21
|
+
? [DataTypes.Keys<T, number>]
|
|
22
|
+
: [never]
|
|
23
|
+
): number;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get max field value item
|
|
27
|
+
* @param field Object field to calculate
|
|
28
|
+
*/
|
|
29
|
+
maxItem(
|
|
30
|
+
field: T extends object ? DataTypes.Keys<T, number> : never
|
|
31
|
+
): T | undefined;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get min number item or number item property
|
|
35
|
+
* @param field Object field to calculate
|
|
36
|
+
*/
|
|
37
|
+
min(
|
|
38
|
+
...field: T extends number
|
|
39
|
+
? [undefined?]
|
|
40
|
+
: T extends object
|
|
41
|
+
? [DataTypes.Keys<T, number>]
|
|
42
|
+
: [never]
|
|
43
|
+
): number;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get min field value item
|
|
47
|
+
* @param field Object field to calculate
|
|
48
|
+
*/
|
|
49
|
+
minItem(
|
|
50
|
+
field: T extends object ? DataTypes.Keys<T, number> : never
|
|
51
|
+
): T | undefined;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Remove items by value or condition
|
|
55
|
+
* @param items Items to remove
|
|
56
|
+
*/
|
|
57
|
+
remove(
|
|
58
|
+
...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]
|
|
59
|
+
): T[];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Sort by property
|
|
63
|
+
* @param property Property
|
|
64
|
+
* @param values Property values
|
|
65
|
+
*/
|
|
66
|
+
sortByProperty<P extends keyof T>(property: P, values: T[P][]): T[];
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Sum number items or number item properties
|
|
70
|
+
* @param field Object field to calculate
|
|
71
|
+
*/
|
|
72
|
+
sum(
|
|
73
|
+
...field: T extends number
|
|
74
|
+
? [undefined?]
|
|
75
|
+
: T extends object
|
|
76
|
+
? [DataTypes.Keys<T, number>]
|
|
77
|
+
: [never]
|
|
78
|
+
): number;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Make all items are unique
|
|
82
|
+
* @param this Input array
|
|
83
|
+
*/
|
|
84
|
+
toUnique(): Array<T>;
|
|
85
|
+
}
|
|
89
86
|
}
|
|
90
87
|
|
|
91
88
|
Array.prototype.different = function <T>(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
this: Array<T>,
|
|
90
|
+
target: Array<T>,
|
|
91
|
+
round?: boolean
|
|
95
92
|
) {
|
|
96
|
-
|
|
93
|
+
return ArrayUtils.differences(this, target, round);
|
|
97
94
|
};
|
|
98
95
|
|
|
99
96
|
Array.prototype.toUnique = function <T>(this: Array<T>) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
97
|
+
if (this.length === 0 || typeof this[0] !== "object")
|
|
98
|
+
return Array.from(new Set(this));
|
|
99
|
+
|
|
100
|
+
const newArray: T[] = [];
|
|
101
|
+
this.forEach((item) => {
|
|
102
|
+
if (newArray.some((newItem) => isEqual(item, newItem))) return;
|
|
103
|
+
newArray.push(item);
|
|
104
|
+
});
|
|
105
|
+
return newArray;
|
|
109
106
|
};
|
|
110
107
|
|
|
111
108
|
Array.prototype.max = function <T>(
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
this: Array<T>,
|
|
110
|
+
field: T extends object ? DataTypes.Keys<T, number> : undefined
|
|
114
111
|
) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
if (field == null) {
|
|
113
|
+
return Math.max(...(this as Array<number>));
|
|
114
|
+
}
|
|
118
115
|
|
|
119
|
-
|
|
116
|
+
return Math.max(...this.map((item) => item[field] as number));
|
|
120
117
|
};
|
|
121
118
|
|
|
122
119
|
Array.prototype.maxItem = function <T>(
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
this: Array<T>,
|
|
121
|
+
field: T extends object ? DataTypes.Keys<T, number> : never
|
|
125
122
|
) {
|
|
126
|
-
|
|
123
|
+
if (this.length === 0) return undefined;
|
|
127
124
|
|
|
128
|
-
|
|
129
|
-
prev[field] > curr[field] ? prev : curr
|
|
130
|
-
);
|
|
125
|
+
return this.reduce((prev, curr) => (prev[field] > curr[field] ? prev : curr));
|
|
131
126
|
};
|
|
132
127
|
|
|
133
128
|
Array.prototype.min = function <T>(
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
this: Array<T>,
|
|
130
|
+
field: T extends object ? DataTypes.Keys<T, number> : undefined
|
|
136
131
|
) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
132
|
+
if (field == null) {
|
|
133
|
+
return Math.min(...(this as Array<number>));
|
|
134
|
+
}
|
|
140
135
|
|
|
141
|
-
|
|
136
|
+
return Math.min(...this.map((item) => item[field] as number));
|
|
142
137
|
};
|
|
143
138
|
|
|
144
139
|
Array.prototype.minItem = function <T>(
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
this: Array<T>,
|
|
141
|
+
field: T extends object ? DataTypes.Keys<T, number> : never
|
|
147
142
|
) {
|
|
148
|
-
|
|
143
|
+
if (this.length === 0) return undefined;
|
|
149
144
|
|
|
150
|
-
|
|
151
|
-
prev[field] < curr[field] ? prev : curr
|
|
152
|
-
);
|
|
145
|
+
return this.reduce((prev, curr) => (prev[field] < curr[field] ? prev : curr));
|
|
153
146
|
};
|
|
154
147
|
|
|
155
148
|
Array.prototype.remove = function <T>(
|
|
156
|
-
|
|
157
|
-
|
|
149
|
+
this: Array<T>,
|
|
150
|
+
...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]
|
|
158
151
|
) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
if (funs.length > 0) {
|
|
172
|
-
// Reduce check loops for performance
|
|
173
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
174
|
-
if (funs.some((fun) => fun(this[i])))
|
|
175
|
-
results.push(...this.splice(i, 1));
|
|
176
|
-
}
|
|
152
|
+
const funs: ((item: T) => boolean)[] = [];
|
|
153
|
+
const results: T[] = [];
|
|
154
|
+
items.forEach((item) => {
|
|
155
|
+
if (typeof item === "function") {
|
|
156
|
+
funs.push(item);
|
|
157
|
+
} else {
|
|
158
|
+
// For object items, should be removed by reference, not by value
|
|
159
|
+
const index = this.indexOf(item);
|
|
160
|
+
if (index >= 0) results.push(...this.splice(index, 1));
|
|
177
161
|
}
|
|
162
|
+
});
|
|
178
163
|
|
|
179
|
-
|
|
164
|
+
if (funs.length > 0) {
|
|
165
|
+
// Reduce check loops for performance
|
|
166
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
167
|
+
if (funs.some((fun) => fun(this[i]))) results.push(...this.splice(i, 1));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return results;
|
|
180
172
|
};
|
|
181
173
|
|
|
182
174
|
Array.prototype.sortByProperty = function <T, P extends keyof T>(
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
175
|
+
this: Array<T>,
|
|
176
|
+
property: P,
|
|
177
|
+
values: T[P][]
|
|
186
178
|
) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
179
|
+
return this.sort((a, b) => {
|
|
180
|
+
const ai = values.indexOf(a[property]);
|
|
181
|
+
const bi = values.indexOf(b[property]);
|
|
182
|
+
|
|
183
|
+
if (ai === bi) return 0;
|
|
184
|
+
if (ai < 0 || bi < 0) return bi === 0 ? 1 : bi;
|
|
185
|
+
return ai - bi;
|
|
186
|
+
});
|
|
195
187
|
};
|
|
196
188
|
|
|
197
189
|
Array.prototype.sum = function <T>(
|
|
198
|
-
|
|
199
|
-
|
|
190
|
+
this: Array<T>,
|
|
191
|
+
field: T extends object ? DataTypes.Keys<T, number> : undefined
|
|
200
192
|
) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
193
|
+
if (field == null) {
|
|
194
|
+
return this.reduce((total, num) => total + (num as number), 0);
|
|
195
|
+
}
|
|
204
196
|
|
|
205
|
-
|
|
197
|
+
return this.reduce((total, item) => total + (item[field] as number), 0);
|
|
206
198
|
};
|
|
207
199
|
|
|
208
200
|
/**
|
|
209
201
|
* Array Utilities
|
|
210
202
|
*/
|
|
211
203
|
export namespace ArrayUtils {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Array 1 items do not exist in Array 2 or reverse match
|
|
206
|
+
* @param a1 Array 1
|
|
207
|
+
* @param a2 Array 2
|
|
208
|
+
* @param round A round for both matches
|
|
209
|
+
*/
|
|
210
|
+
export function differences<T>(a1: T[], a2: T[], round?: boolean) {
|
|
211
|
+
const diff = a1.filter((x) => !a2.includes(x));
|
|
212
|
+
if (round) return [...diff, ...a2.filter((x) => !a1.includes(x))];
|
|
213
|
+
return diff;
|
|
214
|
+
}
|
|
223
215
|
}
|