@etsoo/shared 1.2.10 → 1.2.12
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 +59 -0
- package/lib/cjs/ArrayUtils.d.ts +6 -0
- package/lib/cjs/ArrayUtils.js +11 -0
- package/lib/mjs/ArrayUtils.d.ts +6 -0
- package/lib/mjs/ArrayUtils.js +11 -0
- package/package.json +1 -1
- package/src/ArrayUtils.ts +22 -0
package/__tests__/ArrayUtils.ts
CHANGED
|
@@ -62,3 +62,62 @@ 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 1', () => {
|
|
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
|
+
});
|
|
94
|
+
|
|
95
|
+
test('Tests for sortIds 2', () => {
|
|
96
|
+
const source = [
|
|
97
|
+
{ id: 'AUD', label: '澳元' },
|
|
98
|
+
{ id: 'CAD', label: '加元' },
|
|
99
|
+
{ id: 'CNY', label: '人民币' },
|
|
100
|
+
{ id: 'EUR', label: '欧元' },
|
|
101
|
+
{ id: 'GBP', label: '英镑' },
|
|
102
|
+
{ id: 'HKD', label: '港币' },
|
|
103
|
+
{ id: 'JPY', label: '日元' },
|
|
104
|
+
{ id: 'NZD', label: '纽币' },
|
|
105
|
+
{ id: 'SGD', label: '新币' },
|
|
106
|
+
{ id: 'USD', label: '美元' }
|
|
107
|
+
];
|
|
108
|
+
source.sortByProperty('id', ['USD', 'CNY']);
|
|
109
|
+
|
|
110
|
+
const ids = source.map((s) => s.id);
|
|
111
|
+
expect(ids).toStrictEqual([
|
|
112
|
+
'USD',
|
|
113
|
+
'CNY',
|
|
114
|
+
'AUD',
|
|
115
|
+
'CAD',
|
|
116
|
+
'EUR',
|
|
117
|
+
'GBP',
|
|
118
|
+
'HKD',
|
|
119
|
+
'JPY',
|
|
120
|
+
'NZD',
|
|
121
|
+
'SGD'
|
|
122
|
+
]);
|
|
123
|
+
});
|
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,17 @@ 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 || bi < 0)
|
|
51
|
+
return bi === 0 ? 1 : bi;
|
|
52
|
+
return ai - bi;
|
|
53
|
+
});
|
|
54
|
+
};
|
|
44
55
|
Array.prototype.sum = function (field) {
|
|
45
56
|
if (field == null) {
|
|
46
57
|
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,17 @@ 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 || bi < 0)
|
|
45
|
+
return bi === 0 ? 1 : bi;
|
|
46
|
+
return ai - bi;
|
|
47
|
+
});
|
|
48
|
+
};
|
|
38
49
|
Array.prototype.sum = function (field) {
|
|
39
50
|
if (field == null) {
|
|
40
51
|
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,21 @@ 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 || bi < 0) return bi === 0 ? 1 : bi;
|
|
155
|
+
return ai - bi;
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
|
|
137
159
|
Array.prototype.sum = function <T>(
|
|
138
160
|
this: Array<T>,
|
|
139
161
|
field: T extends object ? DataTypes.Keys<T, number> : undefined
|