@etsoo/shared 1.2.50 → 1.2.52
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/README.md +1 -1
- package/__tests__/ArrayUtils.ts +4 -2
- package/lib/cjs/ArrayUtils.d.ts +1 -1
- package/lib/cjs/ArrayUtils.js +4 -2
- package/lib/cjs/IActionResult.d.ts +14 -1
- package/lib/mjs/ArrayUtils.d.ts +1 -1
- package/lib/mjs/ArrayUtils.js +4 -2
- package/lib/mjs/IActionResult.d.ts +14 -1
- package/package.json +2 -2
- package/src/ArrayUtils.ts +7 -3
- package/src/IActionResult.ts +16 -1
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Using yarn:
|
|
|
15
15
|
$ yarn add @etsoo/shared
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
## ActionResult / IActionResult, IdActionResult, DynamicActionResult
|
|
18
|
+
## ActionResult / IActionResult, IdActionResult, MsgActionResult, IdMsgActionResult, DynamicActionResult
|
|
19
19
|
|Name|Description|
|
|
20
20
|
|---:|---|
|
|
21
21
|
|static create|Create a result from error|
|
package/__tests__/ArrayUtils.ts
CHANGED
|
@@ -65,8 +65,9 @@ test('Tests for maxItem / minItem', () => {
|
|
|
65
65
|
|
|
66
66
|
test('Tests for remove simple', () => {
|
|
67
67
|
const items = [1, 2, 3, 4, 5];
|
|
68
|
-
items.remove(1, 5, (item) => item % 2 === 0);
|
|
68
|
+
const result = items.remove(1, 5, (item) => item % 2 === 0);
|
|
69
69
|
expect(items).toStrictEqual([3]);
|
|
70
|
+
expect(result.length).toBe(4);
|
|
70
71
|
});
|
|
71
72
|
|
|
72
73
|
test('Tests for remove object', () => {
|
|
@@ -78,7 +79,7 @@ test('Tests for remove object', () => {
|
|
|
78
79
|
item
|
|
79
80
|
];
|
|
80
81
|
|
|
81
|
-
items.remove(
|
|
82
|
+
const result = items.remove(
|
|
82
83
|
item,
|
|
83
84
|
(item) => item.id === 2,
|
|
84
85
|
(item) => item.amount <= 2
|
|
@@ -86,6 +87,7 @@ test('Tests for remove object', () => {
|
|
|
86
87
|
|
|
87
88
|
expect(items.length).toBe(1);
|
|
88
89
|
expect(items[0].id).toBe(1);
|
|
90
|
+
expect(result.length).toBe(3);
|
|
89
91
|
});
|
|
90
92
|
|
|
91
93
|
test('Tests for sortIds 1', () => {
|
package/lib/cjs/ArrayUtils.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ declare global {
|
|
|
31
31
|
* Remove items by value or condition
|
|
32
32
|
* @param items Items to remove
|
|
33
33
|
*/
|
|
34
|
-
remove(...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]):
|
|
34
|
+
remove(...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]): T[];
|
|
35
35
|
/**
|
|
36
36
|
* Sort by property
|
|
37
37
|
* @param property Property
|
package/lib/cjs/ArrayUtils.js
CHANGED
|
@@ -43,6 +43,7 @@ Array.prototype.minItem = function (field) {
|
|
|
43
43
|
};
|
|
44
44
|
Array.prototype.remove = function (...items) {
|
|
45
45
|
const funs = [];
|
|
46
|
+
const results = [];
|
|
46
47
|
items.forEach((item) => {
|
|
47
48
|
if (typeof item === 'function') {
|
|
48
49
|
funs.push(item);
|
|
@@ -51,16 +52,17 @@ Array.prototype.remove = function (...items) {
|
|
|
51
52
|
// For object items, should be removed by reference, not by value
|
|
52
53
|
const index = this.indexOf(item);
|
|
53
54
|
if (index >= 0)
|
|
54
|
-
this.splice(index, 1);
|
|
55
|
+
results.push(...this.splice(index, 1));
|
|
55
56
|
}
|
|
56
57
|
});
|
|
57
58
|
if (funs.length > 0) {
|
|
58
59
|
// Reduce check loops for performance
|
|
59
60
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
60
61
|
if (funs.some((fun) => fun(this[i])))
|
|
61
|
-
this.splice(i, 1);
|
|
62
|
+
results.push(...this.splice(i, 1));
|
|
62
63
|
}
|
|
63
64
|
}
|
|
65
|
+
return results;
|
|
64
66
|
};
|
|
65
67
|
Array.prototype.sortByProperty = function (property, values) {
|
|
66
68
|
return this.sort((a, b) => {
|
|
@@ -48,11 +48,24 @@ export interface IActionResult<D extends object = {}> {
|
|
|
48
48
|
readonly ok: boolean;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
* Action result with id
|
|
51
|
+
* Action result with id
|
|
52
52
|
*/
|
|
53
53
|
export type IdActionResult<T extends IdType = number> = IActionResult<{
|
|
54
54
|
id: T;
|
|
55
55
|
}>;
|
|
56
|
+
/**
|
|
57
|
+
* Action result with message data
|
|
58
|
+
*/
|
|
59
|
+
export type MsgActionResult = IActionResult<{
|
|
60
|
+
msg: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Action result with id, message data
|
|
64
|
+
*/
|
|
65
|
+
export type IdMsgActionResult = IActionResult<{
|
|
66
|
+
id: number;
|
|
67
|
+
msg: string;
|
|
68
|
+
}>;
|
|
56
69
|
/**
|
|
57
70
|
* Action result with dynamic data
|
|
58
71
|
*/
|
package/lib/mjs/ArrayUtils.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ declare global {
|
|
|
31
31
|
* Remove items by value or condition
|
|
32
32
|
* @param items Items to remove
|
|
33
33
|
*/
|
|
34
|
-
remove(...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]):
|
|
34
|
+
remove(...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]): T[];
|
|
35
35
|
/**
|
|
36
36
|
* Sort by property
|
|
37
37
|
* @param property Property
|
package/lib/mjs/ArrayUtils.js
CHANGED
|
@@ -37,6 +37,7 @@ Array.prototype.minItem = function (field) {
|
|
|
37
37
|
};
|
|
38
38
|
Array.prototype.remove = function (...items) {
|
|
39
39
|
const funs = [];
|
|
40
|
+
const results = [];
|
|
40
41
|
items.forEach((item) => {
|
|
41
42
|
if (typeof item === 'function') {
|
|
42
43
|
funs.push(item);
|
|
@@ -45,16 +46,17 @@ Array.prototype.remove = function (...items) {
|
|
|
45
46
|
// For object items, should be removed by reference, not by value
|
|
46
47
|
const index = this.indexOf(item);
|
|
47
48
|
if (index >= 0)
|
|
48
|
-
this.splice(index, 1);
|
|
49
|
+
results.push(...this.splice(index, 1));
|
|
49
50
|
}
|
|
50
51
|
});
|
|
51
52
|
if (funs.length > 0) {
|
|
52
53
|
// Reduce check loops for performance
|
|
53
54
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
54
55
|
if (funs.some((fun) => fun(this[i])))
|
|
55
|
-
this.splice(i, 1);
|
|
56
|
+
results.push(...this.splice(i, 1));
|
|
56
57
|
}
|
|
57
58
|
}
|
|
59
|
+
return results;
|
|
58
60
|
};
|
|
59
61
|
Array.prototype.sortByProperty = function (property, values) {
|
|
60
62
|
return this.sort((a, b) => {
|
|
@@ -48,11 +48,24 @@ export interface IActionResult<D extends object = {}> {
|
|
|
48
48
|
readonly ok: boolean;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
* Action result with id
|
|
51
|
+
* Action result with id
|
|
52
52
|
*/
|
|
53
53
|
export type IdActionResult<T extends IdType = number> = IActionResult<{
|
|
54
54
|
id: T;
|
|
55
55
|
}>;
|
|
56
|
+
/**
|
|
57
|
+
* Action result with message data
|
|
58
|
+
*/
|
|
59
|
+
export type MsgActionResult = IActionResult<{
|
|
60
|
+
msg: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Action result with id, message data
|
|
64
|
+
*/
|
|
65
|
+
export type IdMsgActionResult = IActionResult<{
|
|
66
|
+
id: number;
|
|
67
|
+
msg: string;
|
|
68
|
+
}>;
|
|
56
69
|
/**
|
|
57
70
|
* Action result with dynamic data
|
|
58
71
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.52",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^29.5.
|
|
57
|
+
"@types/jest": "^29.5.14",
|
|
58
58
|
"@types/lodash.isequal": "^4.5.8",
|
|
59
59
|
"jest": "^29.7.0",
|
|
60
60
|
"jest-environment-jsdom": "^29.7.0",
|
package/src/ArrayUtils.ts
CHANGED
|
@@ -59,7 +59,7 @@ declare global {
|
|
|
59
59
|
| (T & (DataTypes.Basic | object))
|
|
60
60
|
| ((item: T) => boolean)
|
|
61
61
|
)[]
|
|
62
|
-
):
|
|
62
|
+
): T[];
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* Sort by property
|
|
@@ -157,22 +157,26 @@ Array.prototype.remove = function <T>(
|
|
|
157
157
|
...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]
|
|
158
158
|
) {
|
|
159
159
|
const funs: ((item: T) => boolean)[] = [];
|
|
160
|
+
const results: T[] = [];
|
|
160
161
|
items.forEach((item) => {
|
|
161
162
|
if (typeof item === 'function') {
|
|
162
163
|
funs.push(item);
|
|
163
164
|
} else {
|
|
164
165
|
// For object items, should be removed by reference, not by value
|
|
165
166
|
const index = this.indexOf(item);
|
|
166
|
-
if (index >= 0) this.splice(index, 1);
|
|
167
|
+
if (index >= 0) results.push(...this.splice(index, 1));
|
|
167
168
|
}
|
|
168
169
|
});
|
|
169
170
|
|
|
170
171
|
if (funs.length > 0) {
|
|
171
172
|
// Reduce check loops for performance
|
|
172
173
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
173
|
-
if (funs.some((fun) => fun(this[i])))
|
|
174
|
+
if (funs.some((fun) => fun(this[i])))
|
|
175
|
+
results.push(...this.splice(i, 1));
|
|
174
176
|
}
|
|
175
177
|
}
|
|
178
|
+
|
|
179
|
+
return results;
|
|
176
180
|
};
|
|
177
181
|
|
|
178
182
|
Array.prototype.sortByProperty = function <T, P extends keyof T>(
|
package/src/IActionResult.ts
CHANGED
|
@@ -59,12 +59,27 @@ export interface IActionResult<D extends object = {}> {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* Action result with id
|
|
62
|
+
* Action result with id
|
|
63
63
|
*/
|
|
64
64
|
export type IdActionResult<T extends IdType = number> = IActionResult<{
|
|
65
65
|
id: T;
|
|
66
66
|
}>;
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Action result with message data
|
|
70
|
+
*/
|
|
71
|
+
export type MsgActionResult = IActionResult<{
|
|
72
|
+
msg: string;
|
|
73
|
+
}>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Action result with id, message data
|
|
77
|
+
*/
|
|
78
|
+
export type IdMsgActionResult = IActionResult<{
|
|
79
|
+
id: number;
|
|
80
|
+
msg: string;
|
|
81
|
+
}>;
|
|
82
|
+
|
|
68
83
|
/**
|
|
69
84
|
* Action result with dynamic data
|
|
70
85
|
*/
|