@etsoo/shared 1.0.94 → 1.0.95

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
@@ -38,6 +38,8 @@ Data type definitions and type safe functions
38
38
  |HAlign|Horizontal align|
39
39
  |HAlignEnum|Horizontal align enum|
40
40
  |IdType|Number and string combination id type|
41
+ |IdItem|Item with id or id generator|
42
+ |IdLabelItem|Item with id and label|
41
43
  |Simple|Basic or basic array type|
42
44
  |SimpleEnum|Simple type enum|
43
45
  |SimpleNames|Simple type names|
@@ -56,6 +58,8 @@ Data type definitions and type safe functions
56
58
  |getEnumByValue|Get enum item from value|
57
59
  |getEnumKey|Get enum string literal type value|
58
60
  |getEnumKeys|Get Enum keys|
61
+ |getItemId|Get item id|
62
+ |getItemLabel|Get item label|
59
63
  |isBasicName|Check the type is a basic type or not (type guard)|
60
64
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
61
65
  |isSimpleType|Is the input value simple type, include null and undefined|
@@ -74,6 +74,32 @@ test('Tests for getEnumKeys', () => {
74
74
  expect(DataTypes.getEnumKeys(ProductUnit)).toContainEqual('GRAM');
75
75
  });
76
76
 
77
+ test('Tests for getItemId', () => {
78
+ // Arrange
79
+ const items: DataTypes.IdItem[] = [
80
+ { id: 1 },
81
+ { id: '123' },
82
+ { id: () => 'f123' }
83
+ ];
84
+
85
+ // Assert
86
+ expect(DataTypes.getItemId(items[0])).toBe('1');
87
+ expect(DataTypes.getItemId(items[2])).toBe('f123');
88
+ });
89
+
90
+ test('Tests for getItemLabel', () => {
91
+ // Arrange
92
+ const items: DataTypes.IdLabelItem[] = [
93
+ { id: 1, label: '123' },
94
+ { id: '123', label: () => 'f123' },
95
+ { id: () => 'f123', label: 'l123' }
96
+ ];
97
+
98
+ // Assert
99
+ expect(DataTypes.getItemLabel(items[0])).toBe('123');
100
+ expect(DataTypes.getItemLabel(items[1])).toBe('f123');
101
+ });
102
+
77
103
  test('Tests for isSimpleType', () => {
78
104
  expect(DataTypes.isSimpleType(1)).toBeTruthy();
79
105
  expect(DataTypes.isSimpleType(new Date())).toBeTruthy();
@@ -7,12 +7,17 @@ test('Tests for all', () => {
7
7
  StorageUtils.setSessionData('number', 3.14);
8
8
  StorageUtils.setSessionData('test', { id: 123, name: 'test' });
9
9
 
10
+ const array = StorageUtils.getSessionData<string[]>('array', []);
11
+ array.push('new');
12
+ StorageUtils.setSessionData('array', array);
13
+
10
14
  expect(StorageUtils.getSessionData<string>('string')).toBe('test');
11
15
  expect(StorageUtils.getSessionData('string1', '')).toBe('');
12
16
  expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
13
17
  expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
14
18
  expect(StorageUtils.getSessionData('number1', 0)).toBe(0);
15
19
  expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
20
+ expect(StorageUtils.getSessionData<string[]>('array')).toEqual(array);
16
21
  });
17
22
 
18
23
  test('Tests for getLocalObject', () => {
@@ -133,6 +133,36 @@ export declare namespace DataTypes {
133
133
  * Simple object, string key, simple type and null value Record
134
134
  */
135
135
  type SimpleObject = Record<string, Simple | null | undefined>;
136
+ /**
137
+ * Item with id property
138
+ */
139
+ type IdItem = {
140
+ /**
141
+ * Id field or generator
142
+ */
143
+ id: IdType | (() => string);
144
+ };
145
+ /**
146
+ * Item with id and label property
147
+ */
148
+ type IdLabelItem = IdItem & {
149
+ /**
150
+ * label field or generator
151
+ */
152
+ label: string | (() => string);
153
+ };
154
+ /**
155
+ * Get item id
156
+ * @param item Item with id
157
+ * @returns string id
158
+ */
159
+ const getItemId: (item: IdItem) => string;
160
+ /**
161
+ * Get item label
162
+ * @param item Item with id
163
+ * @returns string id
164
+ */
165
+ const getItemLabel: (item: IdLabelItem) => string;
136
166
  /**
137
167
  * Culture definiton
138
168
  */
@@ -74,6 +74,28 @@ var DataTypes;
74
74
  VAlignEnum[VAlignEnum["Center"] = 2] = "Center";
75
75
  VAlignEnum[VAlignEnum["Bottom"] = 3] = "Bottom";
76
76
  })(VAlignEnum = DataTypes.VAlignEnum || (DataTypes.VAlignEnum = {}));
77
+ /**
78
+ * Get item id
79
+ * @param item Item with id
80
+ * @returns string id
81
+ */
82
+ DataTypes.getItemId = (item) => {
83
+ if (typeof item.id === 'function')
84
+ return item.id();
85
+ if (typeof item.id === 'number')
86
+ return item.id.toString();
87
+ return item.id;
88
+ };
89
+ /**
90
+ * Get item label
91
+ * @param item Item with id
92
+ * @returns string id
93
+ */
94
+ DataTypes.getItemLabel = (item) => {
95
+ if (typeof item.label === 'function')
96
+ return item.label();
97
+ return item.label;
98
+ };
77
99
  /**
78
100
  * Convert value to target type
79
101
  * @param input Input value
@@ -133,6 +133,36 @@ export declare namespace DataTypes {
133
133
  * Simple object, string key, simple type and null value Record
134
134
  */
135
135
  type SimpleObject = Record<string, Simple | null | undefined>;
136
+ /**
137
+ * Item with id property
138
+ */
139
+ type IdItem = {
140
+ /**
141
+ * Id field or generator
142
+ */
143
+ id: IdType | (() => string);
144
+ };
145
+ /**
146
+ * Item with id and label property
147
+ */
148
+ type IdLabelItem = IdItem & {
149
+ /**
150
+ * label field or generator
151
+ */
152
+ label: string | (() => string);
153
+ };
154
+ /**
155
+ * Get item id
156
+ * @param item Item with id
157
+ * @returns string id
158
+ */
159
+ const getItemId: (item: IdItem) => string;
160
+ /**
161
+ * Get item label
162
+ * @param item Item with id
163
+ * @returns string id
164
+ */
165
+ const getItemLabel: (item: IdLabelItem) => string;
136
166
  /**
137
167
  * Culture definiton
138
168
  */
@@ -71,6 +71,28 @@ export var DataTypes;
71
71
  VAlignEnum[VAlignEnum["Center"] = 2] = "Center";
72
72
  VAlignEnum[VAlignEnum["Bottom"] = 3] = "Bottom";
73
73
  })(VAlignEnum = DataTypes.VAlignEnum || (DataTypes.VAlignEnum = {}));
74
+ /**
75
+ * Get item id
76
+ * @param item Item with id
77
+ * @returns string id
78
+ */
79
+ DataTypes.getItemId = (item) => {
80
+ if (typeof item.id === 'function')
81
+ return item.id();
82
+ if (typeof item.id === 'number')
83
+ return item.id.toString();
84
+ return item.id;
85
+ };
86
+ /**
87
+ * Get item label
88
+ * @param item Item with id
89
+ * @returns string id
90
+ */
91
+ DataTypes.getItemLabel = (item) => {
92
+ if (typeof item.label === 'function')
93
+ return item.label();
94
+ return item.label;
95
+ };
74
96
  /**
75
97
  * Convert value to target type
76
98
  * @param input Input value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.94",
3
+ "version": "1.0.95",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DataTypes.ts CHANGED
@@ -172,6 +172,47 @@ export namespace DataTypes {
172
172
  */
173
173
  export type SimpleObject = Record<string, Simple | null | undefined>;
174
174
 
175
+ /**
176
+ * Item with id property
177
+ */
178
+ export type IdItem = {
179
+ /**
180
+ * Id field or generator
181
+ */
182
+ id: IdType | (() => string);
183
+ };
184
+
185
+ /**
186
+ * Item with id and label property
187
+ */
188
+ export type IdLabelItem = IdItem & {
189
+ /**
190
+ * label field or generator
191
+ */
192
+ label: string | (() => string);
193
+ };
194
+
195
+ /**
196
+ * Get item id
197
+ * @param item Item with id
198
+ * @returns string id
199
+ */
200
+ export const getItemId = (item: IdItem) => {
201
+ if (typeof item.id === 'function') return item.id();
202
+ if (typeof item.id === 'number') return item.id.toString();
203
+ return item.id;
204
+ };
205
+
206
+ /**
207
+ * Get item label
208
+ * @param item Item with id
209
+ * @returns string id
210
+ */
211
+ export const getItemLabel = (item: IdLabelItem) => {
212
+ if (typeof item.label === 'function') return item.label();
213
+ return item.label;
214
+ };
215
+
175
216
  /**
176
217
  * Culture definiton
177
218
  */