@etsoo/shared 1.0.94 → 1.0.98

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
@@ -24,6 +24,7 @@ Data type definitions and type safe functions
24
24
  |Name|Description|
25
25
  |---:|---|
26
26
  |DataType|Data type enum|
27
+ |AddOrEditType|Add or edit conditional type for same data model|
27
28
  |Basic|Basic types, includes number, bigint, Date, boolean, string|
28
29
  |BasicArray|Basic type name array|
29
30
  |BasicConditional|Conditional type based on BasicNames|
@@ -38,6 +39,8 @@ Data type definitions and type safe functions
38
39
  |HAlign|Horizontal align|
39
40
  |HAlignEnum|Horizontal align enum|
40
41
  |IdType|Number and string combination id type|
42
+ |IdItem|Item with id or id generator|
43
+ |IdLabelItem|Item with id and label|
41
44
  |Simple|Basic or basic array type|
42
45
  |SimpleEnum|Simple type enum|
43
46
  |SimpleNames|Simple type names|
@@ -56,6 +59,10 @@ Data type definitions and type safe functions
56
59
  |getEnumByValue|Get enum item from value|
57
60
  |getEnumKey|Get enum string literal type value|
58
61
  |getEnumKeys|Get Enum keys|
62
+ |getIdValue|Get object id field value|
63
+ |getItemId|Get item id|
64
+ |getItemLabel|Get item label|
65
+ |getStringValue|Get object string field value|
59
66
  |isBasicName|Check the type is a basic type or not (type guard)|
60
67
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
61
68
  |isSimpleType|Is the input value simple type, include null and undefined|
@@ -74,6 +74,45 @@ 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 getIdValue', () => {
91
+ const data = { id: 1, flag: true };
92
+ expect(DataTypes.getIdValue(data, 'id')).toBe(1);
93
+ expect(DataTypes.getIdValue(data, 'flag')).toBe('true');
94
+ expect(DataTypes.getIdValue(data, 'unknown')).toBeNull();
95
+ });
96
+
97
+ test('Tests for getStringValue', () => {
98
+ const data = { id: 1, flag: true };
99
+ expect(DataTypes.getStringValue(data, 'id')).toBe('1');
100
+ expect(DataTypes.getStringValue(data, 'flag')).toBe('true');
101
+ });
102
+
103
+ test('Tests for getItemLabel', () => {
104
+ // Arrange
105
+ const items: DataTypes.IdLabelItem[] = [
106
+ { id: 1, label: '123' },
107
+ { id: '123', label: () => 'f123' },
108
+ { id: () => 'f123', label: 'l123' }
109
+ ];
110
+
111
+ // Assert
112
+ expect(DataTypes.getItemLabel(items[0])).toBe('123');
113
+ expect(DataTypes.getItemLabel(items[1])).toBe('f123');
114
+ });
115
+
77
116
  test('Tests for isSimpleType', () => {
78
117
  expect(DataTypes.isSimpleType(1)).toBeTruthy();
79
118
  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', () => {
@@ -113,6 +113,10 @@ export declare namespace DataTypes {
113
113
  * Number and string combination id type
114
114
  */
115
115
  type IdType = number | string;
116
+ /**
117
+ * Add or edit conditional type for same data model
118
+ */
119
+ type AddOrEditType<T, Editing extends boolean> = Editing extends true ? T : Partial<T>;
116
120
  /**
117
121
  * Enum value type
118
122
  */
@@ -133,6 +137,36 @@ export declare namespace DataTypes {
133
137
  * Simple object, string key, simple type and null value Record
134
138
  */
135
139
  type SimpleObject = Record<string, Simple | null | undefined>;
140
+ /**
141
+ * Item with id property
142
+ */
143
+ type IdItem = {
144
+ /**
145
+ * Id field or generator
146
+ */
147
+ id: IdType | (() => string);
148
+ };
149
+ /**
150
+ * Item with id and label property
151
+ */
152
+ type IdLabelItem = IdItem & {
153
+ /**
154
+ * label field or generator
155
+ */
156
+ label: string | (() => string);
157
+ };
158
+ /**
159
+ * Get item id
160
+ * @param item Item with id
161
+ * @returns string id
162
+ */
163
+ const getItemId: (item: IdItem) => string;
164
+ /**
165
+ * Get item label
166
+ * @param item Item with id
167
+ * @returns string id
168
+ */
169
+ const getItemLabel: (item: IdLabelItem) => string;
136
170
  /**
137
171
  * Culture definiton
138
172
  */
@@ -215,6 +249,20 @@ export declare namespace DataTypes {
215
249
  * @returns Keys
216
250
  */
217
251
  function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
252
+ /**
253
+ * Get object id field value
254
+ * @param data Data
255
+ * @param key Property name
256
+ * @returns Id value
257
+ */
258
+ function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
259
+ /**
260
+ * Get object string field value
261
+ * @param data Data
262
+ * @param key Property name
263
+ * @returns String value
264
+ */
265
+ function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
218
266
  /**
219
267
  * Check the type is a basic type or not (type guard)
220
268
  * @param name Type name
@@ -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
@@ -286,6 +308,36 @@ var DataTypes;
286
308
  .map((item) => item);
287
309
  }
288
310
  DataTypes.getEnumKeys = getEnumKeys;
311
+ /**
312
+ * Get object id field value
313
+ * @param data Data
314
+ * @param key Property name
315
+ * @returns Id value
316
+ */
317
+ function getIdValue(data, key) {
318
+ if (data == null)
319
+ return null;
320
+ const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
321
+ if (value == null)
322
+ return null;
323
+ if (typeof value === 'number')
324
+ return value;
325
+ return convert(value, 'string');
326
+ }
327
+ DataTypes.getIdValue = getIdValue;
328
+ /**
329
+ * Get object string field value
330
+ * @param data Data
331
+ * @param key Property name
332
+ * @returns String value
333
+ */
334
+ function getStringValue(data, key) {
335
+ const value = getIdValue(data, key);
336
+ if (typeof value === 'number')
337
+ return value.toString();
338
+ return value;
339
+ }
340
+ DataTypes.getStringValue = getStringValue;
289
341
  /**
290
342
  * Check the type is a basic type or not (type guard)
291
343
  * @param name Type name
@@ -113,6 +113,10 @@ export declare namespace DataTypes {
113
113
  * Number and string combination id type
114
114
  */
115
115
  type IdType = number | string;
116
+ /**
117
+ * Add or edit conditional type for same data model
118
+ */
119
+ type AddOrEditType<T, Editing extends boolean> = Editing extends true ? T : Partial<T>;
116
120
  /**
117
121
  * Enum value type
118
122
  */
@@ -133,6 +137,36 @@ export declare namespace DataTypes {
133
137
  * Simple object, string key, simple type and null value Record
134
138
  */
135
139
  type SimpleObject = Record<string, Simple | null | undefined>;
140
+ /**
141
+ * Item with id property
142
+ */
143
+ type IdItem = {
144
+ /**
145
+ * Id field or generator
146
+ */
147
+ id: IdType | (() => string);
148
+ };
149
+ /**
150
+ * Item with id and label property
151
+ */
152
+ type IdLabelItem = IdItem & {
153
+ /**
154
+ * label field or generator
155
+ */
156
+ label: string | (() => string);
157
+ };
158
+ /**
159
+ * Get item id
160
+ * @param item Item with id
161
+ * @returns string id
162
+ */
163
+ const getItemId: (item: IdItem) => string;
164
+ /**
165
+ * Get item label
166
+ * @param item Item with id
167
+ * @returns string id
168
+ */
169
+ const getItemLabel: (item: IdLabelItem) => string;
136
170
  /**
137
171
  * Culture definiton
138
172
  */
@@ -215,6 +249,20 @@ export declare namespace DataTypes {
215
249
  * @returns Keys
216
250
  */
217
251
  function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
252
+ /**
253
+ * Get object id field value
254
+ * @param data Data
255
+ * @param key Property name
256
+ * @returns Id value
257
+ */
258
+ function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
259
+ /**
260
+ * Get object string field value
261
+ * @param data Data
262
+ * @param key Property name
263
+ * @returns String value
264
+ */
265
+ function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
218
266
  /**
219
267
  * Check the type is a basic type or not (type guard)
220
268
  * @param name Type name
@@ -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
@@ -283,6 +305,36 @@ export var DataTypes;
283
305
  .map((item) => item);
284
306
  }
285
307
  DataTypes.getEnumKeys = getEnumKeys;
308
+ /**
309
+ * Get object id field value
310
+ * @param data Data
311
+ * @param key Property name
312
+ * @returns Id value
313
+ */
314
+ function getIdValue(data, key) {
315
+ if (data == null)
316
+ return null;
317
+ const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
318
+ if (value == null)
319
+ return null;
320
+ if (typeof value === 'number')
321
+ return value;
322
+ return convert(value, 'string');
323
+ }
324
+ DataTypes.getIdValue = getIdValue;
325
+ /**
326
+ * Get object string field value
327
+ * @param data Data
328
+ * @param key Property name
329
+ * @returns String value
330
+ */
331
+ function getStringValue(data, key) {
332
+ const value = getIdValue(data, key);
333
+ if (typeof value === 'number')
334
+ return value.toString();
335
+ return value;
336
+ }
337
+ DataTypes.getStringValue = getStringValue;
286
338
  /**
287
339
  * Check the type is a basic type or not (type guard)
288
340
  * @param name Type name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.94",
3
+ "version": "1.0.98",
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
  "homepage": "https://github.com/ETSOO/Shared#readme",
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
- "@types/jest": "^27.0.3",
57
+ "@types/jest": "^27.4.0",
58
58
  "@typescript-eslint/eslint-plugin": "^5.8.0",
59
59
  "@typescript-eslint/parser": "^5.8.0",
60
60
  "eslint": "^8.5.0",
package/src/DataTypes.ts CHANGED
@@ -147,6 +147,13 @@ export namespace DataTypes {
147
147
  */
148
148
  export type IdType = number | string;
149
149
 
150
+ /**
151
+ * Add or edit conditional type for same data model
152
+ */
153
+ export type AddOrEditType<T, Editing extends boolean> = Editing extends true
154
+ ? T
155
+ : Partial<T>;
156
+
150
157
  /**
151
158
  * Enum value type
152
159
  */
@@ -172,6 +179,47 @@ export namespace DataTypes {
172
179
  */
173
180
  export type SimpleObject = Record<string, Simple | null | undefined>;
174
181
 
182
+ /**
183
+ * Item with id property
184
+ */
185
+ export type IdItem = {
186
+ /**
187
+ * Id field or generator
188
+ */
189
+ id: IdType | (() => string);
190
+ };
191
+
192
+ /**
193
+ * Item with id and label property
194
+ */
195
+ export type IdLabelItem = IdItem & {
196
+ /**
197
+ * label field or generator
198
+ */
199
+ label: string | (() => string);
200
+ };
201
+
202
+ /**
203
+ * Get item id
204
+ * @param item Item with id
205
+ * @returns string id
206
+ */
207
+ export const getItemId = (item: IdItem) => {
208
+ if (typeof item.id === 'function') return item.id();
209
+ if (typeof item.id === 'number') return item.id.toString();
210
+ return item.id;
211
+ };
212
+
213
+ /**
214
+ * Get item label
215
+ * @param item Item with id
216
+ * @returns string id
217
+ */
218
+ export const getItemLabel = (item: IdLabelItem) => {
219
+ if (typeof item.label === 'function') return item.label();
220
+ return item.label;
221
+ };
222
+
175
223
  /**
176
224
  * Culture definiton
177
225
  */
@@ -450,6 +498,39 @@ export namespace DataTypes {
450
498
  .map((item) => <K>item);
451
499
  }
452
500
 
501
+ /**
502
+ * Get object id field value
503
+ * @param data Data
504
+ * @param key Property name
505
+ * @returns Id value
506
+ */
507
+ export function getIdValue<T extends {}>(
508
+ data: T | undefined | null,
509
+ key: keyof T | string
510
+ ): IdType | undefined | null {
511
+ if (data == null) return null;
512
+ const value =
513
+ typeof key === 'string' ? Reflect.get(data, key) : data[key];
514
+ if (value == null) return null;
515
+ if (typeof value === 'number') return value;
516
+ return convert(value, 'string');
517
+ }
518
+
519
+ /**
520
+ * Get object string field value
521
+ * @param data Data
522
+ * @param key Property name
523
+ * @returns String value
524
+ */
525
+ export function getStringValue<T extends {}>(
526
+ data: T | undefined | null,
527
+ key: keyof T | string
528
+ ): string | undefined | null {
529
+ const value = getIdValue(data, key);
530
+ if (typeof value === 'number') return value.toString();
531
+ return value;
532
+ }
533
+
453
534
  /**
454
535
  * Check the type is a basic type or not (type guard)
455
536
  * @param name Type name