@etsoo/shared 1.2.9 → 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/README.md CHANGED
@@ -136,13 +136,16 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
136
136
  |Func<R>|Function type, R is return type|
137
137
  |HAlign|Horizontal align|
138
138
  |HAlignEnum|Horizontal align enum|
139
+ |IdDefaultType|Id default type|
139
140
  |IdType|Number and string combination id type|
140
141
  |IdItem|Item with id or id generator|
141
142
  |IdLabelItem|Item with id and label|
142
143
  |IdLabelType|Item with id and label dynamic type|
143
144
  |IdNameItem|Item with id and name|
145
+ |IdTitleItem|Item with id and title|
144
146
  |KeyCollection|Key collection, like { key1: {}, key2: {} }|
145
147
  |Keys|Get specific type keys|
148
+ |LabelDefaultType|Label default type|
146
149
  |MConstructor|Mixins constructor|
147
150
  |ObjType|Generic object type|
148
151
  |Optional|Make properties optional|
@@ -155,6 +158,7 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
155
158
  |SimpleObject|Simple object, string key, simple type and null value Record|
156
159
  |StringDictionary|String key, string value Record|
157
160
  |StringRecord|String key, unknown value Record|
161
+ |TitleDefaultType|Title default type|
158
162
  |VAlign|Vertical align|
159
163
  |VAlignEnum|Vertical align enum|
160
164
  |Methods||
@@ -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
+ });
@@ -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
@@ -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);
@@ -224,6 +224,15 @@ export declare namespace DataTypes {
224
224
  */
225
225
  name: string;
226
226
  };
227
+ /**
228
+ * Item with id and title property
229
+ */
230
+ type IdTitleItem<T extends IdType = number> = IdItem<T> & {
231
+ /**
232
+ * title field
233
+ */
234
+ title: string;
235
+ };
227
236
  /**
228
237
  * Item with id and label dynamic type
229
238
  */
@@ -396,7 +405,7 @@ export type ListType = DataTypes.IdLabelItem<number>;
396
405
  */
397
406
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
407
  /**
399
- * List item with compatible id and name / label
408
+ * List item with compatible id and name / label / title
400
409
  */
401
410
  export type ListType2 = {
402
411
  id: DataTypes.IdType;
@@ -404,6 +413,8 @@ export type ListType2 = {
404
413
  label: string;
405
414
  } | {
406
415
  name: string;
416
+ } | {
417
+ title: string;
407
418
  });
408
419
  /**
409
420
  * Id default type
@@ -417,3 +428,9 @@ export type IdDefaultType<T extends object> = T extends {
417
428
  export type LabelDefaultType<T extends object> = T extends {
418
429
  label: string;
419
430
  } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
431
+ /**
432
+ * Title default type
433
+ */
434
+ export type TitleDefaultType<T extends object> = T extends {
435
+ title: string;
436
+ } ? DataTypes.Keys<T, string> & 'title' : DataTypes.Keys<T, string>;
@@ -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
@@ -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);
@@ -224,6 +224,15 @@ export declare namespace DataTypes {
224
224
  */
225
225
  name: string;
226
226
  };
227
+ /**
228
+ * Item with id and title property
229
+ */
230
+ type IdTitleItem<T extends IdType = number> = IdItem<T> & {
231
+ /**
232
+ * title field
233
+ */
234
+ title: string;
235
+ };
227
236
  /**
228
237
  * Item with id and label dynamic type
229
238
  */
@@ -396,7 +405,7 @@ export type ListType = DataTypes.IdLabelItem<number>;
396
405
  */
397
406
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
407
  /**
399
- * List item with compatible id and name / label
408
+ * List item with compatible id and name / label / title
400
409
  */
401
410
  export type ListType2 = {
402
411
  id: DataTypes.IdType;
@@ -404,6 +413,8 @@ export type ListType2 = {
404
413
  label: string;
405
414
  } | {
406
415
  name: string;
416
+ } | {
417
+ title: string;
407
418
  });
408
419
  /**
409
420
  * Id default type
@@ -417,3 +428,9 @@ export type IdDefaultType<T extends object> = T extends {
417
428
  export type LabelDefaultType<T extends object> = T extends {
418
429
  label: string;
419
430
  } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
431
+ /**
432
+ * Title default type
433
+ */
434
+ export type TitleDefaultType<T extends object> = T extends {
435
+ title: string;
436
+ } ? DataTypes.Keys<T, string> & 'title' : DataTypes.Keys<T, string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
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
package/src/DataTypes.ts CHANGED
@@ -278,6 +278,16 @@ export namespace DataTypes {
278
278
  name: string;
279
279
  };
280
280
 
281
+ /**
282
+ * Item with id and title property
283
+ */
284
+ export type IdTitleItem<T extends IdType = number> = IdItem<T> & {
285
+ /**
286
+ * title field
287
+ */
288
+ title: string;
289
+ };
290
+
281
291
  /**
282
292
  * Item with id and label dynamic type
283
293
  */
@@ -751,11 +761,11 @@ export type ListType = DataTypes.IdLabelItem<number>;
751
761
  export type ListType1 = DataTypes.IdLabelItem<string>;
752
762
 
753
763
  /**
754
- * List item with compatible id and name / label
764
+ * List item with compatible id and name / label / title
755
765
  */
756
766
  export type ListType2 = {
757
767
  id: DataTypes.IdType;
758
- } & ({ label: string } | { name: string });
768
+ } & ({ label: string } | { name: string } | { title: string });
759
769
 
760
770
  /**
761
771
  * Id default type
@@ -770,3 +780,10 @@ export type IdDefaultType<T extends object> = T extends { id: number | string }
770
780
  export type LabelDefaultType<T extends object> = T extends { label: string }
771
781
  ? DataTypes.Keys<T, string> & 'label'
772
782
  : DataTypes.Keys<T, string>;
783
+
784
+ /**
785
+ * Title default type
786
+ */
787
+ export type TitleDefaultType<T extends object> = T extends { title: string }
788
+ ? DataTypes.Keys<T, string> & 'title'
789
+ : DataTypes.Keys<T, string>;