@etsoo/shared 1.2.10 → 1.2.11
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/__tests__/ArrayUtils.ts +29 -0
- package/lib/cjs/ArrayUtils.d.ts +6 -0
- package/lib/cjs/ArrayUtils.js +14 -0
- package/lib/mjs/ArrayUtils.d.ts +6 -0
- package/lib/mjs/ArrayUtils.js +14 -0
- package/package.json +1 -1
- package/src/ArrayUtils.ts +23 -0
package/__tests__/ArrayUtils.ts
CHANGED
|
@@ -62,3 +62,32 @@ test('Tests for maxItem / minItem', () => {
|
|
|
62
62
|
const emptyItems: { id: string; amount: number }[] = [];
|
|
63
63
|
expect(emptyItems.maxItem('amount')).toBeUndefined();
|
|
64
64
|
});
|
|
65
|
+
|
|
66
|
+
test('Tests for sortIds', () => {
|
|
67
|
+
const source = [
|
|
68
|
+
{
|
|
69
|
+
id: 'zh-Hans',
|
|
70
|
+
label: '中文(简体), Chinese (Simplified)'
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
id: 'en',
|
|
74
|
+
label: '英语, English'
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
id: 'fr',
|
|
78
|
+
label: '法语, French'
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'de',
|
|
82
|
+
label: '德语, German'
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 'zh-Hant',
|
|
86
|
+
label: '中文(繁体), Chinese (Traditional)'
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
source.sortByProperty('id', ['en', 'zh-Hans', 'zh-Hant']);
|
|
90
|
+
|
|
91
|
+
const ids = source.map((s) => s.id);
|
|
92
|
+
expect(ids).toStrictEqual(['en', 'zh-Hans', 'zh-Hant', 'fr', 'de']);
|
|
93
|
+
});
|
package/lib/cjs/ArrayUtils.d.ts
CHANGED
|
@@ -27,6 +27,12 @@ declare global {
|
|
|
27
27
|
* @param field Object field to calculate
|
|
28
28
|
*/
|
|
29
29
|
minItem(field: T extends object ? DataTypes.Keys<T, number> : never): T | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Sort by property
|
|
32
|
+
* @param property Property
|
|
33
|
+
* @param values Property values
|
|
34
|
+
*/
|
|
35
|
+
sortByProperty<P extends keyof T>(property: P, values: T[P][]): T[];
|
|
30
36
|
/**
|
|
31
37
|
* Sum number items or number item properties
|
|
32
38
|
* @param field Object field to calculate
|
package/lib/cjs/ArrayUtils.js
CHANGED
|
@@ -41,6 +41,20 @@ Array.prototype.minItem = function (field) {
|
|
|
41
41
|
return undefined;
|
|
42
42
|
return this.reduce((prev, curr) => prev[field] < curr[field] ? prev : curr);
|
|
43
43
|
};
|
|
44
|
+
Array.prototype.sortByProperty = function (property, values) {
|
|
45
|
+
return this.sort((a, b) => {
|
|
46
|
+
const ai = values.indexOf(a[property]);
|
|
47
|
+
const bi = values.indexOf(b[property]);
|
|
48
|
+
if (ai === bi)
|
|
49
|
+
return 0;
|
|
50
|
+
if (ai < 0)
|
|
51
|
+
return bi;
|
|
52
|
+
else if (bi < 0)
|
|
53
|
+
return -ai;
|
|
54
|
+
else
|
|
55
|
+
return ai - bi;
|
|
56
|
+
});
|
|
57
|
+
};
|
|
44
58
|
Array.prototype.sum = function (field) {
|
|
45
59
|
if (field == null) {
|
|
46
60
|
return this.reduce((total, num) => total + num, 0);
|
package/lib/mjs/ArrayUtils.d.ts
CHANGED
|
@@ -27,6 +27,12 @@ declare global {
|
|
|
27
27
|
* @param field Object field to calculate
|
|
28
28
|
*/
|
|
29
29
|
minItem(field: T extends object ? DataTypes.Keys<T, number> : never): T | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Sort by property
|
|
32
|
+
* @param property Property
|
|
33
|
+
* @param values Property values
|
|
34
|
+
*/
|
|
35
|
+
sortByProperty<P extends keyof T>(property: P, values: T[P][]): T[];
|
|
30
36
|
/**
|
|
31
37
|
* Sum number items or number item properties
|
|
32
38
|
* @param field Object field to calculate
|
package/lib/mjs/ArrayUtils.js
CHANGED
|
@@ -35,6 +35,20 @@ Array.prototype.minItem = function (field) {
|
|
|
35
35
|
return undefined;
|
|
36
36
|
return this.reduce((prev, curr) => prev[field] < curr[field] ? prev : curr);
|
|
37
37
|
};
|
|
38
|
+
Array.prototype.sortByProperty = function (property, values) {
|
|
39
|
+
return this.sort((a, b) => {
|
|
40
|
+
const ai = values.indexOf(a[property]);
|
|
41
|
+
const bi = values.indexOf(b[property]);
|
|
42
|
+
if (ai === bi)
|
|
43
|
+
return 0;
|
|
44
|
+
if (ai < 0)
|
|
45
|
+
return bi;
|
|
46
|
+
else if (bi < 0)
|
|
47
|
+
return -ai;
|
|
48
|
+
else
|
|
49
|
+
return ai - bi;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
38
52
|
Array.prototype.sum = function (field) {
|
|
39
53
|
if (field == null) {
|
|
40
54
|
return this.reduce((total, num) => total + num, 0);
|
package/package.json
CHANGED
package/src/ArrayUtils.ts
CHANGED
|
@@ -50,6 +50,13 @@ declare global {
|
|
|
50
50
|
field: T extends object ? DataTypes.Keys<T, number> : never
|
|
51
51
|
): T | undefined;
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Sort by property
|
|
55
|
+
* @param property Property
|
|
56
|
+
* @param values Property values
|
|
57
|
+
*/
|
|
58
|
+
sortByProperty<P extends keyof T>(property: P, values: T[P][]): T[];
|
|
59
|
+
|
|
53
60
|
/**
|
|
54
61
|
* Sum number items or number item properties
|
|
55
62
|
* @param field Object field to calculate
|
|
@@ -134,6 +141,22 @@ Array.prototype.minItem = function <T>(
|
|
|
134
141
|
);
|
|
135
142
|
};
|
|
136
143
|
|
|
144
|
+
Array.prototype.sortByProperty = function <T, P extends keyof T>(
|
|
145
|
+
this: Array<T>,
|
|
146
|
+
property: P,
|
|
147
|
+
values: T[P][]
|
|
148
|
+
) {
|
|
149
|
+
return this.sort((a, b) => {
|
|
150
|
+
const ai = values.indexOf(a[property]);
|
|
151
|
+
const bi = values.indexOf(b[property]);
|
|
152
|
+
|
|
153
|
+
if (ai === bi) return 0;
|
|
154
|
+
if (ai < 0) return bi;
|
|
155
|
+
else if (bi < 0) return -ai;
|
|
156
|
+
else return ai - bi;
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
|
|
137
160
|
Array.prototype.sum = function <T>(
|
|
138
161
|
this: Array<T>,
|
|
139
162
|
field: T extends object ? DataTypes.Keys<T, number> : undefined
|